diff --git a/app/routes/_data.machines._index/action.tsx b/app/routes/_data.machines._index/action.tsx index ffc12b1..b15d003 100644 --- a/app/routes/_data.machines._index/action.tsx +++ b/app/routes/_data.machines._index/action.tsx @@ -114,13 +114,24 @@ export async function menuAction(request: ActionFunctionArgs['request']) { } try { - await post('v1/node/register', session.get('hsApiKey')!, { + const qp = new URLSearchParams() + qp.append('user', user) + qp.append('key', key) + + const url = `v1/node/register?${qp.toString()}` + await post(url, session.get('hsApiKey')!, { user, key, }) - return json({ message: 'Machine registered' }) + return json({ + success: true, + message: 'Machine registered' + }) } catch { - return json({ message: 'Failed to register machine' }, { + return json({ + success: false, + message: 'Failed to register machine' + }, { status: 500, }) } diff --git a/app/routes/_data.machines._index/dialogs/new.tsx b/app/routes/_data.machines._index/dialogs/new.tsx index 5db7622..3548cb9 100644 --- a/app/routes/_data.machines._index/dialogs/new.tsx +++ b/app/routes/_data.machines._index/dialogs/new.tsx @@ -1,5 +1,5 @@ -import { Form, useSubmit } from '@remix-run/react' -import { Dispatch, SetStateAction, useState } from 'react' +import { Form, useFetcher } from '@remix-run/react' +import { Dispatch, SetStateAction, useState, useEffect } from 'react' import { PlusIcon, ServerIcon, KeyIcon } from '@primer/octicons-react' import { cn } from '~/utils/cn' @@ -8,6 +8,8 @@ import Dialog from '~/components/Dialog' import TextField from '~/components/TextField' import Select from '~/components/Select' import Menu from '~/components/Menu' +import Spinner from '~/components/Spinner' +import { toast } from '~/components/Toaster' import { Machine, User } from '~/types' export interface NewProps { @@ -16,11 +18,26 @@ export interface NewProps { } export default function New(data: NewProps) { - const submit = useSubmit() + const fetcher = useFetcher() const mkeyState = useState(false) const pkeyState = useState(false) const [mkey, setMkey] = useState('') - const [user, setUser] = useState(data.users[0].id) + const [user, setUser] = useState('') + const [toasted, setToasted] = useState(false) + + useEffect(() => { + if (!fetcher.data || toasted) { + return + } + + if (fetcher.data.success) { + toast('Registered new machine') + } else { + toast('Failed to register machine due to an invalid key') + } + + setToasted(true) + }, [fetcher.data, toasted, mkey]) return ( <> @@ -43,17 +60,15 @@ export default function New(data: NewProps) { {' '} on your device. -
+ > )} diff --git a/app/routes/_data.users._index/route.tsx b/app/routes/_data.users._index/route.tsx index b6d79c2..b7a6e5b 100644 --- a/app/routes/_data.users._index/route.tsx +++ b/app/routes/_data.users._index/route.tsx @@ -335,7 +335,7 @@ function UserCard({ user, magic }: CardProps) {