2021-12-19 09:53:13 +01:00
< script lang = "ts" >
2022-01-09 15:01:24 +01:00
import { Row , Col , Input , Button , Label , Alert , Spinner , Modal , ModalHeader , ModalBody } from 'sveltestrap';
2022-05-20 13:39:30 +02:00
import { createEventDispatcher , tick } from 'svelte';
2022-02-01 15:24:45 -05:00
import { Group } from '../../api/types';
import api from '../../api';
2021-12-19 09:53:13 +01:00
import autosize from 'svelte-autosize';
2022-05-09 15:00:37 -04:00
const descriptions: string[] = JSON.parse(localStorage.getItem("pk-config"))?.description_templates;
2021-12-30 10:22:08 +01:00
let loading: boolean = false;
2021-12-19 09:53:13 +01:00
export let group: Group;
export let editMode: boolean;
let err: string[] = [];
2022-02-01 15:24:45 -05:00
let input: Group = {... group } ;
2021-12-19 09:53:13 +01:00
2021-12-19 12:01:55 +01:00
const dispatch = createEventDispatcher();
function update() {
dispatch('update', group);
}
2022-01-09 15:01:24 +01:00
function deletion() {
dispatch('deletion', group.id);
}
2021-12-19 09:53:13 +01:00
async function submit() {
let data = input;
err = [];
if (data.color && !/^#?[A-Fa-f0-9]{ 6 } $/.test(input.color)) {
err.push(`"${ data . color } " is not a valid color, the color must be a 6-digit hex code. (example: #ff0000)`);
} else if (data.color) {
if (data.color.startsWith("#")) {
data.color = input.color.slice(1, input.color.length);
}
}
err = err;
if (err.length > 0) return;
loading = true;
try {
2022-02-01 15:24:45 -05:00
let res = await api().groups(group.id).patch({ data } );
2022-01-26 21:06:50 +01:00
group = {... group , ... res } ;
2021-12-19 09:53:13 +01:00
err = [];
2021-12-19 12:01:55 +01:00
update();
2021-12-19 09:53:13 +01:00
editMode = false;
loading = false;
} catch (error) {
console.log(error);
err.push(error.message);
err = err;
loading = false;
}
}
2022-01-09 15:01:24 +01:00
let deleteOpen: boolean = false;
const toggleDeleteModal = () => deleteOpen = !deleteOpen;
let deleteInput: string;
let deleteErr: string;
async function submitDelete() {
deleteErr = null;
if (!deleteInput) {
deleteErr = "Please type out the group ID.";
return;
}
if (deleteInput.trim().toLowerCase() !== group.id) {
deleteErr = "This group's ID does not match the provided ID.";
return;
}
loading = true;
try {
2022-02-01 15:24:45 -05:00
await api().groups(group.id).delete();
2022-01-09 15:01:24 +01:00
deleteErr = null;
toggleDeleteModal();
loading = false;
deletion();
} catch (error) {
console.log(error);
deleteErr = error.message;
loading = false;
}
}
2022-05-20 13:39:30 +02:00
async function focus(el) {
await tick();
el.focus();
}
2021-12-19 09:53:13 +01:00
< / script >
{ #each err as error }
< Alert color = "danger" > { @html error } </ Alert >
{ /each }
< Row >
< Col xs = { 12 } lg= { 4 } class = "mb-2" >
< Label > Name:< / Label >
2022-05-20 13:39:30 +02:00
< Input bind:value = { input . name } maxlength= { 100 } type = "text" placeholder = { group . name } aria-label="group name " />
2021-12-19 09:53:13 +01:00
< / Col >
< Col xs = { 12 } lg= { 4 } class = "mb-2" >
< Label > Display name:< / Label >
2022-05-20 13:39:30 +02:00
< textarea class = "form-control" style = "resize: none; height: 1em" bind:value = { input . display_name } maxlength= { 100 } type = "text" placeholder = { group . display_name } aria-label="group display name " />
2021-12-19 09:53:13 +01:00
< / Col >
< Col xs = { 12 } lg= { 4 } class = "mb-2" >
< Label > Color:< / Label >
2022-05-20 13:39:30 +02:00
< Input bind:value = { input . color } type="text" placeholder = { group . color } aria-label="group color " />
2021-12-19 09:53:13 +01:00
< / Col >
< Col xs = { 12 } lg= { 4 } class = "mb-2" >
2022-01-12 10:46:49 +01:00
< Label > Icon url:< / Label >
2022-05-20 13:39:30 +02:00
< Input bind:value = { input . icon } maxlength= { 256 } type = "url" placeholder = { group . icon } aria-label="group icon url " />
2021-12-19 09:53:13 +01:00
< / Col >
< Col xs = { 12 } lg= { 4 } class = "mb-2" >
< Label > Banner url:< / Label >
2022-05-20 13:39:30 +02:00
< Input bind:value = { input . banner } maxlength= { 256 } type = "url" placeholder = { group . banner } aria-label="group banner url " />
2021-12-19 09:53:13 +01:00
< / Col >
< / Row >
< div class = "my-2" >
< b > Description:< / b > < br / >
2022-05-09 15:00:37 -04:00
{ #if descriptions . length > 0 && descriptions [ 0 ]. trim () != "" }
2022-05-20 13:39:30 +02:00
< Button size = "sm" color = "primary" on:click = {() => input . description = descriptions [ 0 ]} aria-label="use template 1 " > Template 1</ Button >
2022-05-09 15:00:37 -04:00
{ /if }
{ #if descriptions . length > 1 && descriptions [ 1 ]. trim () != "" }
2022-05-20 13:39:30 +02:00
< Button size = "sm" color = "primary" on:click = {() => input . description = descriptions [ 1 ]} aria-label="use template 2 " > Template 2</ Button >
2022-05-09 15:00:37 -04:00
{ /if }
{ #if descriptions . length > 2 && descriptions [ 2 ]. trim () != "" }
2022-05-20 13:39:30 +02:00
< Button size = "sm" color = "primary" on:click = {() => input . description = descriptions [ 2 ]} aria-label="use template 3 " > Template 3</ Button >
2022-05-09 15:00:37 -04:00
{ /if }
< br >
2022-05-20 13:39:30 +02:00
< textarea class = "form-control" bind:value = { input . description } maxlength= { 1000 } use:autosize placeholder = { group . description } aria-label="group description " />
2021-12-19 09:53:13 +01:00
< / div >
2022-05-20 13:39:30 +02:00
{ #if ! loading } < Button style = "flex: 0" color = "primary" on:click = { submit } aria-label="submit edits " > Submit</ Button > < Button style = "flex: 0" color = "secondary" on:click = {() => editMode = false } aria-label="cancel edits " > Back</ Button >< Button style = "flex: 0; float: right;" color = "danger" on:click = { toggleDeleteModal } aria-label="delete group " > Delete</ Button >
{ : else } < Button style = "flex: 0" color = "primary" disabled aria-label = "submit edits" >< Spinner size = "sm" /></ Button > < Button style = "flex: 0" color = "secondary" disabled aria-label = "cancel edits" > Back</ Button >< Button style = "flex: 0; float: right;" color = "danger" disabled aria-label = "delete group" > Delete</ Button > { /if }
2022-01-09 15:01:24 +01:00
< Modal size = "lg" isOpen = { deleteOpen } toggle= { toggleDeleteModal } >
< ModalHeader toggle = { toggleDeleteModal } >
Delete member
< / ModalHeader >
< ModalBody >
{ #if deleteErr } < Alert color = "danger" > { deleteErr } </ Alert > { /if }
2022-04-28 20:29:13 +02:00
< Label > If you're sure you want to delete this group, type out the group ID (< code > { group . id } </ code > ) below.</ Label >
2022-05-20 13:39:30 +02:00
< input class = "mb-3 form-control" bind:value = { deleteInput } maxlength= { 7 } placeholder = { group . id } aria-label= { `type out the group id ${ group . id } to confirm deletion` } use:focus >
{ #if ! loading } < Button style = "flex 0" color = "danger" on:click = { submitDelete } aria-label="confirm delete " > Delete</ Button > < Button style = "flex: 0" color = "secondary" on:click = { toggleDeleteModal } aria-label="cancel delete " > Back</ Button >
{ : else } < Button style = "flex 0" color = "danger" disabled aria-label = "confirm delete" >< Spinner size = "sm" /></ Button > < Button style = "flex: 0" color = "secondary" disabled aria-label = "cancel delete" > Back</ Button >
2022-01-09 15:01:24 +01:00
{ /if }
< / ModalBody >
< / Modal >