diff options
| author | Fuwn <[email protected]> | 2023-12-14 16:43:33 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-12-14 16:43:33 -0800 |
| commit | 2cca45221b7bd1dc3d65361a341a7310271a280d (patch) | |
| tree | 3b7b4ca1ce27ab1503de7a23ea7eb1dca31d1cc6 /src/lib/Tools | |
| parent | feat(sequelspy): display upcoming date (diff) | |
| download | due.moe-2cca45221b7bd1dc3d65361a341a7310271a280d.tar.xz due.moe-2cca45221b7bd1dc3d65361a341a7310271a280d.zip | |
feat(sequelspy): save options
Diffstat (limited to 'src/lib/Tools')
| -rw-r--r-- | src/lib/Tools/CharacterBirthdays.svelte | 11 | ||||
| -rw-r--r-- | src/lib/Tools/SequelSpy.svelte | 41 | ||||
| -rw-r--r-- | src/lib/Tools/tool.ts | 13 |
3 files changed, 44 insertions, 21 deletions
diff --git a/src/lib/Tools/CharacterBirthdays.svelte b/src/lib/Tools/CharacterBirthdays.svelte index 048a6078..936db0c3 100644 --- a/src/lib/Tools/CharacterBirthdays.svelte +++ b/src/lib/Tools/CharacterBirthdays.svelte @@ -4,7 +4,7 @@ import { ACDBBirthdays, type ACDBBirthday } from '$lib/Birthday/ACDB'; import { aniSearchBirthdays, type aniSearchBirthday } from '$lib/Birthday/aniSearch'; import Error from '$lib/Error.svelte'; - import { clearAllParameters } from './tool'; + import { clearAllParameters, parseOrDefault } from './tool'; interface Birthday { name: string; @@ -12,15 +12,10 @@ origin?: string; } - const parseOrDefault = (parameter: string, fallback: number): number => - browser && urlParameters?.size !== 0 - ? parseInt(urlParameters?.get(parameter) || '', 10) || fallback - : fallback; - const urlParameters = browser ? new URLSearchParams(window.location.search) : null; let date = new Date(); - let month = parseOrDefault('month', date.getMonth() + 1); - let day = parseOrDefault('day', date.getDate()); + let month = parseOrDefault(urlParameters, 'month', date.getMonth() + 1); + let day = parseOrDefault(urlParameters, 'day', date.getDate()); let anisearchBirthdays: Promise<aniSearchBirthday[]>; let acdbBirthdays: Promise<ACDBBirthday[]>; diff --git a/src/lib/Tools/SequelSpy.svelte b/src/lib/Tools/SequelSpy.svelte index 5386066c..5ef94c1f 100644 --- a/src/lib/Tools/SequelSpy.svelte +++ b/src/lib/Tools/SequelSpy.svelte @@ -3,27 +3,42 @@ import { prequels, type MediaPrequel } from '$lib/AniList/prequels'; import MediaTitle from '$lib/List/MediaTitleDisplay.svelte'; import { onMount } from 'svelte'; - import { clearAllParameters } from './tool'; + import { clearAllParameters, parseOrDefault } from './tool'; import { airingTime } from '$lib/Media/anime'; import type { Media } from '$lib/AniList/media'; + import { page } from '$app/stores'; + import { browser } from '$app/environment'; export let user: AniListAuthorisation; let currentPrequels: Promise<MediaPrequel[]> = Promise.resolve([]) as Promise<MediaPrequel[]>; - let year = new Date().getFullYear(); - let season = (() => { - if (new Date().getMonth() >= 0 && new Date().getMonth() <= 2) - return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; - else if (new Date().getMonth() >= 3 && new Date().getMonth() <= 5) - return 'SPRING' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; - else if (new Date().getMonth() >= 6 && new Date().getMonth() <= 8) - return 'SUMMER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; - else if (new Date().getMonth() >= 9 && new Date().getMonth() <= 11) - return 'FALL' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; - else return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; - })(); + const urlParameters = browser ? new URLSearchParams(window.location.search) : null; + let year = parseOrDefault(urlParameters, 'year', new Date().getFullYear()); + let season = parseOrDefault( + urlParameters, + 'season', + (() => { + if (new Date().getMonth() >= 0 && new Date().getMonth() <= 2) + return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; + else if (new Date().getMonth() >= 3 && new Date().getMonth() <= 5) + return 'SPRING' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; + else if (new Date().getMonth() >= 6 && new Date().getMonth() <= 8) + return 'SUMMER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; + else if (new Date().getMonth() >= 9 && new Date().getMonth() <= 11) + return 'FALL' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; + else return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; + })() + ); $: currentPrequels = prequels(user, year, season); + $: { + if (browser) { + $page.url.searchParams.set('year', year.toString()); + $page.url.searchParams.set('season', season.toString()); + clearAllParameters(['year', 'season']); + history.replaceState(null, '', `?${$page.url.searchParams.toString()}`); + } + } onMount(clearAllParameters); diff --git a/src/lib/Tools/tool.ts b/src/lib/Tools/tool.ts index 77d80418..fc50a715 100644 --- a/src/lib/Tools/tool.ts +++ b/src/lib/Tools/tool.ts @@ -12,3 +12,16 @@ export const clearAllParameters = (saved: string[] = []) => { history.replaceState(null, '', `?${value.url.searchParams.toString()}`); }); }; + +export const parseOrDefault = <T = string | number>( + urlParameters: URLSearchParams | null, + parameter: string, + fallback: T +): T => + typeof fallback === 'number' + ? ((browser && urlParameters?.size !== 0 + ? parseInt(urlParameters?.get(parameter) || '', 10) || fallback + : fallback) as T) + : ((browser && urlParameters?.size !== 0 + ? urlParameters?.get(parameter) || fallback + : fallback) as T); |