aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Tools/Tracker/Tool.svelte
blob: a3705fbe060c7e8a0bfe55f0f73dafecfde680b1 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script lang="ts">
  import Spacer from '$lib/Layout/Spacer.svelte';
  import { v6 as uuidv6 } from 'uuid';
  import { database, type TrackerEntry } from '$lib/Database/IDB/tracker';
  import { onMount } from 'svelte';
  import Message from '$lib/Loading/Message.svelte';

  let url = '';
  let title = '';
  let progress = 0;
  let error = '';
  let masterList: TrackerEntry[] | null = null;
  let confirmDelete = 0;

  $: listAccess = masterList || [];

  onMount(async () => {
    masterList = await database.entries.toArray();
  });

  const adjustEntry = (id: string, to: number) => {
    const entry = listAccess.find((entry) => entry.id === id);

    if (!entry) return;

    entry.progress = Math.max(0, to);

    database.entries.update(id, { progress: entry.progress });

    masterList = listAccess.map((entry) =>
      entry.id === id ? { ...entry, progress: entry.progress } : entry
    );
  };

  const addEntry = async (url: string, title: string, progress: number) => {
    if (!url || !title) {
      error = 'URL and title are required fields';

      return;
    }

    if (listAccess.some((entry) => entry.url === url)) {
      error =
        'Entry with URL already exists: ' + listAccess.find((entry) => entry.url === url)?.title;

      return;
    }

    await database.entries.add({ url, title, progress, id: uuidv6() });

    masterList = await database.entries.toArray();
  };

  const deleteEntry = async (id: string) => {
    if (confirmDelete !== 1) {
      confirmDelete = 1;
      error = 'Click again to confirm deletion';

      return;
    }

    await database.entries.delete(id);

    masterList = await database.entries.toArray();
    confirmDelete = 0;
    error = '';
  };
</script>

<div class="card">
  {#if error}
    <p><b>Error</b>: {error}</p>
  {/if}

  <input type="url" placeholder="URL" bind:value={url} />
  <input type="text" placeholder="Title" bind:value={title} />
  <input type="number" placeholder="Progress (defaults to 0)" bind:value={progress} />
  <button class="button-lined" onclick={() => addEntry(url, title, progress)}> Add </button>

  <Spacer />

  {#if masterList === null}
    <Message message="Loading entries ..." />
  {:else}
    <ul>
      {#each listAccess.sort((a, b) => a.title.localeCompare(b.title)) as entry}
        <li>
          <div class="entry" id={entry.id}>
            <a href={entry.url} title={entry.id}>
              {entry.title}
            </a>

            <span class="opaque">|</span>

            <input
              type="number"
              value={entry.progress}
              size={3}
              onchange={(e) =>
                adjustEntry(
                  entry.id,
                  e.target
                    ? Number((e.target as HTMLInputElement).value) || entry.progress
                    : entry.progress
                )}
            />

            <span class="entry-adjust">
              <span class="opaque">|</span>
              <button
                class="button-square button-action"
                onclick={() => adjustEntry(entry.id, entry.progress - 1)}
                >-
              </button>
              <button
                class="button-square button-action"
                onclick={() => adjustEntry(entry.id, entry.progress + 1)}
              >
                +
              </button>
              <span class="opaque">|</span>
              <button onclick={() => deleteEntry(entry.id)}>Remove</button>
            </span>
          </div>
        </li>
      {/each}
    </ul>
  {/if}
</div>

<style>
  .entry {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    width: 100%;
  }

  .entry-adjust {
    opacity: 0;
    transition: opacity 0.2s;
  }

  .entry:hover .entry-adjust {
    opacity: 1;
    transition: opacity 0.2s;
  }
</style>