-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
54 lines (46 loc) · 1.38 KB
/
actions.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use server";
import { auth } from "./auth";
import { parseServerActionResponse } from "./lib/utils";
import slugify from 'slugify';
import { writeClient } from "./sanity/lib/write-client";
export const createPitch = async (state: any, form: FormData, pitch: string) => {
const session = await auth();
if (!session)
return parseServerActionResponse({
error: "Not signed in",
status: "ERROR"
});
const { title, description, category, link } = Object.fromEntries(
Array.from(form).filter(([key]) => key !== 'pitch' )
);
const slug = slugify(title as string, { lower: true, strict: true});
try {
const startup = {
title,
description,
category,
image: link,
slug: {
_type: slug,
current: slug,
},
author: {
_type: 'reference',
_ref: session?.id
},
pitch,
};
const result = await writeClient.create( { _type: "startup", ...startup });
return parseServerActionResponse({
...result,
error: '',
status: "SUCCESS"
})
} catch (error) {
console.log(error);
return parseServerActionResponse({
error: JSON.stringify(error),
status: "ERROR"
})
}
};