Files
varo-website/src/app/admin/reports/reports.ts
bytedream 2e713273ca
All checks were successful
deploy / build-and-deploy (/testvaro, /opt/website-test, website-test) (push) Successful in 14s
deploy / build-and-deploy (/varo, /opt/website, website) (push) Successful in 13s
update reports
2025-06-10 00:08:00 +02:00

85 lines
2.2 KiB
TypeScript

import { type ActionReturnType, actions } from 'astro:actions';
import { writable } from 'svelte/store';
import { actionErrorPopup } from '@util/action.ts';
// types
export type Reports = Exclude<ActionReturnType<typeof actions.report.reports>['data'], undefined>['reports'];
export type Report = Reports[0];
export type ReportStatus = Exclude<
Exclude<ActionReturnType<typeof actions.report.reportStatus>['data'], undefined>['reportStatus'],
null
> & { strikeReasonId: number | null };
export type StrikeReasons = Exclude<
ActionReturnType<typeof actions.report.strikeReasons>['data'],
undefined
>['strikeReasons'];
// state
export const reports = writable<Reports>([]);
// actions
export async function fetchReports(reporterUsername: string | null, reportedUsername: string | null) {
const { data, error } = await actions.report.reports({ reporter: reporterUsername, reported: reportedUsername });
if (error) {
actionErrorPopup(error);
return;
}
reports.set(data.reports);
}
export async function addReport(report: Report) {
const { data, error } = await actions.report.addReport({
reason: report.reason,
body: report.body,
createdAt: report.createdAt,
reporter: report.reporter.id,
reported: report.reported?.id ?? null
});
if (error) {
actionErrorPopup(error);
return;
}
reports.update((old) => {
old.push(Object.assign(report, { id: data.id, status: null }));
return old;
});
}
export async function getReportStatus(report: Report) {
const { data, error } = await actions.report.reportStatus({ reportId: report.id });
if (error) {
actionErrorPopup(error);
return;
}
return data.reportStatus;
}
export async function editReportStatus(report: Report, reportStatus: ReportStatus) {
const { error } = await actions.report.editReportStatus({
reportId: report.id,
status: reportStatus.status,
notice: reportStatus.notice,
statement: reportStatus.statement,
strikeReasonId: reportStatus.strikeReasonId
});
if (error) {
actionErrorPopup(error);
}
}
export async function getStrikeReasons() {
const { data, error } = await actions.report.strikeReasons();
if (error) {
actionErrorPopup(error);
return;
}
return data.strikeReasons;
}