From fe9b769831200ed0192b5a082c445583cc4d1e28 Mon Sep 17 00:00:00 2001
From: Trirst <abeces968@gmail.com>
Date: Sat, 16 Oct 2021 23:06:39 +0700
Subject: [PATCH] Tag creation

---
 src/App.svelte         |  1 +
 src/routes.js          |  2 ++
 src/routes/Tag.svelte  | 66 ++++++++++++++++++++++++++++++++++++++++++
 src/static/js/paths.js |  2 +-
 tailwind.config.cjs    |  2 +-
 5 files changed, 71 insertions(+), 2 deletions(-)
 create mode 100644 src/routes/Tag.svelte

diff --git a/src/App.svelte b/src/App.svelte
index 65306c9..b7c7266 100644
--- a/src/App.svelte
+++ b/src/App.svelte
@@ -8,6 +8,7 @@
 <div class="flex flex-row space-x-4 m-1.5">
   <a href="#/" class="">Home</a>
   <a href="#/upload">Upload</a>
+  <a href="#/tag">Tag</a>
   <span>:|:</span>
   <UserAuth />
 </div>
diff --git a/src/routes.js b/src/routes.js
index 73313cc..65ea066 100644
--- a/src/routes.js
+++ b/src/routes.js
@@ -1,9 +1,11 @@
 import Home from '@/routes/Home.svelte';
 import Upload from '@/routes/Upload.svelte';
 import ImagePost from '@/routes/ImagePost.svelte';
+import Tag from '@/routes/Tag.svelte';
 
 export default {
     '/': Home,
     '/upload': Upload,
     '/image/:snowflake': ImagePost,
+    '/tag': Tag,
 }
\ No newline at end of file
diff --git a/src/routes/Tag.svelte b/src/routes/Tag.svelte
new file mode 100644
index 0000000..f3f64ee
--- /dev/null
+++ b/src/routes/Tag.svelte
@@ -0,0 +1,66 @@
+<script>
+    import { fetchAPI } from "@/static/js/helper";
+    import { paths } from "@/static/js/paths";
+    import { user } from "@/stores";
+
+    const tagTypes = [
+        "artist",
+        "character",
+        "copyright",
+        "generic",
+        "group",
+        "meta",
+    ];
+    let tag = "";
+    let tagType = tagTypes[0];
+    $: validTag = checkTag(tag);
+
+    function putTag() {
+        const options = {
+            method: "PUT",
+            headers: {
+                secret: $user.secret,
+            },
+        };
+        return fetchAPI(paths.TagField(tag), options);
+    }
+    function patchTagType() {
+        const options = {
+            method: "PATCH",
+            headers: {
+                "Content-Type": "application/json",
+                secret: $user.secret,
+            },
+            body: JSON.stringify({ type: tagType }),
+        };
+        return fetchAPI(paths.TagInfo(tag), options);
+    }
+    function addTag() {
+        putTag()
+            .then(() => patchTagType())
+            .finally(() => {
+                tag = "";
+                tagType = tagTypes[0];
+            });
+    }
+    function checkTag(tag) {
+        const regex = /^[a-z0-9()_-]*$/g;
+        return !tag.match(regex) || tag.length > 128;
+    }
+</script>
+
+<div class="space-x-4 m-2">
+    <input type="text" placeholder="Tag name" bind:value={tag} />
+    <select bind:value={tagType}>
+        <option disabled>Tag type</option>
+        {#each tagTypes as type}
+            <option value={type}>{type}</option>
+        {/each}
+    </select>
+    <button on:click={addTag} disabled={validTag} class="disabled:opacity-50"
+        >Add</button
+    >
+</div>
+{#if validTag}
+    <div>No space or capital letter in tag name allowed.</div>
+{/if}
diff --git a/src/static/js/paths.js b/src/static/js/paths.js
index 0eb243e..c202107 100644
--- a/src/static/js/paths.js
+++ b/src/static/js/paths.js
@@ -12,7 +12,7 @@ let Base = "/api",
     ImageTagField = (flake, tag) => { return `${ImageTag(flake)}/${tag}` },
     Tag = Base + "/tag",
     TagField = (tag) => { return `${Tag}/${tag}` },
-    TagInfo = TagField + "/info",
+    TagInfo = (tag) => `${TagField(tag)}/info`,
     TagPage = TagField + "/page",
     TagPageField = (entry) => { return `${TagPage}/${entry}` },
     TagPageImage = TagPageField + "/image",
diff --git a/tailwind.config.cjs b/tailwind.config.cjs
index 1d618ab..666a77d 100644
--- a/tailwind.config.cjs
+++ b/tailwind.config.cjs
@@ -1,5 +1,5 @@
 module.exports = {
-  // mode: 'jit',
+  mode: 'jit',
   purge: ['./index.html', './src/**/*.{svelte,js,ts}'],
   darkMode: false, // or 'media' or 'class',
   theme: {
-- 
GitLab