diff --git a/apps/portal/src/components/questions/CreatListDialog.tsx b/apps/portal/src/components/questions/CreatListDialog.tsx deleted file mode 100644 index 64af7e2d..00000000 --- a/apps/portal/src/components/questions/CreatListDialog.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import { Button, Dialog } from '@tih/ui'; - -export type CreateListDialogProps = { - onCancel: () => void; - onCreate: () => void; - show: boolean; -}; - -export default function CreateListDialog({ - show, - onCancel, - onCreate, -}: CreateListDialogProps) { - return ( - -
-
- -
- - - -
-
- ); -} diff --git a/apps/portal/src/components/questions/CreateListDialog.tsx b/apps/portal/src/components/questions/CreateListDialog.tsx new file mode 100644 index 00000000..d363172c --- /dev/null +++ b/apps/portal/src/components/questions/CreateListDialog.tsx @@ -0,0 +1,67 @@ +import { useForm } from 'react-hook-form'; +import { Button, Dialog, TextInput } from '@tih/ui'; + +import { useFormRegister } from '~/utils/questions/useFormRegister'; + +export type CreateListFormData = { + name: string; +}; + +export type CreateListDialogProps = { + onCancel: () => void; + onSubmit: (data: CreateListFormData) => Promise; + show: boolean; +}; + +export default function CreateListDialog({ + show, + onCancel, + onSubmit, +}: CreateListDialogProps) { + const { + register: formRegister, + handleSubmit, + formState: { isSubmitting }, + } = useForm(); + const register = useFormRegister(formRegister); + + return ( + +
+
+ +
+
+ ); +} diff --git a/apps/portal/src/pages/questions/lists.tsx b/apps/portal/src/pages/questions/lists.tsx index 976701c6..721d5b84 100644 --- a/apps/portal/src/pages/questions/lists.tsx +++ b/apps/portal/src/pages/questions/lists.tsx @@ -8,13 +8,15 @@ import { } from '@heroicons/react/24/outline'; import QuestionListCard from '~/components/questions/card/question/QuestionListCard'; -import CreateListDialog from '~/components/questions/CreatListDialog'; +import type { CreateListFormData } from '~/components/questions/CreateListDialog'; +import CreateListDialog from '~/components/questions/CreateListDialog'; import DeleteListDialog from '~/components/questions/DeleteListDialog'; import { Button } from '~/../../../packages/ui/dist'; import { APP_TITLE } from '~/utils/questions/constants'; import { SAMPLE_QUESTION } from '~/utils/questions/constants'; import createSlug from '~/utils/questions/createSlug'; +import { trpc } from '~/utils/trpc'; export const questions = [ SAMPLE_QUESTION, @@ -34,22 +36,40 @@ export const questions = [ SAMPLE_QUESTION, ]; -export const lists = [ - { id: 1, name: 'list 1', questions }, - { id: 2, name: 'list 2', questions }, - { id: 3, name: 'list 3', questions }, - { id: 4, name: 'list 4', questions }, - { id: 5, name: 'list 5', questions }, -]; - export default function ListPage() { + const utils = trpc.useContext(); + const { data: lists } = trpc.useQuery(['questions.lists.getListsByUser']); + const { mutateAsync: createList } = trpc.useMutation( + 'questions.lists.create', + { + onSuccess: () => { + // TODO: Add optimistic update + utils.invalidateQueries(['questions.lists.getListsByUser']); + }, + }, + ); + const { mutateAsync: deleteList } = trpc.useMutation( + 'questions.lists.delete', + { + onSuccess: () => { + // TODO: Add optimistic update + utils.invalidateQueries(['questions.lists.getListsByUser']); + }, + }, + ); + const [selectedList, setSelectedList] = useState( - (lists ?? []).length > 0 ? lists[0].id : '', + lists?.length ? lists[0].id : '', ); const [showDeleteListDialog, setShowDeleteListDialog] = useState(false); const [showCreateListDialog, setShowCreateListDialog] = useState(false); - const handleDeleteList = () => { + const [listIdToDelete, setListIdToDelete] = useState(''); + + const handleDeleteList = async (listId: string) => { + await deleteList({ + id: listId, + }); setShowDeleteListDialog(false); }; @@ -57,7 +77,10 @@ export default function ListPage() { setShowDeleteListDialog(false); }; - const handleCreateList = () => { + const handleCreateList = async (data: CreateListFormData) => { + await createList({ + name: data.name, + }); setShowCreateListDialog(false); }; @@ -68,7 +91,7 @@ export default function ListPage() { const listOptions = ( <>