Skip to content

Commit

Permalink
feat: add persistent presence
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Oct 5, 2024
1 parent cf350fe commit a7b483a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export const admin = (() => {
storage('config').clear();
storage('comment').clear();
storage('session').clear();
storage('information').clear();
}

if (!session.isAdmin() || (JSON.parse(atob((session.getToken() ?? '.').split('.')[1])).exp ?? 0) < ((new Date()).getTime() / 1000)) {
Expand Down
11 changes: 6 additions & 5 deletions js/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,19 @@ export const comment = (() => {
}

const btn = util.disableButton(button);
const isPresence = presence ? presence.value === "1" : true;

if (!session.isAdmin()) {
storage('information').set('name', nameValue);

if (!id) {
storage('information').set('presence', isPresence);
}
}

const response = await request(HTTP_POST, '/api/comment')
.token(session.getToken())
.body(dto.postCommentRequest(id, nameValue, presence ? presence.value === "1" : true, form.value))
.body(dto.postCommentRequest(id, nameValue, isPresence, form.value))
.send(dto.postCommentResponse)
.then((res) => res, () => null);

Expand All @@ -210,10 +215,6 @@ export const comment = (() => {
owns.set(response.data.uuid, response.data.own);
form.value = null;

if (presence) {
presence.value = "0";
}

if (!id) {
const newPage = await pagination.reset();
if (newPage) {
Expand Down
11 changes: 9 additions & 2 deletions js/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { confetti } from './confetti.js';

export const guest = (() => {

const information = storage('information');

const countDownDate = () => {
const until = document.getElementById('count-down')?.getAttribute('data-time')?.replace(' ', 'T');
if (!until) {
Expand Down Expand Up @@ -81,7 +83,7 @@ export const guest = (() => {

const form = document.getElementById('form-name');
if (form) {
form.value = storage('information').get('name') ?? name;
form.value = information.get('name') ?? name;
}

util.opacity('loading', 0.025);
Expand Down Expand Up @@ -118,8 +120,13 @@ export const guest = (() => {
storage('tracker').clear();
}

const presence = document.getElementById('form-presence');
if (presence && information.get('presence') !== undefined) {
presence.value = information.get('presence') ? "1" : "2";
}

const info = document.getElementById('information');
if (info && storage('information').get('info')) {
if (info && information.get('info')) {
info.remove();
}

Expand Down
2 changes: 1 addition & 1 deletion js/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const pagination = (() => {
};

const reset = async () => {
if (pageNow == 0) {
if (pageNow === 0) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const storage = (table) => {
localStorage.setItem(table, JSON.stringify(storage));
};

const has = (key) => Object.keys(get()).includes(String(key));

const unset = (key) => {
if (!has(key)) {
return;
Expand All @@ -21,8 +23,6 @@ export const storage = (table) => {
localStorage.setItem(table, JSON.stringify(storage));
};

const has = (key) => Object.keys(get()).includes(String(key));

const clear = () => localStorage.setItem(table, JSON.stringify({}));

if (!localStorage.getItem(table)) {
Expand Down
5 changes: 3 additions & 2 deletions js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ export const theme = (() => {
}
}

const toggle = document.getElementById('darkMode');

if (isDarkMode()) {
onDark();
document.documentElement.setAttribute(THEME_BS_DATA, THEME_DARK);
const toggle = document.getElementById('darkMode');
theme.set('active', THEME_DARK);
if (toggle) {
toggle.checked = true;
}
} else {
onLight();
document.documentElement.setAttribute(THEME_BS_DATA, THEME_LIGHT);
theme.set('active', THEME_LIGHT);
const toggle = document.getElementById('darkMode');
if (toggle) {
toggle.checked = false;
}
Expand Down

0 comments on commit a7b483a

Please sign in to comment.