This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo-list-item-update-sheet.tsx
139 lines (121 loc) · 3.54 KB
/
todo-list-item-update-sheet.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use client';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { ChangeEventHandler, ReactNode, useState } from 'react';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Sheet,
SheetClose,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger,
} from '@/components/ui/sheet';
import { QUERY_KEYS } from '@/constants/query-keys';
import { axiosPatchTodoOf } from '@/lib/apis';
import { Todo } from '@/types/todo';
type TodoListItemUpdateSheetProps = {
trigger: ReactNode;
todo: Todo;
};
const TodoListItemUpdateSheet = ({
trigger,
todo,
}: TodoListItemUpdateSheetProps) => {
const queryClient = useQueryClient();
const axiosPatchTodoTitleAndDescriptionOf = async (data: {
todoId: number;
title: string;
description: string;
}) => {
const { todoId, title, description } = data;
if (title === '') {
throw new Error('title is required'); /* unreachable */
}
await axiosPatchTodoOf(todoId, {
title,
description: description === '' ? null : description,
});
};
const { mutate } = useMutation({
mutationFn: axiosPatchTodoTitleAndDescriptionOf,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.TODOS });
toast.success('할 일을 수정했어요.');
},
onError: () => {
toast.error('할 일 수정에 실패했어요. 잠시 후에 시도해주세요.');
},
});
const [title, setTitle] = useState<string>(todo.title);
const [description, setDescription] = useState<string>(
todo.description ?? '',
);
const isTitleEmpty = title === '';
const handleTitleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
const { value } = event.currentTarget;
setTitle(value);
};
const handleDescriptionChange: ChangeEventHandler<HTMLInputElement> = (
event,
) => {
const { value } = event.currentTarget;
setDescription(value);
};
const handleSubmit = () => {
mutate({
todoId: todo.id,
title,
description,
});
};
return (
<Sheet>
<SheetTrigger asChild>{trigger}</SheetTrigger>
<SheetContent side="bottom">
<SheetHeader>
<SheetTitle>Update Todo</SheetTitle>
<SheetDescription>
View and edit the details of this reminder.
</SheetDescription>
</SheetHeader>
<div className="grid gap-4 py-4">
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="title" className="text-right">
Title
</Label>
<Input
id="title"
value={title}
onChange={handleTitleChange}
className="col-span-3"
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="description" className="text-right">
Description
</Label>
<Input
id="description"
value={description}
onChange={handleDescriptionChange}
className="col-span-3"
/>
</div>
</div>
<SheetFooter>
<SheetClose asChild>
<Button disabled={isTitleEmpty} onClick={handleSubmit}>
Save changes
</Button>
</SheetClose>
</SheetFooter>
</SheetContent>
</Sheet>
);
};
export default TodoListItemUpdateSheet;