navigate to channel page when created channel

This commit is contained in:
Ren Amamiya
2023-04-23 21:21:43 +07:00
parent 4cba4039bc
commit f671521356

View File

@@ -14,12 +14,14 @@ import { useSetAtom } from 'jotai';
import { getEventHash, signEvent } from 'nostr-tools'; import { getEventHash, signEvent } from 'nostr-tools';
import { useContext, useEffect, useState } from 'react'; import { useContext, useEffect, useState } from 'react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { navigate } from 'vite-plugin-ssr/client/router';
export const CreateChannelModal = () => { export const CreateChannelModal = () => {
const [pool]: any = useContext(RelayContext); const [pool]: any = useContext(RelayContext);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [image, setImage] = useState(DEFAULT_AVATAR); const [image, setImage] = useState(DEFAULT_AVATAR);
const [loading, setLoading] = useState(false);
const setChannel = useSetAtom(defaultChannelsAtom); const setChannel = useSetAtom(defaultChannelsAtom);
@@ -32,6 +34,8 @@ export const CreateChannelModal = () => {
} = useForm(); } = useForm();
const onSubmit = (data: any) => { const onSubmit = (data: any) => {
setLoading(true);
const event: any = { const event: any = {
content: JSON.stringify(data), content: JSON.stringify(data),
created_at: dateToUnix(), created_at: dateToUnix(),
@@ -41,7 +45,6 @@ export const CreateChannelModal = () => {
}; };
event.id = getEventHash(event); event.id = getEventHash(event);
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
console.log(event);
// publish channel // publish channel
pool.publish(event, FULL_RELAYS); pool.publish(event, FULL_RELAYS);
@@ -56,10 +59,14 @@ export const CreateChannelModal = () => {
created_at: event.created_at, created_at: event.created_at,
}, },
]); ]);
// close modal
setOpen(false);
// reset form // reset form
reset(); reset();
setTimeout(() => {
// close modal
setOpen(false);
// redirect to channel page
navigate(`/channel?id=${event.id}`);
}, 2000);
}; };
useEffect(() => { useEffect(() => {
@@ -174,9 +181,32 @@ export const CreateChannelModal = () => {
<button <button
type="submit" type="submit"
disabled={!isDirty || !isValid} disabled={!isDirty || !isValid}
className="h-11 w-full transform rounded-lg bg-fuchsia-500 font-medium text-white active:translate-y-1 disabled:cursor-not-allowed disabled:opacity-30" className="inline-flex h-11 w-full transform items-center justify-center rounded-lg bg-fuchsia-500 font-medium text-white active:translate-y-1 disabled:cursor-not-allowed disabled:opacity-30"
> >
Create {loading ? (
<svg
className="h-4 w-4 animate-spin text-black dark:text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
></path>
</svg>
) : (
'Create channel'
)}
</button> </button>
</div> </div>
</form> </form>