refactor(dashboard): use svelte context for lists

This commit is contained in:
Jake Fulmine 2023-06-03 12:27:58 +02:00
parent f2e160c526
commit a48f2b40c4
23 changed files with 598 additions and 433 deletions

View file

@ -1,14 +1,17 @@
<script lang="ts">
import { tick, createEventDispatcher } from "svelte";
import { tick, getContext } from "svelte";
import { ModalBody, ModalHeader, Col, Row, Input, Label, ModalFooter, Button, Spinner, Alert } from "sveltestrap";
import type { Group, GroupPrivacy } from '../../api/types';
import api from '../../api';
import type { Writable } from "svelte/store";
export let privacyOpen: boolean;
export let group: Group;
const togglePrivacyModal = () => (privacyOpen = !privacyOpen);
$: groups = getContext<Writable<Group[]>>("groups")
let err: string;
let loading = false;
let success = false;
@ -18,12 +21,6 @@
Object.keys(privacy).forEach(x => privacy[x] = target.value);
}
const dispatch = createEventDispatcher();
function update(group) {
dispatch('update', group);
}
// I can't use the hacked together Required<T> type from the bulk privacy here
// that breaks updating the displayed privacy after submitting
// but there's not really any way for any privacy fields here to be missing
@ -43,7 +40,15 @@
const data: Group = {privacy: privacy};
try {
let res = await api().groups(group.id).patch({data});
group = {...group, ...res};
const newgroup = {...group, ...res};
const newList = $groups.map((g: Group) => {
if (group.uuid === g.uuid) {
g = newgroup
}
return g
})
groups.set(newList);
success = true;
} catch (error) {
console.log(error);