From 1bac732008ecf140ad906ee5ea790bc8ce6a2368 Mon Sep 17 00:00:00 2001 From: bombies Date: Thu, 26 Oct 2023 08:31:39 -0500 Subject: [PATCH] Finish dream log implementation --- .../dreams/CurrentDreamsContainer.tsx | 4 +- .../dashboard/components/dreams/DreamCard.tsx | 12 +- .../forms/characters/AddCharacterForm.tsx | 7 +- .../dreams/forms/log/DreamCharacterSelect.tsx | 17 +- .../dreams/forms/log/DreamTagSelect.tsx | 17 +- .../dreams/forms/log/LogDreamForm.tsx | 221 ++++++++++++------ .../dreams/forms/log/LogDreamModal.tsx | 5 +- .../dreams/forms/tags/AddTagForm.tsx | 7 +- .../(site)/(internal)/dashboard/layout.tsx | 4 +- src/app/(site)/(internal)/dashboard/page.tsx | 2 +- 10 files changed, 197 insertions(+), 99 deletions(-) diff --git a/src/app/(site)/(internal)/dashboard/components/dreams/CurrentDreamsContainer.tsx b/src/app/(site)/(internal)/dashboard/components/dreams/CurrentDreamsContainer.tsx index e088592..ad3618f 100644 --- a/src/app/(site)/(internal)/dashboard/components/dreams/CurrentDreamsContainer.tsx +++ b/src/app/(site)/(internal)/dashboard/components/dreams/CurrentDreamsContainer.tsx @@ -17,12 +17,12 @@ const CurrentDreamsContainer: FC = () => { )), [dreams.data, endOfToday, startOfToday]) return ( -
+

Today - {startOfToday.toLocaleDateString("en-US", { dateStyle: "medium" })}

-
+
{dreamCards}
diff --git a/src/app/(site)/(internal)/dashboard/components/dreams/DreamCard.tsx b/src/app/(site)/(internal)/dashboard/components/dreams/DreamCard.tsx index 8e41e05..c709e7d 100644 --- a/src/app/(site)/(internal)/dashboard/components/dreams/DreamCard.tsx +++ b/src/app/(site)/(internal)/dashboard/components/dreams/DreamCard.tsx @@ -11,20 +11,20 @@ const DreamCard: FC = ({dream}) => { return ( -

{dream.title}

-

+

{dream.title}

+

{new Date(dream.createdAt.toString()).toLocaleTimeString("en-US", { timeStyle: 'short' })}

-

+

{dream.description.substring(0, Math.min(dream.description.length, 100))}

diff --git a/src/app/(site)/(internal)/dashboard/components/dreams/forms/characters/AddCharacterForm.tsx b/src/app/(site)/(internal)/dashboard/components/dreams/forms/characters/AddCharacterForm.tsx index a85453d..6b077c6 100644 --- a/src/app/(site)/(internal)/dashboard/components/dreams/forms/characters/AddCharacterForm.tsx +++ b/src/app/(site)/(internal)/dashboard/components/dreams/forms/characters/AddCharacterForm.tsx @@ -69,7 +69,11 @@ const AddCharacterForm: FC = ({onSuccess}) => { } return ( -
+
= ({onSuccess}) => { isLoading={isCreating} isDisabled={isCreating} type="submit" + form="add_dream_character_form" color="primary" variant="shadow" >Create Character diff --git a/src/app/(site)/(internal)/dashboard/components/dreams/forms/log/DreamCharacterSelect.tsx b/src/app/(site)/(internal)/dashboard/components/dreams/forms/log/DreamCharacterSelect.tsx index dede788..7e021f3 100644 --- a/src/app/(site)/(internal)/dashboard/components/dreams/forms/log/DreamCharacterSelect.tsx +++ b/src/app/(site)/(internal)/dashboard/components/dreams/forms/log/DreamCharacterSelect.tsx @@ -7,35 +7,34 @@ import {Chip} from "@nextui-org/chip"; import {SelectItem} from "@nextui-org/react"; import {DreamCharactersState} from "@/app/(site)/(internal)/dashboard/components/dreams/hooks/useDreamCharacters"; import {UseFormRegister} from "react-hook-form"; -import AddCharacterModal from "@/app/(site)/(internal)/dashboard/components/dreams/forms/characters/AddCharacterModal"; import PlusIcon from "@/app/(site)/components/icons/PlusIcon"; type Props = { register?: UseFormRegister, - characters: DreamCharactersState + characters: DreamCharactersState, + isDisabled?: boolean, + onModalOpen?: () => void, } -const DreamCharacterSelect: FC = ({characters, register}) => { +const DreamCharacterSelect: FC = ({onModalOpen, isDisabled, characters, register}) => { const [modalOpen, setModalOpen] = useState(false) return ( - setModalOpen(false)} - />
& { tags?: string[] | string @@ -20,79 +28,160 @@ type FormProps = Omit & { type Props = { onForget?: () => void; + onCreate?: (dream: Dream) => void; } -const LogDreamForm: FC = ({onForget}) => { - const {characters, tags} = useDreamsData() +type CreateDreamArgs = { + arg: { + dto: PostDreamDto + } +} + +const CreateDream = () => { + const mutator = (url: string, {arg}: CreateDreamArgs) => axios.post(url, arg.dto) + return useSWRMutation('/api/me/dreams', mutator) +} + +const LogDreamForm: FC = ({onCreate, onForget}) => { + const {data: session} = useSession() + const [addTagModalOpen, setAddTagModalOpen] = useState(false) + const [addCharacterModalOpen, setAddCharacterModalOpen] = useState(false) + const {characters, tags, dreams} = useDreamsData() const {register, handleSubmit} = useForm() + const {trigger: createDream, isMutating: dreamIsCreating} = CreateDream() + + const handleDreamCreation = useCallback(async (dto: PostDreamDto) => ( + createDream({dto}) + .then(res => { + const dream = res.data!! + if (onCreate) + onCreate(dream) + return dream + }) + .catch(handleAxiosError) + ), [createDream, onCreate]) + + const onSubmit: SubmitHandler = useCallback(async ({ + tags: dataTags, + characters: dataCharacters, + ...data + }) => { + if (!session?.user) + return - const onSubmit: SubmitHandler = useCallback(({tags, characters, ...data}) => { const mutableData: PostDreamDto = {...data} - if (typeof tags === "string") - mutableData.tags = tags.split(",") - if (typeof characters === "string") - mutableData.characters = characters.split(",") - console.log(mutableData) - }, []) + if (typeof dataTags === "string") + mutableData.tags = dataTags.split(",") + if (typeof dataCharacters === "string") + mutableData.characters = dataCharacters.split(",") + + const {tags: tagIds, characters: characterIds, comments, ...dreamData} = mutableData + // In case there's ever a need to include the tags and such in the optimistic data + // const tagObjects = tagIds && tags.data.filter(tag => tagIds.some(id => id === tag.id)) + // const characterObjects = characterIds && characters.data.filter(character => characterIds.some(id => id === character.id)) + + await toast.promise(dreams.optimisticData + .addOptimisticData( + () => handleDreamCreation(mutableData), + { + id: '', + userId: session.user.id, + ...dreamData, + comments: comments ?? null, + createdAt: new Date(), + updatedAt: new Date() + } + ), { + loading: "Creating new dream log...", + success: "Successfully logged your dream!", + error: "Could not log your dream!" + } + ) + }, [dreams.optimisticData, handleDreamCreation, session?.user]) return ( - -
- -