From d969b9e21b5d9c6abb94da3b9d0e956d7012483e Mon Sep 17 00:00:00 2001 From: klemek Date: Sun, 12 Jul 2026 17:46:21 +0200 Subject: [PATCH] feat: store options in cookies --- README.md | 8 ++--- resources/ts/components/TaskList.vue | 12 ++++--- resources/ts/enums.ts | 22 ++++++------ resources/ts/lib/cookies.ts | 52 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 resources/ts/lib/cookies.ts diff --git a/README.md b/README.md index dc9605e..d7924a8 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,14 @@ - [x] list option: show next refresh - [x] list option: move completed below - [x] list option: hide completed -- [ ] options in cookies -- [ ] list per URL +- [x] options in cookies - [ ] Empty list message -- [ ] home screen - [ ] change lang -- [ ] lang in cookies - [ ] toasts - [ ] handle errors +- [ ] list per URL +- [ ] home screen +- [ ] lang in cookies - [ ] simple view - [ ] Dockerfile - [ ] README diff --git a/resources/ts/components/TaskList.vue b/resources/ts/components/TaskList.vue index 48f8c66..320c40c 100644 --- a/resources/ts/components/TaskList.vue +++ b/resources/ts/components/TaskList.vue @@ -6,15 +6,17 @@ import { CirclePlus } from "@lucide/vue"; import TaskItem from "@/components/TaskItem.vue"; import { taskCompleted, sortTasks } from "@/lib/tasks"; import { SortType } from "@/enums"; +import { booleanCookieRef, enumCookieRef } from "@/lib/cookies"; const taskInput = ref(""); const tasks = ref([]); const list = ref("test"); -const sortType = ref(SortType.RESET_DATE); -const sortReverse = ref(false); -const showLastChecked = ref(false); -const showNextReset = ref(false); -const separateCompleted = ref(false); + +const sortType = enumCookieRef("toutDouxSortType", SortType.CREATED_AT); +const sortReverse = booleanCookieRef("toutDouxSortReverse", false); +const showLastChecked = booleanCookieRef("toutDouxShowLastChecked", false); +const showNextReset = booleanCookieRef("toutDouxShowNextReset", false); +const separateCompleted = booleanCookieRef("toutDouxSeparateCompleted", false); const hasCompleted = computed(() => tasks.value.some(taskCompleted)); diff --git a/resources/ts/enums.ts b/resources/ts/enums.ts index 823f4bb..230bbeb 100644 --- a/resources/ts/enums.ts +++ b/resources/ts/enums.ts @@ -1,16 +1,16 @@ export enum CronType { - ONE_TIME = 1, - EVERY_DAY = 2, - EVERY_WEEK = 3, - EVERY_MONTH = 4, - EVERY_YEAR = 5, - SPECIFIC = 6, + ONE_TIME, + EVERY_DAY, + EVERY_WEEK, + EVERY_MONTH, + EVERY_YEAR, + SPECIFIC, } export enum SortType { - CREATED_AT = 1, - UPDATED_AT = 2, - CHECK_DATE = 3, - RESET_DATE = 4, - NAME = 5, + CREATED_AT, + UPDATED_AT, + CHECK_DATE, + RESET_DATE, + NAME, } diff --git a/resources/ts/lib/cookies.ts b/resources/ts/lib/cookies.ts new file mode 100644 index 0000000..817f8bd --- /dev/null +++ b/resources/ts/lib/cookies.ts @@ -0,0 +1,52 @@ +import { customRef } from "vue"; + +export function setCookie(name: string, value: string, days = 365) { + const date = new Date(); + date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); + const expires = `expires=${date.toUTCString()}`; + document.cookie = `${name}=${value}; path=/; ${expires}`; +} + +export function getCookie(name: string, defaultValue: string): string { + const prefix = `${name}=`; + const decodedCookie = decodeURIComponent(document.cookie); + const cookies = decodedCookie.split(";"); + for (const cookie of cookies) { + if (cookie.trim().startsWith(prefix)) { + return cookie.trim().substring(prefix.length, cookie.length); + } + } + return defaultValue; +} + +export function cookieRef( + name: string, + defaultValue: T, + deserializer: (value: string) => T, + serializer: (value: T) => string, +) { + let value = deserializer(getCookie(name, serializer(defaultValue))); + setCookie(name, serializer(value)); + + return customRef((track, trigger) => { + return { + get() { + track(); + return value; + }, + set(newValue) { + value = newValue; + trigger(); + setCookie(name, serializer(value)); + }, + }; + }); +} + +export function booleanCookieRef(name: string, defaultValue: boolean) { + return cookieRef(name, defaultValue, (v) => v === "1", (v) => v ? "1" : "0"); +} + +export function enumCookieRef(name: string, defaultValue: T) { + return cookieRef(name, defaultValue, (v) => parseInt(v) as T, (v: T) => v.toFixed(0)); +}