aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Tools/BirthdaysTemplate.svelte
blob: 5f476275d3c3c70e967bf88ba2bc9ce6dc689274 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<script lang="ts">
  import { browser } from '$app/environment';
  import { page } from '$app/stores';
  import { onMount } from 'svelte';
  import { clearAllParameters, parseOrDefault } from '../Utility/parameters';
  import Message from '$lib/Loading/Message.svelte';
  import locale from '$stores/locale';
  import Error from '$lib/Error/RateLimited.svelte';
  import Skeleton from '$lib/Loading/Skeleton.svelte';

  export let remoteURL: string;

  const urlParameters = browser ? new URLSearchParams(window.location.search) : null;
  let date = new Date();
  let month = parseOrDefault(urlParameters, 'month', date.getMonth() + 1);
  let day = parseOrDefault(urlParameters, 'day', date.getDate());
  const remoteBirthdays = fetch(remoteURL);

  $: {
    month = Math.min(month, 12);
    month = Math.max(month, 1);
    day = Math.min(day, new Date(2024, month, 0).getDate());
    day = Math.max(day, 1);

    if (browser) {
      $page.url.searchParams.set('month', month.toString());
      $page.url.searchParams.set('day', day.toString());
      clearAllParameters(['month', 'day']);
      history.replaceState(null, '', `?${$page.url.searchParams.toString()}`);
    }
  }

  onMount(() => clearAllParameters(['month', 'day']));
</script>

{#await remoteBirthdays}
  <Message message="Loading birthdays ... 33%" />

  <Skeleton grid={true} count={100} width="150px" height="170px" />
{:then birthdaysResponse}
  {#await birthdaysResponse.json()}
    <Message message="Loading birthdays ... 66%" />

    <Skeleton grid={true} count={100} width="150px" height="170px" />
  {:then birthdays}
    {@const todaysBirthdays = birthdays.filter(
      (birthday: { month: number; day: number }) => birthday.month === month && birthday.day === day
    )}

    <p>
      <select bind:value={month}>
        {#each Array.from({ length: 12 }, (_, i) => i + 1) as month}
          <option value={month}>
            {new Date(0, month - 1).toLocaleString('default', { month: 'long' })}
          </option>
        {/each}
      </select>

      <select bind:value={day}>
        {#each Array.from({ length: new Date(2024, month, 0).getDate() }, (_, i) => i + 1) as day}
          <option value={day}>{day}</option>
        {/each}
      </select>
    </p>

    {#if todaysBirthdays.length === 0}
      <Message
        message={`No birthdays for ${$locale().dayFormatter(
          new Date(new Date().getFullYear(), month - 1, day)
        )}.`}
        fullscreen={false}
        loader="ripple"
      />
    {:else}
      <div class="characters">
        {#each todaysBirthdays as birthday}
          <div class="card card-small">
            <a
              href={`https://anilist.co/search/characters?search=${encodeURIComponent(
                birthday.name
              ).replace(/%20/g, '+')}`}
              target="_blank"
            >
              {birthday.name}
              <img
                src={birthday.pictureURL}
                alt="Character"
                class="character-image"
                loading="lazy"
                decoding="async"
              />
            </a>
          </div>
        {/each}
      </div>
    {/if}
  {:catch}
    <Error type="Character" card />
  {/await}
{:catch}
  <Error type="Character" card />
{/await}

<style lang="scss">
  .characters {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr));
    gap: 1rem;
    grid-row-gap: 1rem;
    align-items: start;

    img {
      width: 100%;
      height: auto;
      aspect-ratio: 300 / 413;
      object-fit: cover;
      object-position: top;
      border-radius: 8px;
      margin-top: 0.5rem;
      box-shadow: 0 4px 30px var(--base01);
    }
  }
</style>