This commit is contained in:
Timothy J. Baek
2024-06-15 03:35:44 -06:00
parent a6ee7415d8
commit 3c599e24e5
5 changed files with 183 additions and 63 deletions
@@ -8,54 +8,14 @@
const dispatch = createEventDispatcher();
export let show;
export let memory = {};
let showUpdateBtn = false;
const i18n = getContext('i18n');
let loading = false;
let content = '';
let isMemoryLoaded = false;
$: {
if (memory && memory.id && !isMemoryLoaded) {
showUpdateBtn = true;
content = memory.content;
isMemoryLoaded = true;
}
if (!show) {
showUpdateBtn = false;
isMemoryLoaded = false;
memory = {};
content = '';
}
}
const submitHandler = async () => {
loading = true;
if (memory && memory.id) {
const res = await updateMemoryById(localStorage.token, memory.id, content).catch((error) => {
toast.error(error);
return null;
});
if (res) {
console.log(res);
toast.success('Memory updated successfully');
content = '';
show = false;
isMemoryLoaded = false;
memory = {};
dispatch('save');
}
loading = false;
return;
}
const res = await addNewMemory(localStorage.token, content).catch((error) => {
toast.error(error);
@@ -78,7 +38,7 @@
<div>
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
<div class=" text-lg font-medium self-center">
{memory.id ? $i18n.t('Edit Memory') : $i18n.t('Add Memory')}
{$i18n.t('Add Memory')}
</div>
<button
class="self-center"
@@ -128,12 +88,7 @@
type="submit"
disabled={loading}
>
{#if showUpdateBtn}
{$i18n.t('Update')}
{:else}
{$i18n.t('Add')}
{/if}
{$i18n.t('Add')}
{#if loading}
<div class="ml-2 self-center">