Skip to content

Commit

Permalink
Begyndt på global config
Browse files Browse the repository at this point in the history
  • Loading branch information
jona799t committed Dec 15, 2023
1 parent 999ff16 commit ab44283
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/lib/js/globalConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { get, post } from '$lib/js/http.js';
import { cookieInfo } from '$lib/js/LectioCookieHandler';
import { writable } from 'svelte/store';

let configNonce = Date.now().toString(36);
let betlecMappeId = null;
let betlecConfigId = null;

async function setConfig(value, overwrite = true) {
console.log('Opdaterer config filen');
const body = {
fileName: 'config.json',
folderId: betlecMappeId,
contentType: 'application/json',
content: JSON.stringify(value),
fileComment: 'Denne fil er autogenereret af BetterLectio. Hvis du fjerner den kan du risikere at alle dine indstillinger på BetterLectio fjernes.',
public: false
};
if (overwrite) body.documentId = betlecConfigId;
await post('/dokument_upload', JSON.stringify(body));
}

const writableConfig = () => {
const { subscribe, set, update } = writable(undefined);

return {
subscribe,
set: value => {
if (value) setConfig(value);
return set(value);
},
update
};
};
export const config = writableConfig();

async function getConfig() {
configNonce = await Date.now().toString(36);
if (!betlecMappeId) {
const user = await cookieInfo();
const dokumenter = await get(`/dokumenter?folderid=S${user.userId}__&nonce=${configNonce}`);
const betlecMappe = dokumenter.indhold.filter(dokument => dokument.navn === '.betterlectio');
if (betlecMappe.length > 0) {
betlecMappeId = betlecMappe[0].id;
} else {
console.log('Opretter mappen');
await post('/opret_mappe', JSON.stringify({
folderName: '.betterlectio',
folderComment: 'Denne mappe er autogenereret af BetterLectio. Hvis du fjerner den kan du risikere at alle dine indstillinger, themes og plugins på BetterLectio fjernes.',
folderId: `S${user.userId}__`
}));
getConfig();
return;
}
}

const betlecMappeDokumenter = await get(`/dokumenter?folderid=${betlecMappeId}&nonce=${configNonce}`);
const _betlecConfigId = betlecMappeDokumenter.indhold.filter(dokument => dokument.navn === 'config.json');
if (_betlecConfigId.length > 0) {
betlecConfigId = _betlecConfigId[0].id;
console.log(betlecConfigId);
} else {
console.log('Opretter config fil');
await setConfig({}, false);
getConfig();
return;
}

config.set(await get(`/dokument_hent?id=${betlecConfigId}&configNonce=${configNonce}`));
}
getConfig();
13 changes: 13 additions & 0 deletions src/routes/globalConfigTest/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { config } from '$lib/js/globalConfig.js';
function update() {
if ($config.test === undefined) $config.test = true;
else $config.test = !$config.test;
}
</script>

{#if $config}
<p>{JSON.stringify($config)}</p>
<button class="btn" on:click={update}>Update</button>
{/if}

0 comments on commit ab44283

Please sign in to comment.