forked from javayhu/free-directory-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-application.ts
39 lines (33 loc) · 1.2 KB
/
delete-application.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"use server";
import { auth } from "@/auth";
import { client } from "@/sanity/lib/client";
import { revalidatePath } from "next/cache";
export type deleteApplicationFormData = {
lang: string;
applicationId: string;
};
// https://nextjs.org/learn/dashboard-app/mutating-data
export async function deleteApplication(userId: string, data: deleteApplicationFormData) {
try {
const session = await auth();
if (!session?.user || session?.user.id !== userId) {
console.log("deleteApplication, unauthorized");
throw new Error("Unauthorized");
}
console.log("deleteApplication, username:", session?.user?.name);
// console.log("deleteApplication, data:", data);
const { lang, applicationId } = data;
console.log("deleteApplication, lang:", lang, " applicationId:", applicationId);
const res = await client.delete(applicationId);
if (!res) {
console.log("deleteApplication, fail");
return { status: "error" };
}
console.log("deleteApplication, success, res:", res);
revalidatePath(`/${lang}/dashboard/app`);
return { status: "success" };
} catch (error) {
console.log("deleteApplication, error", error);
return { status: "error" };
}
}