Add save button
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { isEqual } from "radash";
|
||||||
|
|
||||||
const { user } = useUserSession();
|
const { user } = useUserSession();
|
||||||
|
|
||||||
|
const toast = useToast();
|
||||||
|
|
||||||
const stravaLink = computed(() => {
|
const stravaLink = computed(() => {
|
||||||
return `https://www.strava.com/athletes/${toValue(user).id}`;
|
return `https://www.strava.com/athletes/${toValue(user).id}`;
|
||||||
});
|
});
|
||||||
@@ -15,22 +19,31 @@ const preferences = useState<FormData>("preferences", () => ({
|
|||||||
language: "English",
|
language: "English",
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const { status } = useLazyFetch("/api/preferences", {
|
const { data: preferencesData, status } = useLazyFetch("/api/preferences", {
|
||||||
onResponse({ error, response }) {
|
onResponse({ error, response }) {
|
||||||
if (error) {
|
if (error) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
preferences.value = response._data;
|
preferences.value = { ...response._data };
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
watchEffect(async () => {
|
const onSavePreferences = async () => {
|
||||||
await $fetch("/api/preferences", {
|
await $fetch("/api/preferences", {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: toValue(preferences),
|
body: toValue(preferences),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
toast.add({
|
||||||
|
title: "Saved",
|
||||||
|
description: "Preferences saved succesfully!",
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const onResetPreferences = () => {
|
||||||
|
preferences.value = { ...preferencesData.value };
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -79,6 +92,17 @@ watchEffect(async () => {
|
|||||||
</template>
|
</template>
|
||||||
</CardField>
|
</CardField>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<template #footer>
|
||||||
|
<div class="flex gap-4">
|
||||||
|
<UButton class="px-4" @click="onSavePreferences"
|
||||||
|
>Save preferences</UButton
|
||||||
|
>
|
||||||
|
<UButton variant="ghost" color="neutral" @click="onResetPreferences"
|
||||||
|
>Cancel</UButton
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
</UCard>
|
</UCard>
|
||||||
</UContainer>
|
</UContainer>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user