-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start dream edit front-end implementation
- Loading branch information
Showing
6 changed files
with
228 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/app/(site)/(internal)/dashboard/components/dreams/card/DreamView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"use client" | ||
|
||
import {FC, Fragment, useEffect, useMemo, useState} from "react"; | ||
import {Dream} from "@prisma/client"; | ||
import {DreamWithRelations, PatchDreamDto} from "@/app/api/me/dreams/dreams.dto"; | ||
import {calcEstimatedReadingTime, fetcher} from "@/utils/client/client-utils"; | ||
import useSWR from "swr"; | ||
import {Chip} from "@nextui-org/chip"; | ||
import EditableInput from "@/app/(site)/components/EditableInput"; | ||
import {Button} from "@nextui-org/button"; | ||
|
||
type Props = { | ||
dream: Dream, | ||
fetchDream?: boolean, | ||
onEdit?: (dto: PatchDreamDto) => void | ||
} | ||
|
||
const FetchFullDream = (dream: Dream, doFetch: boolean) => { | ||
return useSWR(doFetch && `/api/me/dreams/${dream.id}?tags=true&characters=true`, fetcher<DreamWithRelations | null>, {refreshInterval: 0}) | ||
} | ||
|
||
const DreamView: FC<Props> = ({dream, fetchDream, onEdit}) => { | ||
const {data: fullDream, error: fullDreamError} = FetchFullDream(dream, fetchDream ?? true) | ||
const [editMode, setEditMode] = useState(false) | ||
|
||
const tagChips = useMemo(() => fullDream?.tags?.map(tag => ( | ||
<Chip key={tag.id} color="primary" variant="flat" size="sm"> | ||
{tag.tag} | ||
</Chip> | ||
)), [fullDream?.tags]) | ||
|
||
useEffect(() => { | ||
if (fullDreamError) | ||
console.error(fullDreamError) | ||
}, [fullDreamError]) | ||
|
||
return ( | ||
<Fragment> | ||
<Button onPress={() => setEditMode(prev => !prev)}> | ||
Toggle Edit Mode | ||
</Button> | ||
{(tagChips || fullDreamError) && ( | ||
<div className="flex flex-wrap gap-2 mb-3"> | ||
{tagChips ?? (fullDreamError && | ||
<Chip color="danger" variant="flat" size="sm"> | ||
Error Loading Tags | ||
</Chip> | ||
)} | ||
</div> | ||
)} | ||
<EditableInput | ||
isEditable={editMode} | ||
label="Edit Dream Title" | ||
value={dream.title} | ||
onEdit={(value) => { | ||
console.log(value) | ||
}} | ||
> | ||
<h1 className="text-4xl phone:text-2xl font-bold">{dream.title}</h1> | ||
</EditableInput> | ||
<h3 className="text-subtext text-sm font-semibold italic">{dream.comments}</h3> | ||
<h3 className="text-subtext text-xs italic">~{calcEstimatedReadingTime(dream.description)} min. | ||
read</h3> | ||
<article | ||
className="text-[#EAE0FF] phone:text-sm whitespace-pre-wrap rounded-3xl border border-primary/40 bg-[#0C0015]/50 p-6 mt-6">{dream.description}</article> | ||
</Fragment> | ||
) | ||
} | ||
|
||
export default DreamView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
"use client" | ||
|
||
import {FC, PropsWithChildren, useCallback, useEffect, useState} from "react"; | ||
import {InputProps} from "@nextui-org/react"; | ||
import {AnimatePresence, motion} from "framer-motion"; | ||
import Input from "@/app/(site)/components/Input"; | ||
import {Button} from "@nextui-org/button"; | ||
import {SubmitHandler, useForm} from "react-hook-form"; | ||
import CheckIcon from "@/app/(site)/components/icons/CheckIcon"; | ||
import CloseIcon from "@/app/(site)/components/icons/CloseIcon"; | ||
|
||
type FormProps = { | ||
value: string | ||
} | ||
|
||
type Props = { | ||
isEditable?: boolean, | ||
value?: string, | ||
onEdit?: (value: string) => void | ||
} & Pick<InputProps, "classNames" | "placeholder" | "label"> & PropsWithChildren | ||
|
||
const EditableInput: FC<Props> = ({isEditable, value, children, onEdit, ...inputProps}) => { | ||
const {register, handleSubmit} = useForm<FormProps>() | ||
const [currentValue, setCurrentValue] = useState(value ?? "") | ||
const [editToggled, setEditToggled] = useState(false) | ||
|
||
useEffect(() => { | ||
if (isEditable) | ||
setCurrentValue(value ?? "") | ||
else setEditToggled(false) | ||
}, [isEditable, value]) | ||
|
||
const onSubmit: SubmitHandler<FormProps> = useCallback((data) => { | ||
if (data.value === value) | ||
return setEditToggled(false) | ||
|
||
if (onEdit) | ||
onEdit(data.value) | ||
setEditToggled(false) | ||
}, [onEdit, value]) | ||
|
||
return ( | ||
<AnimatePresence> | ||
{isEditable ? (editToggled ? | ||
<motion.form | ||
initial={{opacity: 0}} | ||
animate={{opacity: 1}} | ||
exit={{opacity: 1}} | ||
onSubmit={handleSubmit(onSubmit)} | ||
> | ||
<Input | ||
isRequired | ||
{...inputProps} | ||
register={register} | ||
id={"value"} | ||
value={currentValue} | ||
onValueChange={setCurrentValue} | ||
endContent={ | ||
<div className="flex gap-2"> | ||
<Button | ||
isIconOnly | ||
color="success" | ||
variant="light" | ||
type="submit" | ||
> | ||
<CheckIcon/> | ||
</Button> | ||
<Button | ||
isIconOnly | ||
color="danger" | ||
variant="light" | ||
onPress={() => { | ||
setEditToggled(false) | ||
setCurrentValue(value ?? "") | ||
}} | ||
> | ||
<CloseIcon/> | ||
</Button> | ||
</div> | ||
} | ||
/> | ||
|
||
</motion.form> | ||
: | ||
<motion.div | ||
className="cursor-pointer" | ||
onClick={() => setEditToggled(true)} | ||
initial={{ | ||
color: "#EAE0FF" | ||
}} | ||
whileHover={{ | ||
color: "#9E23FF" | ||
}} | ||
> | ||
{children} | ||
</motion.div> | ||
) : ( | ||
<motion.div | ||
initial={{opacity: 0}} | ||
animate={{opacity: 1}} | ||
exit={{opacity: 1}} | ||
> | ||
{children} | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
) | ||
} | ||
|
||
export default EditableInput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters