popup on goals save
This commit is contained in:
parent
80e1c33c8c
commit
a219b4449a
2 changed files with 52 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
|||
import type { Diary } from '$lib/types/api';
|
||||
import { kcal, g } from '$lib/utils/format';
|
||||
import { updateDiary } from '$lib/api/diary';
|
||||
import { updateUserSettings } from '$lib/api/settings';
|
||||
import { useQueryClient } from '@tanstack/svelte-query';
|
||||
import Sheet from '$lib/components/ui/Sheet.svelte';
|
||||
|
||||
|
|
@ -31,6 +32,8 @@
|
|||
// Goal editing sheet
|
||||
let sheetOpen = $state(false);
|
||||
let saving = $state(false);
|
||||
let syncSettingsOpen = $state(false);
|
||||
let lastSavedForm: typeof form | null = null;
|
||||
let form = $state<{
|
||||
calories_goal: number | null;
|
||||
protein_goal: number;
|
||||
|
|
@ -61,11 +64,19 @@
|
|||
try {
|
||||
await updateDiary(date, form);
|
||||
await queryClient.invalidateQueries({ queryKey: ['diary', date] });
|
||||
lastSavedForm = { ...form };
|
||||
sheetOpen = false;
|
||||
syncSettingsOpen = true;
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSyncToSettings() {
|
||||
if (!lastSavedForm) return;
|
||||
await updateUserSettings(lastSavedForm);
|
||||
syncSettingsOpen = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="bg-zinc-900 rounded-2xl p-4 relative">
|
||||
|
|
@ -125,6 +136,20 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<Sheet open={syncSettingsOpen} onclose={() => syncSettingsOpen = false} title="Update global settings?">
|
||||
<p class="text-sm text-zinc-400 mb-5">Override your default macro goals in user settings with these values as well?</p>
|
||||
<div class="flex gap-3">
|
||||
<button
|
||||
onclick={() => syncSettingsOpen = false}
|
||||
class="flex-1 bg-zinc-800 hover:bg-zinc-700 rounded-xl py-3 text-sm font-medium transition-colors"
|
||||
>No</button>
|
||||
<button
|
||||
onclick={handleSyncToSettings}
|
||||
class="flex-1 bg-green-600 hover:bg-green-500 rounded-xl py-3 font-semibold transition-colors"
|
||||
>Yes</button>
|
||||
</div>
|
||||
</Sheet>
|
||||
|
||||
<Sheet open={sheetOpen} onclose={() => sheetOpen = false} title="Day goals">
|
||||
<form onsubmit={handleSave} class="space-y-4">
|
||||
<!-- Calories: null = auto-calculated by server from macros -->
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
<script lang="ts">
|
||||
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
|
||||
import { getUserSettings, updateUserSettings } from '$lib/api/settings';
|
||||
import { updateDiary } from '$lib/api/diary';
|
||||
import TopBar from '$lib/components/ui/TopBar.svelte';
|
||||
import Sheet from '$lib/components/ui/Sheet.svelte';
|
||||
import { today } from '$lib/utils/date';
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
|
@ -15,6 +17,8 @@
|
|||
let saving = $state(false);
|
||||
let saved = $state(false);
|
||||
let error = $state('');
|
||||
let syncDiaryOpen = $state(false);
|
||||
let lastSavedForm: typeof form | null = null;
|
||||
|
||||
let form = $state<{
|
||||
calories_goal: number | null;
|
||||
|
|
@ -49,14 +53,23 @@
|
|||
try {
|
||||
await updateUserSettings(form);
|
||||
queryClient.invalidateQueries({ queryKey: ['user-settings'] });
|
||||
lastSavedForm = { ...form };
|
||||
saved = true;
|
||||
setTimeout(() => { saved = false; }, 2000);
|
||||
syncDiaryOpen = true;
|
||||
} catch {
|
||||
error = 'Failed to save settings';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSyncToDiary() {
|
||||
if (!lastSavedForm) return;
|
||||
await updateDiary(today(), lastSavedForm);
|
||||
queryClient.invalidateQueries({ queryKey: ['diary', today()] });
|
||||
syncDiaryOpen = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col h-screen">
|
||||
|
|
@ -139,3 +152,17 @@
|
|||
{/if}
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<Sheet open={syncDiaryOpen} onclose={() => syncDiaryOpen = false} title="Update today's diary?">
|
||||
<p class="text-sm text-zinc-400 mb-5">Override today's diary macro goals with these values as well?</p>
|
||||
<div class="flex gap-3">
|
||||
<button
|
||||
onclick={() => syncDiaryOpen = false}
|
||||
class="flex-1 bg-zinc-800 hover:bg-zinc-700 rounded-xl py-3 text-sm font-medium transition-colors"
|
||||
>No</button>
|
||||
<button
|
||||
onclick={handleSyncToDiary}
|
||||
class="flex-1 bg-green-600 hover:bg-green-500 rounded-xl py-3 font-semibold transition-colors"
|
||||
>Yes</button>
|
||||
</div>
|
||||
</Sheet>
|
||||
|
|
|
|||
Loading…
Reference in a new issue