Only admins can add or delete tags

This commit is contained in:
Alex Ling 2020-12-30 11:12:39 +00:00
parent 981a1f0226
commit 93f745aecb
3 changed files with 12 additions and 11 deletions

View File

@ -256,11 +256,13 @@ const bulkProgress = (action, el) => {
const tagsComponent = () => { const tagsComponent = () => {
return { return {
loading: true, loading: true,
isAdmin: false,
tags: [], tags: [],
newTag: '', newTag: '',
inputShown: false, inputShown: false,
tid: $('.upload-field').attr('data-title-id'), tid: $('.upload-field').attr('data-title-id'),
load() { load(admin) {
this.isAdmin = admin;
const url = `${base_url}api/tags/${this.tid}`; const url = `${base_url}api/tags/${this.tid}`;
this.request(url, 'GET', (data) => { this.request(url, 'GET', (data) => {
this.tags = data.tags; this.tags = data.tags;
@ -269,7 +271,7 @@ const tagsComponent = () => {
}, },
add() { add() {
const tag = this.newTag.trim(); const tag = this.newTag.trim();
const url = `${base_url}api/tags/${this.tid}/${encodeURIComponent(tag)}`; const url = `${base_url}api/admin/tags/${this.tid}/${encodeURIComponent(tag)}`;
this.request(url, 'PUT', () => { this.request(url, 'PUT', () => {
this.tags.push(tag); this.tags.push(tag);
this.newTag = ''; this.newTag = '';
@ -281,7 +283,7 @@ const tagsComponent = () => {
}, },
rm(event) { rm(event) {
const tag = event.currentTarget.id.split('-')[0]; const tag = event.currentTarget.id.split('-')[0];
const url = `${base_url}api/tags/${this.tid}/${encodeURIComponent(tag)}`; const url = `${base_url}api/admin/tags/${this.tid}/${encodeURIComponent(tag)}`;
this.request(url, 'DELETE', () => { this.request(url, 'DELETE', () => {
const idx = this.tags.indexOf(tag); const idx = this.tags.indexOf(tag);
if (idx < 0) return; if (idx < 0) return;
@ -295,9 +297,6 @@ const tagsComponent = () => {
$('#tag-input').get(0).focus(); $('#tag-input').get(0).focus();
}); });
} }
},
buildURL(tid, tag) {
}, },
request(url, method, cb) { request(url, method, cb) {
$.ajax({ $.ajax({

View File

@ -715,7 +715,8 @@ class APIRouter < Router
Koa.describe "Adds a new tag to a title" Koa.describe "Adds a new tag to a title"
Koa.path "tid", desc: "A title ID" Koa.path "tid", desc: "A title ID"
Koa.response 200, ref: "$result" Koa.response 200, ref: "$result"
put "/api/tags/:tid/:tag" do |env| Koa.tag "admin"
put "/api/admin/tags/:tid/:tag" do |env|
begin begin
title = (@context.library.get_title env.params.url["tid"]).not_nil! title = (@context.library.get_title env.params.url["tid"]).not_nil!
tag = env.params.url["tag"] tag = env.params.url["tag"]
@ -737,7 +738,8 @@ class APIRouter < Router
Koa.describe "Deletes a tag from a title" Koa.describe "Deletes a tag from a title"
Koa.path "tid", desc: "A title ID" Koa.path "tid", desc: "A title ID"
Koa.response 200, ref: "$result" Koa.response 200, ref: "$result"
delete "/api/tags/:tid/:tag" do |env| Koa.tag "admin"
delete "/api/admin/tags/:tid/:tag" do |env|
begin begin
title = (@context.library.get_title env.params.url["tid"]).not_nil! title = (@context.library.get_title env.params.url["tid"]).not_nil!
tag = env.params.url["tag"] tag = env.params.url["tag"]

View File

@ -1,12 +1,12 @@
<div class="uk-margin" x-data="tagsComponent()" x-cloak x-init="load()"> <div class="uk-margin" x-data="tagsComponent()" x-cloak x-init="load(<%= is_admin %>)">
<p class="uk-text-meta" @selectstart.prevent> <p class="uk-text-meta" @selectstart.prevent>
<span style="position:relative; bottom:3px; margin-right:5px;">Tags: </span> <span style="position:relative; bottom:3px; margin-right:5px;">Tags: </span>
<template x-for="tag in tags" :key="tag"> <template x-for="tag in tags" :key="tag">
<span class="uk-label uk-label-primary" style="padding:2px 5px; margin:0 5px 5px 5px; text-transform:none;"> <span class="uk-label uk-label-primary" style="padding:2px 5px; margin:0 5px 5px 5px; text-transform:none;">
<a class="uk-link-reset" @click="rm($event)" :id="`${tag}-rm`"><span uk-icon="close" style="margin-right: 5px; position: relative; bottom: 1.5px;"></span></a><a class="uk-link-reset" x-text="tag" :href="`<%= base_url %>tags/${encodeURIComponent(tag)}`"></a> <a class="uk-link-reset" x-show="isAdmin" @click="rm($event)" :id="`${tag}-rm`"><span uk-icon="close" style="margin-right: 5px; position: relative; bottom: 1.5px;"></span></a><a class="uk-link-reset" x-text="tag" :href="`<%= base_url %>tags/${encodeURIComponent(tag)}`"></a>
</span> </span>
</template> </template>
<a class="uk-link-reset" style="position:relative; bottom:3px;" :uk-icon="inputShown ? 'close' : 'plus'" @click="toggleInput($nextTick)"></a> <a class="uk-link-reset" style="position:relative; bottom:3px;" :uk-icon="inputShown ? 'close' : 'plus'" @click="toggleInput($nextTick)" x-show="isAdmin"></a>
</p> </p>
<input id="tag-input" class="uk-input" type="text" placeholder="Type in a new tag and hit enter" x-model="newTag" @keydown="keydown($event)" x-show="inputShown"> <input id="tag-input" class="uk-input" type="text" placeholder="Type in a new tag and hit enter" x-model="newTag" @keydown="keydown($event)" x-show="inputShown">
</div> </div>