chore(client/ts): port services/protected_session

This commit is contained in:
Elian Doran 2024-12-19 22:25:48 +02:00
parent 214a71892d
commit f7dc9ea8e4
No known key found for this signature in database
2 changed files with 20 additions and 7 deletions

View File

@ -1,6 +1,7 @@
import server from './server.js'; import server from './server.js';
import protectedSessionHolder from './protected_session_holder.js'; import protectedSessionHolder from './protected_session_holder.js';
import toastService from "./toast.js"; import toastService from "./toast.js";
import type { ToastOptions } from "./toast.js";
import ws from "./ws.js"; import ws from "./ws.js";
import appContext from "../components/app_context.js"; import appContext from "../components/app_context.js";
import froca from "./froca.js"; import froca from "./froca.js";
@ -8,7 +9,19 @@ import utils from "./utils.js";
import options from "./options.js"; import options from "./options.js";
import { t } from './i18n.js'; import { t } from './i18n.js';
let protectedSessionDeferred = null; let protectedSessionDeferred: JQuery.Deferred<any, any, any> | null = null;
// TODO: Deduplicate with server when possible.
interface Response {
success: boolean;
}
interface Message {
taskId: string;
data: {
protect: boolean
}
}
async function leaveProtectedSession() { async function leaveProtectedSession() {
if (protectedSessionHolder.isProtectedSessionAvailable()) { if (protectedSessionHolder.isProtectedSessionAvailable()) {
@ -44,11 +57,11 @@ async function reloadData() {
await froca.loadInitialTree(); await froca.loadInitialTree();
// make sure that all notes used in the application are loaded, including the ones not shown in the tree // make sure that all notes used in the application are loaded, including the ones not shown in the tree
await froca.reloadNotes(allNoteIds, true); await froca.reloadNotes(allNoteIds);
} }
async function setupProtectedSession(password) { async function setupProtectedSession(password: string) {
const response = await server.post('login/protected', { password: password }); const response = await server.post<Response>('login/protected', { password: password });
if (!response.success) { if (!response.success) {
toastService.showError(t("protected_session.wrong_password"), 3000); toastService.showError(t("protected_session.wrong_password"), 3000);
@ -80,13 +93,13 @@ ws.subscribeToMessages(async message => {
} }
}); });
async function protectNote(noteId, protect, includingSubtree) { async function protectNote(noteId: string, protect: boolean, includingSubtree: boolean) {
await enterProtectedSession(); await enterProtectedSession();
await server.put(`notes/${noteId}/protect/${protect ? 1 : 0}?subtree=${includingSubtree ? 1 : 0}`); await server.put(`notes/${noteId}/protect/${protect ? 1 : 0}?subtree=${includingSubtree ? 1 : 0}`);
} }
function makeToast(message, title, text) { function makeToast(message: Message, title: string, text: string): ToastOptions {
return { return {
id: message.taskId, id: message.taskId,
title, title,

View File

@ -1,7 +1,7 @@
import ws from "./ws.js"; import ws from "./ws.js";
import utils from "./utils.js"; import utils from "./utils.js";
interface ToastOptions { export interface ToastOptions {
id?: string; id?: string;
icon: string; icon: string;
title: string; title: string;