|
1 | 1 | //import "index.scss" |
2 | 2 | import { useEffect, useState } from 'react' |
3 | 3 | import { Button } from '@/components/ui/button' |
4 | | -import type { Quote } from '@/model/quote.model' |
| 4 | +import type { Quote, QuoteFormData, Tag } from '@/model/quote.model' |
5 | 5 | import { ListQuote } from '@/feature/quote/list.quote' |
6 | 6 | import DeleteQuoteDialog from '@/feature/quote/dialog/delete.quote.dialog' |
7 | 7 | import AddEditQuoteDialog from '@/feature/quote/dialog/add-edit.quote.dialog' |
8 | | -import { getAllQuotes, addQuote, updateQuote, deleteQuote } from '@/db/quote.db' |
| 8 | +import { getAllQuotes, addQuote, updateQuote, deleteQuote, getAllQuote } from '@/db/quote.db' |
9 | 9 | import { PlusIcon, Home, BookOpen, Search, Settings, SearchIcon } from 'lucide-react' |
10 | 10 | import { InputGroup, InputGroupInput, InputGroupAddon } from '@/components/ui/input-group' |
| 11 | +import { addOrGetTag } from '@/db/tag.db' |
| 12 | +import { addQuoteTag, deleteAllQuoteTags, getAllQuotesDetails } from '@/db/quote_tags.db' |
11 | 13 |
|
12 | 14 | function App() { |
13 | 15 | const [loading, setLoading] = useState(false) |
@@ -35,115 +37,150 @@ function App() { |
35 | 37 | setOpenedDeleteDialog(true) |
36 | 38 | } |
37 | 39 |
|
38 | | - const handleSubmit = async (quote: Quote) => { |
39 | | - console.log(quote) |
40 | | - if (quote.id) { // edit |
41 | | - await updateQuote(quote) |
42 | | - } else { |
43 | | - await addQuote({ |
44 | | - ...quote, |
45 | | - id: new Date().getTime(), |
46 | | - }) |
| 40 | + const getTags = async (tags: string[]): Promise<Tag[]> => { |
| 41 | + const result: Tag[] = []; |
| 42 | + |
| 43 | + for (const tag of tags) { |
| 44 | + const savedTag = await addOrGetTag({ name: tag }); |
| 45 | + result.push(savedTag); |
47 | 46 | } |
48 | | - fetchQuotes() |
49 | | - setOpenAddOrEditDialog(false) |
50 | | - setSelectedQuote(null) |
| 47 | + |
| 48 | + return result; |
| 49 | + }; |
| 50 | + |
| 51 | + |
| 52 | + const handleSubmit = async (quote: QuoteFormData) => { |
| 53 | + console.log({quote}) |
| 54 | + let quoteId: number | undefined = quote.id |
| 55 | + if (quoteId) { // edit |
| 56 | + await updateQuote({ |
| 57 | + ...quote, |
| 58 | + text: quote.text || "Empty" |
| 59 | + }) |
| 60 | + } else { |
| 61 | + const newQuote = await addQuote({ |
| 62 | + ...quote, |
| 63 | + text: quote.text || "Empty", |
| 64 | + }) |
| 65 | + quoteId = newQuote.id |
51 | 66 | } |
52 | 67 |
|
53 | | - const handleDeleteSubmit = async (quote: Quote) => { |
54 | | - console.log("deleted") |
55 | | - await deleteQuote(quote.id!) |
56 | | - console.log("deleted") |
57 | | - setSelectedQuote(null) |
58 | | - setOpenedDeleteDialog(false) |
59 | | - fetchQuotes() |
| 68 | + console.log({quoteId}); |
| 69 | + |
| 70 | + const tags = await getTags(quote.tags!) |
| 71 | + console.log({ tags }) |
| 72 | + |
| 73 | + // Delete all existing tags for this quote |
| 74 | + await deleteAllQuoteTags(quoteId!) |
| 75 | + |
| 76 | + // Add new tags for this quote |
| 77 | + for (const tag of tags) { |
| 78 | + await addQuoteTag({ |
| 79 | + quoteId: quoteId!, |
| 80 | + tagId: tag.id! |
| 81 | + }) |
60 | 82 | } |
61 | 83 |
|
62 | | - const fetchQuotes = async () => { |
63 | | - const storedQuotes = await getAllQuotes(); |
64 | | - console.log({ storedQuotes }); |
65 | | - setLoading(false) |
66 | | - setSelectedQuote(null) |
67 | | - setQuotes(storedQuotes); |
68 | | - }; |
| 84 | + fetchQuotes() |
| 85 | + setOpenAddOrEditDialog(false) |
| 86 | + setSelectedQuote(null) |
| 87 | +} |
| 88 | + |
| 89 | +const handleDeleteSubmit = async (quote: QuoteFormData) => { |
| 90 | + console.log("deleted") |
| 91 | + await deleteQuote(quote.id!) |
| 92 | + console.log("deleted") |
| 93 | + setSelectedQuote(null) |
| 94 | + setOpenedDeleteDialog(false) |
| 95 | + fetchQuotes() |
| 96 | +} |
69 | 97 |
|
70 | | - useEffect(() => { |
71 | | - setLoading(true) |
72 | | - fetchQuotes(); |
73 | | - }, []); |
74 | | - |
75 | | - return ( |
76 | | - <div className="bg-primary/10"> |
77 | | - {/* Purple Header */} |
78 | | - <header className="w-full text-white bg-primary p-4 flex flex-col items-center"> |
79 | | - <h1 className="p-4">Quotes Keeper 3.0</h1> |
80 | | - |
81 | | - </header> |
82 | | - |
83 | | - {/* Sticky Search Bar */} |
84 | | - <div |
85 | | - className="rounded-b-2xl sticky top-0 bg-primary p-4"> |
86 | | - <InputGroup |
87 | | - className=" mb-4 text-white border-white border"> |
88 | | - <InputGroupInput placeholder="Search..." className='text-white' /> |
89 | | - <InputGroupAddon> |
90 | | - <SearchIcon className='text-white' /> |
91 | | - </InputGroupAddon> |
92 | | - </InputGroup> |
93 | | - </div> |
94 | | - |
95 | | - {/* Main Content */} |
96 | | - <main className="p-4 "> |
97 | | - <ListQuote |
98 | | - loading={loading} |
99 | | - quotes={quotes} |
100 | | - onEdit={openEditDialog} |
101 | | - onDelete={openDeleteDialog} |
102 | | - /> |
103 | | - </main> |
104 | | - |
105 | | - {/* Bottom Navigation */} |
106 | | - <nav className="flex items-center justify-center "> |
107 | | - {/* <button className="nav-item active"> |
| 98 | +const fetchQuotes = async () => { |
| 99 | + const storedQuotes = await getAllQuotesDetails(); |
| 100 | + console.log({storedQuotes}); |
| 101 | + |
| 102 | + setLoading(false) |
| 103 | + setSelectedQuote(null) |
| 104 | + setQuotes(storedQuotes); |
| 105 | +}; |
| 106 | + |
| 107 | +useEffect(() => { |
| 108 | + setLoading(true) |
| 109 | + fetchQuotes(); |
| 110 | +}, []); |
| 111 | + |
| 112 | +return ( |
| 113 | + <div className="bg-primary/10"> |
| 114 | + {/* Purple Header */} |
| 115 | + <header className="w-full text-white bg-primary p-4 flex flex-col items-center"> |
| 116 | + <h1 className="p-4">Quotes Keeper 3.0</h1> |
| 117 | + |
| 118 | + </header> |
| 119 | + |
| 120 | + {/* Sticky Search Bar */} |
| 121 | + <div |
| 122 | + className="rounded-b-2xl sticky top-0 bg-primary p-4"> |
| 123 | + <InputGroup |
| 124 | + className=" mb-4 text-white border-white border"> |
| 125 | + <InputGroupInput placeholder="Search..." className='text-white' /> |
| 126 | + <InputGroupAddon> |
| 127 | + <SearchIcon className='text-white' /> |
| 128 | + </InputGroupAddon> |
| 129 | + </InputGroup> |
| 130 | + </div> |
| 131 | + |
| 132 | + {/* Main Content */} |
| 133 | + <main className="p-4 "> |
| 134 | + <ListQuote |
| 135 | + loading={loading} |
| 136 | + quotes={quotes} |
| 137 | + onEdit={openEditDialog} |
| 138 | + onDelete={openDeleteDialog} |
| 139 | + /> |
| 140 | + </main> |
| 141 | + |
| 142 | + {/* Bottom Navigation */} |
| 143 | + <nav className="flex items-center justify-center "> |
| 144 | + {/* <button className="nav-item active"> |
108 | 145 | <Home size={24} /> |
109 | 146 | <span>Home</span> |
110 | 147 | </button> |
111 | 148 | <button className="nav-item"> |
112 | 149 | <BookOpen size={24} /> |
113 | 150 | <span>Books</span> |
114 | 151 | </button> */} |
115 | | - <Button |
| 152 | + <Button |
116 | 153 | size="lg" |
117 | | - className=" fixed bottom-4 " |
118 | | - onClick={openAddDialog}> |
119 | | - <PlusIcon /> |
120 | | - </Button> |
121 | | - {/* <button className="nav-item"> |
| 154 | + className=" fixed bottom-4 " |
| 155 | + onClick={openAddDialog}> |
| 156 | + <PlusIcon /> |
| 157 | + </Button> |
| 158 | + {/* <button className="nav-item"> |
122 | 159 | <Search size={24} /> |
123 | 160 | <span>Discover</span> |
124 | 161 | </button> |
125 | 162 | <button className="nav-item"> |
126 | 163 | <Settings size={24} /> |
127 | 164 | <span>Settings</span> |
128 | 165 | </button> */} |
129 | | - </nav> |
130 | | - |
131 | | - <AddEditQuoteDialog |
132 | | - mode={addOrEdit} |
133 | | - quote={selectedQuote} |
134 | | - open={openAddOrEditDialog} |
135 | | - setOpen={setOpenAddOrEditDialog} |
136 | | - handleSubmit={handleSubmit} |
137 | | - /> |
138 | | - |
139 | | - <DeleteQuoteDialog |
140 | | - open={openedDeleteDialog} |
141 | | - setOpen={setOpenedDeleteDialog} |
142 | | - quote={selectedQuote} |
143 | | - handleDelete={handleDeleteSubmit} |
144 | | - /> |
145 | | - </div> |
146 | | - ) |
| 166 | + </nav> |
| 167 | + |
| 168 | + <AddEditQuoteDialog |
| 169 | + mode={addOrEdit} |
| 170 | + quote={selectedQuote} |
| 171 | + open={openAddOrEditDialog} |
| 172 | + setOpen={setOpenAddOrEditDialog} |
| 173 | + handleSubmit={handleSubmit} |
| 174 | + /> |
| 175 | + |
| 176 | + <DeleteQuoteDialog |
| 177 | + open={openedDeleteDialog} |
| 178 | + setOpen={setOpenedDeleteDialog} |
| 179 | + quote={selectedQuote} |
| 180 | + handleDelete={handleDeleteSubmit} |
| 181 | + /> |
| 182 | + </div> |
| 183 | +) |
147 | 184 | } |
148 | 185 |
|
149 | 186 | export default App |
0 commit comments