From 075f7bfba8addb01733fe9edee5f86e93096f6b4 Mon Sep 17 00:00:00 2001 From: Jeff Sieu Date: Sun, 6 Nov 2022 02:14:58 +0800 Subject: [PATCH] [questions][fix] ui fixes and enhancements (#514) Co-authored-by: wlren --- .../questions/AddToListDropdown.tsx | 177 ++++++++++-------- .../questions/ContributeQuestionCard.tsx | 50 +---- .../questions/SortOptionsSelect.tsx | 18 +- .../card/question/BaseQuestionCard.tsx | 9 +- .../forms/ContributeQuestionForm.tsx | 20 +- .../forms/CreateQuestionEncounterForm.tsx | 41 ++-- .../[questionId]/[questionSlug]/index.tsx | 10 +- apps/portal/src/pages/questions/browse.tsx | 63 ++++--- apps/portal/src/pages/questions/lists.tsx | 100 ++++++---- apps/portal/src/utils/questions/constants.ts | 15 ++ apps/portal/src/utils/questions/mutations.ts | 60 ++++++ apps/portal/src/utils/questions/useVote.ts | 143 ++++++++++++-- 12 files changed, 476 insertions(+), 230 deletions(-) create mode 100644 apps/portal/src/utils/questions/mutations.ts diff --git a/apps/portal/src/components/questions/AddToListDropdown.tsx b/apps/portal/src/components/questions/AddToListDropdown.tsx index 4ae9da46..039edd7e 100644 --- a/apps/portal/src/components/questions/AddToListDropdown.tsx +++ b/apps/portal/src/components/questions/AddToListDropdown.tsx @@ -3,22 +3,52 @@ import type { PropsWithChildren } from 'react'; import { useMemo } from 'react'; import { Fragment, useRef, useState } from 'react'; import { Menu, Transition } from '@headlessui/react'; -import { CheckIcon, HeartIcon } from '@heroicons/react/20/solid'; +import { CheckIcon, HeartIcon, PlusIcon } from '@heroicons/react/20/solid'; +import { + useAddQuestionToListAsync, + useCreateListAsync, + useRemoveQuestionFromListAsync, +} from '~/utils/questions/mutations'; import { useProtectedCallback } from '~/utils/questions/useProtectedCallback'; import { trpc } from '~/utils/trpc'; +import CreateListDialog from './CreateListDialog'; + export type AddToListDropdownProps = { questionId: string; }; +export type DropdownButtonProps = PropsWithChildren<{ + onClick: () => void; +}>; + +function DropdownButton({ onClick, children }: DropdownButtonProps) { + return ( + + {({ active }) => ( + + )} + + ); +} + export default function AddToListDropdown({ questionId, }: AddToListDropdownProps) { const [menuOpened, setMenuOpened] = useState(false); const ref = useRef(null); + const [show, setShow] = useState(false); - const utils = trpc.useContext(); + const createListAsync = useCreateListAsync(); const { data: lists } = trpc.useQuery(['questions.lists.getListsByUser']); const listsWithQuestionData = useMemo(() => { @@ -30,25 +60,8 @@ export default function AddToListDropdown({ })); }, [lists, questionId]); - const { mutateAsync: addQuestionToList } = trpc.useMutation( - 'questions.lists.createQuestionEntry', - { - // TODO: Add optimistic update - onSuccess: () => { - utils.invalidateQueries(['questions.lists.getListsByUser']); - }, - }, - ); - - const { mutateAsync: removeQuestionFromList } = trpc.useMutation( - 'questions.lists.deleteQuestionEntry', - { - // TODO: Add optimistic update - onSuccess: () => { - utils.invalidateQueries(['questions.lists.getListsByUser']); - }, - }, - ); + const addQuestionToList = useAddQuestionToListAsync(); + const removeQuestionFromList = useRemoveQuestionFromListAsync(); const addClickOutsideListener = () => { document.addEventListener('click', handleClickOutside, true); @@ -101,63 +114,79 @@ export default function AddToListDropdown({ ); return ( - -
- - -
- - - - {menuOpened && ( - <> - {(listsWithQuestionData ?? []).map((list) => ( -
- - {({ active }) => ( - - )} - -
- ))} - - )} -
-
-
+ + + + ))} + { + setShow(true); + }}> + + + )} + + + + { + setShow(false); + }} + onSubmit={async (data) => { + await createListAsync(data); + setShow(false); + }} + /> + ); } diff --git a/apps/portal/src/components/questions/ContributeQuestionCard.tsx b/apps/portal/src/components/questions/ContributeQuestionCard.tsx index f91e1eb4..be050d96 100644 --- a/apps/portal/src/components/questions/ContributeQuestionCard.tsx +++ b/apps/portal/src/components/questions/ContributeQuestionCard.tsx @@ -1,9 +1,4 @@ import { useState } from 'react'; -import { - BuildingOffice2Icon, - CalendarDaysIcon, - QuestionMarkCircleIcon, -} from '@heroicons/react/24/outline'; import { TextInput } from '@tih/ui'; import { useProtectedCallback } from '~/utils/questions/useProtectedCallback'; @@ -32,44 +27,19 @@ export default function ContributeQuestionCard({ return (
+
{createButton}
{listOptions}
+
+
+