|
12 | 12 | import { error } from '@sveltejs/kit'; |
13 | 13 | import EditMemoryModal from './EditMemoryModal.svelte'; |
14 | 14 | import localizedFormat from 'dayjs/plugin/localizedFormat'; |
| 15 | + import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte'; |
15 | 16 |
|
16 | 17 | const i18n = getContext('i18n'); |
17 | 18 | dayjs.extend(localizedFormat); |
|
26 | 27 |
|
27 | 28 | let selectedMemory = null; |
28 | 29 |
|
| 30 | + let showClearConfirmDialog = false; |
| 31 | +
|
| 32 | + let onClearConfirmed = async () => { |
| 33 | + const res = await deleteMemoriesByUserId(localStorage.token).catch((error) => { |
| 34 | + toast.error(`${error}`); |
| 35 | + return null; |
| 36 | + }); |
| 37 | +
|
| 38 | + if (res && memories.length > 0) { |
| 39 | + toast.success($i18n.t('Memory cleared successfully')); |
| 40 | + memories = []; |
| 41 | + } |
| 42 | + showClearConfirmDialog = false; |
| 43 | + }; |
| 44 | +
|
29 | 45 | $: if (show && memories.length === 0 && loading) { |
30 | 46 | (async () => { |
31 | 47 | memories = await getMemories(localStorage.token); |
|
175 | 191 | > |
176 | 192 | <button |
177 | 193 | class=" px-3.5 py-1.5 font-medium text-red-500 hover:bg-black/5 dark:hover:bg-white/5 outline outline-1 outline-red-300 dark:outline-red-800 rounded-3xl" |
178 | | - on:click={async () => { |
179 | | - const res = await deleteMemoriesByUserId(localStorage.token).catch((error) => { |
180 | | - toast.error(`${error}`); |
181 | | - return null; |
182 | | - }); |
183 | | - |
184 | | - if (res) { |
185 | | - toast.success($i18n.t('Memory cleared successfully')); |
186 | | - memories = []; |
| 194 | + on:click={() => { |
| 195 | + if (memories.length > 0) { |
| 196 | + showClearConfirmDialog = true; |
| 197 | + } else { |
| 198 | + toast.error($i18n.t('No memories to clear')); |
187 | 199 | } |
188 | 200 | }}>{$i18n.t('Clear memory')}</button |
189 | 201 | > |
|
192 | 204 | </div> |
193 | 205 | </Modal> |
194 | 206 |
|
| 207 | +<ConfirmDialog |
| 208 | + title={$i18n.t('Clear Memory')} |
| 209 | + message={$i18n.t('Are you sure you want to clear all memories? This action cannot be undone.')} |
| 210 | + show={showClearConfirmDialog} |
| 211 | + on:confirm={onClearConfirmed} |
| 212 | + on:cancel={() => { |
| 213 | + showClearConfirmDialog = false; |
| 214 | + }} |
| 215 | +/> |
| 216 | + |
195 | 217 | <AddMemoryModal |
196 | 218 | bind:show={showAddMemoryModal} |
197 | 219 | on:save={async () => { |
|
0 commit comments