From: Adam Dullage Date: Mon, 29 Apr 2024 19:58:35 +0000 (+0100) Subject: Added note view, navbar styling & API improvements X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=32d93020b13f08c3b24371a181a64755d19e5e95;p=flatnotes.git Added note view, navbar styling & API improvements --- diff --git a/client/App.vue b/client/App.vue index 09d4e1e..4aeaec4 100644 --- a/client/App.vue +++ b/client/App.vue @@ -21,8 +21,8 @@ const route = useRoute(); onBeforeMount(() => { getConfig() - .then((response) => { - globalStore.authType = response.data.authType; + .then((data) => { + globalStore.authType = data.authType; }) .catch(function (error) { if (!error.handled) { diff --git a/client/api.js b/client/api.js index d56334d..4636dd4 100644 --- a/client/api.js +++ b/client/api.js @@ -1,5 +1,6 @@ import * as constants from "./constants.js"; +import { Note } from "./classes.js"; import axios from "axios"; import { getStoredToken } from "./tokenStorage.js"; import router from "./router.js"; @@ -40,24 +41,41 @@ api.interceptors.response.use( }, ); -export function getConfig() { - return api.get("/api/config"); +export async function getConfig() { + try { + const response = await api.get("/api/config"); + return response.data; + } catch (error) { + return Promise.reject(error); + } } -export function getToken(username, password, totp) { - return api.post("/api/token", { - username: username, - password: totp ? password + totp : password, - }); +export async function getToken(username, password, totp) { + try { + const response = await api.post("/api/token", { + username: username, + password: totp ? password + totp : password, + }); + return response.data.access_token; + } catch (response) { + return Promise.reject(response); + } } -export function getNotes(term, sort, order, limit) { - return api.get("/api/search", { - params: { - term: term, - sort: sort, - order: order, - limit: limit, - }, - }); +export async function getNotes(term, sort, order, limit) { + try { + const response = await api.get("/api/search", { + params: { + term: term, + sort: sort, + order: order, + limit: limit, + }, + }); + return response.data.map( + (note) => new Note(note.title, note.lastModified, note.content), + ); + } catch (response) { + return Promise.reject(response); + } } diff --git a/client/classes.js b/client/classes.js new file mode 100644 index 0000000..84f7679 --- /dev/null +++ b/client/classes.js @@ -0,0 +1,49 @@ +import router from "./router.js"; + +class Note { + constructor(title, lastModified, content) { + this.title = title; + this.lastModified = lastModified; + this.content = content; + } + + get href() { + return `${router.resolve({ name: "note", params: { title: this.title } }).href}`; + } + + get lastModifiedAsDate() { + return new Date(this.lastModified * 1000); + } + + get lastModifiedAsString() { + return this.lastModifiedAsDate.toLocaleString(); + } +} + +class SearchResult extends Note { + constructor(searchResult) { + super(searchResult.title, searchResult.lastModified); + this.score = searchResult.score; + this.titleHighlights = searchResult.titleHighlights; + this.contentHighlights = searchResult.contentHighlights; + this.tagMatches = searchResult.tagMatches; + } + + get titleHighlightsOrTitle() { + return this.titleHighlights ? this.titleHighlights : this.title; + } + + get includesHighlights() { + if ( + this.titleHighlights || + this.contentHighlights || + (this.tagMatches != null && this.tagMatches.length) + ) { + return true; + } else { + return false; + } + } +} + +export { Note, SearchResult }; diff --git a/client/partials/NavBar.vue b/client/partials/NavBar.vue index 778a7d3..f7a5515 100644 --- a/client/partials/NavBar.vue +++ b/client/partials/NavBar.vue @@ -1,9 +1,9 @@