️ (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',
BacklinkNote = 'BacklinkNote',
RepetitionCard = 'RepetitionCard',
History = 'History'
History = 'History',
UserSettings = 'UserSettings'
}

View File

@@ -50,6 +50,7 @@ class Data {
return result?.ok ?? false
}
}
const result = await this.locale?.put(model)
return result?.ok ?? false
} 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?:
| 'Courgette'
| 'IBM Plex Serif'

View File

@@ -1,5 +1,7 @@
import { defineStore } from 'pinia'
import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum'
import { RepoFile } from '@/modules/repo/interfaces/RepoFile'
import { UserSettings } from '@/modules/repo/interfaces/UserSettings'
import {
@@ -32,10 +34,10 @@ export const useUserRepoStore = defineStore({
needToLogin: false
}),
actions: {
async setUserRepo(newUser: string, newRepo: string) {
async setUserRepo(user: string, repo: string) {
this.isReadmeOffline = true
this.user = newUser
this.repo = newRepo
this.user = user
this.repo = repo
try {
await refreshToken()
@@ -43,18 +45,37 @@ export const useUserRepoStore = defineStore({
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) => {
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) => {
this.readme = readme
})
.then(() => getMainReadme(newUser, newRepo))
.then(() => getMainReadme(user, repo))
.then((readme) => {
this.readme = readme