|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
2 | | -import { Button, Flex, Input, Spinner } from "@chakra-ui/react"; |
| 2 | +import { Button, Flex, Input, Spinner, useColorMode } from "@chakra-ui/react"; |
3 | 3 | import { useMutation, useQueryClient } from "@tanstack/react-query"; |
4 | 4 | import { useState } from "react"; |
5 | 5 | import { IoMdAdd } from "react-icons/io"; |
6 | 6 | import { BASE_URL } from "../App"; |
7 | 7 |
|
8 | 8 | const TodoForm = () => { |
9 | | - const [newTodo, setNewTodo] = useState(""); |
| 9 | + const [newTodo, setNewTodo] = useState(""); |
| 10 | + const { colorMode } = useColorMode(); // Chakra UI hook to get the current color mode |
10 | 11 |
|
11 | | - const queryClient = useQueryClient(); |
| 12 | + const queryClient = useQueryClient(); |
12 | 13 |
|
13 | | - const { mutate: createTodo, isPending: isCreating } = useMutation({ |
14 | | - mutationKey: ["createTodo"], |
15 | | - mutationFn: async (e: React.FormEvent) => { |
16 | | - e.preventDefault(); |
17 | | - try { |
18 | | - const res = await fetch(BASE_URL + `/todos`, { |
19 | | - method: "POST", |
20 | | - headers: { |
21 | | - "Content-Type": "application/json", |
22 | | - }, |
23 | | - body: JSON.stringify({ body: newTodo }), |
24 | | - }); |
25 | | - const data = await res.json(); |
| 14 | + const { mutate: createTodo, isPending: isCreating } = useMutation({ |
| 15 | + mutationKey: ["createTodo"], |
| 16 | + mutationFn: async (e: React.FormEvent) => { |
| 17 | + e.preventDefault(); |
| 18 | + try { |
| 19 | + const res = await fetch(BASE_URL + `/todos`, { |
| 20 | + method: "POST", |
| 21 | + headers: { |
| 22 | + "Content-Type": "application/json", |
| 23 | + }, |
| 24 | + body: JSON.stringify({ body: newTodo }), |
| 25 | + }); |
| 26 | + const data = await res.json(); |
26 | 27 |
|
27 | | - if (!res.ok) { |
28 | | - throw new Error(data.error || "Something went wrong"); |
29 | | - } |
| 28 | + if (!res.ok) { |
| 29 | + throw new Error(data.error || "Something went wrong"); |
| 30 | + } |
30 | 31 |
|
31 | | - setNewTodo(""); |
32 | | - return data; |
33 | | - } catch (error: any) { |
34 | | - throw new Error(error); |
35 | | - } |
36 | | - }, |
37 | | - onSuccess: () => { |
38 | | - queryClient.invalidateQueries({ queryKey: ["todos"] }); |
39 | | - }, |
40 | | - onError: (error: any) => { |
41 | | - alert(error.message); |
42 | | - }, |
43 | | - }); |
| 32 | + setNewTodo(""); |
| 33 | + return data; |
| 34 | + } catch (error: any) { |
| 35 | + throw new Error(error); |
| 36 | + } |
| 37 | + }, |
| 38 | + onSuccess: () => { |
| 39 | + queryClient.invalidateQueries({ queryKey: ["todos"] }); |
| 40 | + }, |
| 41 | + onError: (error: any) => { |
| 42 | + alert(error.message); |
| 43 | + }, |
| 44 | + }); |
44 | 45 |
|
45 | | - return ( |
46 | | - <form onSubmit={createTodo}> |
47 | | - <Flex gap={2}> |
48 | | - <Input |
49 | | - type='text' |
50 | | - value={newTodo} |
51 | | - onChange={(e) => setNewTodo(e.target.value)} |
52 | | - ref={(input) => input && input.focus()} |
53 | | - /> |
54 | | - <Button |
55 | | - mx={2} |
56 | | - type='submit' |
57 | | - _active={{ |
58 | | - transform: "scale(.97)", |
59 | | - }} |
60 | | - > |
61 | | - {isCreating ? <Spinner size={"xs"} /> : <IoMdAdd size={30} />} |
62 | | - </Button> |
63 | | - </Flex> |
64 | | - </form> |
65 | | - ); |
| 46 | + return ( |
| 47 | + <form onSubmit={createTodo}> |
| 48 | + <Flex gap={2}> |
| 49 | + <Input |
| 50 | + type="text" |
| 51 | + value={newTodo} |
| 52 | + onChange={(e) => setNewTodo(e.target.value)} |
| 53 | + ref={(input) => input && input.focus()} |
| 54 | + style={{ |
| 55 | + border: `1px solid ${colorMode === "dark" ? "white" : "black"}`, // Change border color based on mode |
| 56 | + }} |
| 57 | + /> |
| 58 | + <Button |
| 59 | + mx={2} |
| 60 | + type="submit" |
| 61 | + _active={{ |
| 62 | + transform: "scale(.97)", |
| 63 | + }} |
| 64 | + > |
| 65 | + {isCreating ? <Spinner size={"xs"} /> : <IoMdAdd size={30} />} |
| 66 | + </Button> |
| 67 | + </Flex> |
| 68 | + </form> |
| 69 | + ); |
66 | 70 | }; |
67 | 71 | export default TodoForm; |
68 | 72 |
|
|
0 commit comments