️ (user settings) cache user settings

This commit is contained in:
Julien Calixte
2023-09-11 23:02:35 +02:00
parent 4adaad8683
commit 1d21ac50b2
4 changed files with 36 additions and 10 deletions

View File

@@ -4,5 +4,6 @@ export enum DataType {
Note = 'Note', Note = 'Note',
BacklinkNote = 'BacklinkNote', BacklinkNote = 'BacklinkNote',
RepetitionCard = 'RepetitionCard', RepetitionCard = 'RepetitionCard',
History = 'History' History = 'History',
UserSettings = 'UserSettings'
} }

View File

@@ -50,6 +50,7 @@ class Data {
return result?.ok ?? false return result?.ok ?? false
} }
} }
const result = await this.locale?.put(model) const result = await this.locale?.put(model)
return result?.ok ?? false return result?.ok ?? false
} catch (error) { } catch (error) {

View File

@@ -1,4 +1,7 @@
export interface UserSettings { import { DataType } from '@/data/DataType.enum'
import { Model } from '@/data/models/Model'
export interface UserSettings extends Model<DataType.UserSettings> {
fontFamily?: fontFamily?:
| 'Courgette' | 'Courgette'
| 'IBM Plex Serif' | 'IBM Plex Serif'

View File

@@ -1,5 +1,7 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum'
import { RepoFile } from '@/modules/repo/interfaces/RepoFile' import { RepoFile } from '@/modules/repo/interfaces/RepoFile'
import { UserSettings } from '@/modules/repo/interfaces/UserSettings' import { UserSettings } from '@/modules/repo/interfaces/UserSettings'
import { import {
@@ -32,10 +34,10 @@ export const useUserRepoStore = defineStore({
needToLogin: false needToLogin: false
}), }),
actions: { actions: {
async setUserRepo(newUser: string, newRepo: string) { async setUserRepo(user: string, repo: string) {
this.isReadmeOffline = true this.isReadmeOffline = true
this.user = newUser this.user = user
this.repo = newRepo this.repo = repo
try { try {
await refreshToken() await refreshToken()
@@ -43,18 +45,37 @@ export const useUserRepoStore = defineStore({
console.warn('impossible to refresh token') console.warn('impossible to refresh token')
} }
getFiles(newUser, newRepo) const userSettingsId = `UserSetting-${user}-${repo}`
const cachedUserSettings = await data.get<
DataType.UserSettings,
UserSettings
>(userSettingsId)
if (cachedUserSettings) {
this.userSettings = cachedUserSettings
}
getFiles(user, repo)
.then((files) => { .then((files) => {
this.files = files this.files = files
return getUserSettingsContent(newUser, newRepo, files) return getUserSettingsContent(user, repo, files)
}) })
.then((userSettings) => (this.userSettings = userSettings)) .then((userSettings) => {
this.userSettings = userSettings
getCachedMainReadme(newUser, newRepo) if (userSettings) {
data.update<DataType.UserSettings, UserSettings>({
...userSettings,
_id: userSettingsId
})
}
})
getCachedMainReadme(user, repo)
.then((readme) => { .then((readme) => {
this.readme = readme this.readme = readme
}) })
.then(() => getMainReadme(newUser, newRepo)) .then(() => getMainReadme(user, repo))
.then((readme) => { .then((readme) => {
this.readme = readme this.readme = readme