Skip to content

Commit

Permalink
Sets toast messages in server actions
Browse files Browse the repository at this point in the history
  • Loading branch information
allanlasser committed Nov 26, 2024
1 parent b03ca0a commit 7ce4d3d
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/routes/(app)/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { _ } from "svelte-i18n";
import { createFeedback, type Feedback } from "@/lib/api/feedback";
import type { Actions } from "./$types";
import { fail } from "@sveltejs/kit";
import { setFlash } from "sveltekit-flash-message/server";

export const actions = {
feedback: async ({ request, fetch }) => {
feedback: async ({ request, cookies, fetch }) => {
const data = await request.formData();
// POST form data to baserow
const feedback: Feedback = {
Expand All @@ -14,8 +15,16 @@ export const actions = {
};
try {
await createFeedback(feedback, fetch);
setFlash(
{
message: "Feedback recieved, thanks for using DocumentCloud!",
status: "success",
},
cookies,
);
return { success: true };
} catch (e) {
setFlash({ message: e.message, status: "error" }, cookies);
fail(500, { message: String(e) });
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/routes/(app)/add-ons/[owner]/[repo]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fail } from "@sveltejs/kit";
import { CSRF_COOKIE_NAME } from "@/config/config.js";

import { getAddon, buildPayload, dispatch } from "$lib/api/addons";
import { setFlash } from "sveltekit-flash-message/server";

export const actions = {
async dispatch({ cookies, fetch, request, params }) {
Expand Down Expand Up @@ -33,10 +34,13 @@ export const actions = {
const { data, error } = await dispatch(payload, csrf_token, fetch);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { ...error });
}

const type = payload.event ? "event" : "run";
const message = payload.event ? "Event scheduled" : "Run dispatched";
setFlash({ message, status: "success" }, cookies);
return {
type,
[type]: data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fail } from "@sveltejs/kit";
import { CSRF_COOKIE_NAME } from "@/config/config.js";

import { getEvent, buildPayload, update } from "$lib/api/addons";
import { setFlash } from "sveltekit-flash-message/server";

export const actions = {
async update({ cookies, fetch, params, request }) {
Expand Down Expand Up @@ -35,9 +36,11 @@ export const actions = {
const { data, error } = await update(id, payload, csrf_token, fetch);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { ...error });
}

setFlash({ message: "Event updated", status: "success" }, cookies);
return {
type: "event",
event: data,
Expand Down
22 changes: 22 additions & 0 deletions src/routes/(app)/documents/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fail } from "@sveltejs/kit";

import { CSRF_COOKIE_NAME } from "@/config/config.js";
import { destroy_many, edit_many, add_tags } from "$lib/api/documents";
import { setFlash } from "sveltekit-flash-message/server";

export function load({ cookies }) {
const csrf_token = cookies.get(CSRF_COOKIE_NAME);
Expand Down Expand Up @@ -51,9 +52,21 @@ export const actions = {
const errors = results.filter((r) => r.error);

if (errors.length) {
errors.forEach((r) => {
if (r.error) {
setFlash({ message: r.error.message, status: "error" }, cookies);
}
});
return fail(400, { errors });
}

setFlash(
{
message: `Data saved to ${results.filter((r) => !r.error).length} documents`,
status: "success",
},
cookies,
);
return {
success: true,
};
Expand All @@ -71,9 +84,12 @@ export const actions = {
const { error } = await destroy_many(ids, csrf_token, fetch);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { ...error });
}

const message = ids.length === 1 ? "Document deleted" : "Documents deleted";
setFlash({ message, status: "success" }, cookies);
return {
success: true,
count: ids.length,
Expand All @@ -100,9 +116,15 @@ export const actions = {
const { error } = await edit_many(docs, csrf_token, fetch);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { ...error });
}

const message =
ids.length === 1
? "Saved edits to one document"
: `Saved edits to ${ids.length} documents`;
setFlash({ message, status: "success" }, cookies);
return {
success: true,
count: ids.length,
Expand Down
37 changes: 28 additions & 9 deletions src/routes/(app)/documents/[id]-[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Actions } from "./$types";
import type { Access, Document, Note } from "$lib/api/types";

import { fail, redirect } from "@sveltejs/kit";
import { fail } from "@sveltejs/kit";
import { setFlash, redirect } from "sveltekit-flash-message/server";

import { CSRF_COOKIE_NAME } from "@/config/config.js";
import { destroy, edit, redact } from "$lib/api/documents";
import * as notes from "$lib/api/notes";
import { isErrorCode } from "$lib/utils/api";
import { setFlash } from "sveltekit-flash-message/server";

export function load({ cookies }) {
const csrf_token = cookies.get(CSRF_COOKIE_NAME);
Expand Down Expand Up @@ -61,10 +61,7 @@ export const actions = {
error: error.errors,
});
}
setFlash(
{ message: "Document metadata saved", status: "success" },
cookies,
);
setFlash({ message: "Data saved", status: "success" }, cookies);
return {
success: true,
document,
Expand All @@ -84,10 +81,16 @@ export const actions = {
const { error } = await destroy(id, csrf_token, fetch);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { message: error.message });
}

return redirect(302, "/documents/");
return redirect(
302,
"/documents/",
{ message: "Document deleted", status: "success" },
cookies,
);
},

async edit({ cookies, fetch, request, params }) {
Expand All @@ -98,20 +101,22 @@ export const actions = {
const form = await request.formData();
const { id } = params;

const update: Partial<Document> = Object.fromEntries(form);
const update: Partial<Document> = Object.fromEntries(form.entries());

// noindex is a boolean so needs special treatment
update.noindex = form.get("noindex") === "on";

const { data: document, error } = await edit(id, update, csrf_token, fetch);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, {
message: error.message,
errors: error.errors,
});
}

setFlash({ message: "Metadata saved", status: "success" }, cookies);
return {
success: true,
document,
Expand All @@ -132,14 +137,22 @@ export const actions = {

// probably the API is down
if (!resp) {
setFlash(
{ message: "Redactions failed to save.", status: "error" },
cookies,
);
return fail(500, { error: "Something went wrong." });
}

// something else broke
if (isErrorCode(resp.status)) {
setFlash(
{ message: "Redactions failed to save.", status: "error" },
cookies,
);
return fail(resp.status, await resp.json());
}

setFlash({ message: "Redactions saved", status: "success" }, cookies);
return {
success: true,
redactions: await resp.json(), // this should be the same as above
Expand Down Expand Up @@ -176,12 +189,14 @@ export const actions = {
);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, {
message: error.message,
errors: error.errors,
});
}

setFlash({ message: "Note created", status: "success" }, cookies);
return {
success: true,
note: created,
Expand Down Expand Up @@ -213,12 +228,14 @@ export const actions = {
);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, {
message: error.message,
errors: error.errors,
});
}

setFlash({ message: "Note updated", status: "success" }, cookies);
return {
success: true,
note: updated,
Expand All @@ -239,9 +256,11 @@ export const actions = {
const { error } = await notes.remove(params.id, note_id, csrf_token, fetch);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { message: error.message });
}

setFlash({ message: "Note deleted", status: "success" }, cookies);
return {
success: true,
};
Expand Down
3 changes: 3 additions & 0 deletions src/routes/(app)/projects/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fail } from "@sveltejs/kit";

import { CSRF_COOKIE_NAME } from "@/config/config.js";
import * as projects from "$lib/api/projects";
import { setFlash } from "sveltekit-flash-message/server";

export function load({ cookies }) {
const csrf_token = cookies.get(CSRF_COOKIE_NAME);
Expand All @@ -30,9 +31,11 @@ export const actions = {

try {
const created = await projects.create(project, csrf_token, fetch);
setFlash({ message: "Project created", status: "success" }, cookies);
return { success: true, project: created };
} catch (error) {
// todo: return better errors
setFlash({ message: error.message, status: "error" }, cookies);
return fail(400, { error });
}
},
Expand Down
15 changes: 15 additions & 0 deletions src/routes/(app)/projects/[id]-[slug]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ export const actions = {
);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { message: error.message });
}

setFlash({ message: "Edits saved", status: "success" }, cookies);
return { success: true, project: updated };
},

Expand All @@ -100,9 +102,11 @@ export const actions = {
);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { ...error });
}

setFlash({ message: "Invitation sent", status: "success" }, cookies);
return {
user,
};
Expand Down Expand Up @@ -137,9 +141,14 @@ export const actions = {
);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { ...error });
}

setFlash(
{ message: "Updated collaborator settings", status: "success" },
cookies,
);
return {
user: data,
};
Expand Down Expand Up @@ -171,7 +180,13 @@ export const actions = {
);

if (error) {
setFlash({ message: error.message, status: "error" }, cookies);
return fail(error.status, { ...error });
}

setFlash(
{ message: "Collaborator removed from project", status: "success" },
cookies,
);
},
} satisfies Actions;

0 comments on commit 7ce4d3d

Please sign in to comment.