2021-12-12 07:46:57 +01:00
< script lang = "ts" >
import { Row , Col , Input , Button , Label , Alert } from 'sveltestrap';
import autosize from 'svelte-autosize';
2021-12-30 10:17:27 +01:00
// import moment from 'moment-timezone';
2021-12-12 07:46:57 +01:00
import { currentUser } from '../../stores';
2022-05-09 15:00:37 -04:00
const descriptions: string[] = JSON.parse(localStorage.getItem("pk-config"))?.description_templates;
2022-02-01 15:24:45 -05:00
import { System } from '../../api/types';
import api from '../../api';
2021-12-12 07:46:57 +01:00
export let editMode: boolean;
2022-02-01 15:24:45 -05:00
export let user: System;
2021-12-12 07:46:57 +01:00
export let loading: boolean;
let err: string[] = [];
2022-02-01 15:24:45 -05:00
let input: System = {... user } ;
2021-12-12 07:46:57 +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);
}
}
2021-12-30 10:17:27 +01:00
/* if (data.timezone && !moment.tz.zone(data.timezone)) {
2021-12-12 07:46:57 +01:00
err.push(`"${ data . timezone } " is not a valid timezone, check out < a target = "_blank" style = "color: var(--bs-body-color);" href = "https://xske.github.io/tz/" > this site</ a > to see your current timezone!`);
2021-12-30 10:17:27 +01:00
} */
2021-12-12 07:46:57 +01:00
err = err;
if (err.length > 0) return;
loading = true;
try {
2022-02-01 15:24:45 -05:00
let res = await api().systems("@me").patch({ data } );
2021-12-12 07:46:57 +01:00
user = res;
currentUser.update(() => res);
err = [];
editMode = false;
loading = false;
} catch (error) {
console.log(error);
err.push(error.message);
err = err;
loading = false;
}
}
< / 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 = { user . name } aria-label="system name " />
2021-12-12 07:46:57 +01:00
< / Col >
< Col xs = { 12 } lg= { 4 } class = "mb-2" >
< Label > Tag:< / Label >
2022-05-20 13:39:30 +02:00
< Input bind:value = { input . tag } maxlength= { 100 } type = "text" placeholder = { user . tag } aria-label="system tag " />
2021-12-12 07:46:57 +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 = { user . color } aria-label="system color " />
2021-12-12 07:46:57 +01:00
< / Col >
< Col xs = { 12 } lg= { 4 } class = "mb-2" >
< Label > Avatar url:< / Label >
2022-05-20 13:39:30 +02:00
< Input bind:value = { input . avatar_url } maxlength= { 256 } type = "url" placeholder = { user . avatar_url } aria-label="system avatar url " />
2021-12-12 07:46:57 +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 = { user . banner } aria-label="system banner url " />
2021-12-12 07:46:57 +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 = { user . description } aria-label="system description " />
2021-12-12 07:46:57 +01:00
< / div >
2022-05-20 13:39:30 +02:00
< Button style = "flex: 0" color = "primary" on:click = { submit } aria-label="submit edits " > Submit</ Button > < Button style = "flex: 0" color = "light" on:click = {() => editMode = false } aria-label="cancel edits " > Back</ Button >