feat(frontend): member fields and flags editors, fix user fields editor

This commit is contained in:
sam 2024-12-09 16:41:54 +01:00
parent 2a0df335bc
commit b0a286dd9f
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
4 changed files with 68 additions and 1 deletions

View file

@ -0,0 +1,32 @@
<script lang="ts">
import { apiRequest } from "$api";
import ApiError, { type RawApiError } from "$api/error";
import { mergePreferences, type User } from "$api/models/user";
import FieldsEditor from "$components/editor/FieldsEditor.svelte";
import log from "$lib/log";
import type { PageData } from "./$types";
type Props = { data: PageData };
let { data }: Props = $props();
let fields = $state(data.member.fields);
let ok: { ok: boolean; error: RawApiError | null } | null = $state(null);
let allPreferences = $derived(mergePreferences(data.user.custom_preferences));
const update = async () => {
try {
const resp = await apiRequest<User>("PATCH", `/users/@me/members/${data.member.id}`, {
body: { fields },
token: data.token,
});
fields = resp.fields;
ok = { ok: true, error: null };
} catch (e) {
ok = { ok: false, error: null };
log.error("Could not update fields:", e);
if (e instanceof ApiError) ok.error = e.obj;
}
};
</script>
<FieldsEditor bind:fields {ok} {allPreferences} {update} />

View file

@ -0,0 +1,7 @@
import { apiRequest } from "$api";
import type { PrideFlag } from "$api/models/user";
export const load = async ({ fetch, cookies }) => {
const flags = await apiRequest<PrideFlag[]>("GET", "/users/@me/flags", { fetch, cookies });
return { flags };
};

View file

@ -0,0 +1,28 @@
<script lang="ts">
import { fastRequest } from "$api";
import type { RawApiError } from "$api/error";
import ApiError from "$api/error";
import ProfileFlagsEditor from "$components/editor/ProfileFlagsEditor.svelte";
import log from "$lib/log";
import type { PageData } from "./$types";
type Props = { data: PageData };
let { data }: Props = $props();
let form: { ok: boolean; error: RawApiError | null } | null = $state(null);
const save = async (flags: string[]) => {
try {
await fastRequest("PATCH", `/users/@me/members/${data.member.id}`, {
body: { flags },
token: data.token,
});
form = { ok: true, error: null };
} catch (e) {
log.error("Could not update profile flags for member %s:", data.member.id, e);
if (e instanceof ApiError) form = { ok: false, error: e.obj };
}
};
</script>
<ProfileFlagsEditor profileFlags={data.member.flags} allFlags={data.flags} {save} {form} />

View file

@ -29,4 +29,4 @@
};
</script>
<FieldsEditor {fields} {ok} {allPreferences} {update} />
<FieldsEditor bind:fields {ok} {allPreferences} {update} />