2024-05-26 12:18:09 -07:00
|
|
|
"use server"
|
2024-04-23 01:53:37 -04:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
import { revalidatePath } from "next/cache"
|
2024-04-27 21:24:20 -04:00
|
|
|
|
2024-04-23 01:53:37 -04:00
|
|
|
export async function createSandbox(body: {
|
2024-05-26 12:18:09 -07:00
|
|
|
type: string
|
|
|
|
name: string
|
|
|
|
userId: string
|
|
|
|
visibility: string
|
2024-04-23 01:53:37 -04:00
|
|
|
}) {
|
2024-05-05 22:33:24 -07:00
|
|
|
const res = await fetch(
|
2024-05-26 18:37:36 -07:00
|
|
|
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox`,
|
2024-05-05 22:33:24 -07:00
|
|
|
{
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2024-05-26 18:37:36 -07:00
|
|
|
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
2024-05-05 22:33:24 -07:00
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
}
|
2024-05-26 12:18:09 -07:00
|
|
|
)
|
2024-04-27 16:22:35 -04:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
return await res.text()
|
2024-04-23 01:53:37 -04:00
|
|
|
}
|
2024-04-27 21:24:20 -04:00
|
|
|
|
|
|
|
export async function updateSandbox(body: {
|
2024-05-26 12:18:09 -07:00
|
|
|
id: string
|
|
|
|
name?: string
|
|
|
|
visibility?: "public" | "private"
|
2024-04-27 21:24:20 -04:00
|
|
|
}) {
|
2024-05-26 18:37:36 -07:00
|
|
|
await fetch(`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox`, {
|
2024-04-27 21:24:20 -04:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2024-05-26 18:37:36 -07:00
|
|
|
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
2024-04-27 21:24:20 -04:00
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
2024-05-26 12:18:09 -07:00
|
|
|
})
|
2024-04-27 21:24:20 -04:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
revalidatePath("/dashboard")
|
2024-04-27 21:24:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteSandbox(id: string) {
|
2024-05-26 18:37:36 -07:00
|
|
|
await fetch(
|
|
|
|
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox?id=${id}`,
|
|
|
|
{
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2024-04-27 21:24:20 -04:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
revalidatePath("/dashboard")
|
2024-04-27 21:24:20 -04:00
|
|
|
}
|
2024-05-01 01:29:16 -04:00
|
|
|
|
|
|
|
export async function shareSandbox(sandboxId: string, email: string) {
|
2024-05-05 22:33:24 -07:00
|
|
|
const res = await fetch(
|
2024-05-26 18:37:36 -07:00
|
|
|
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox/share`,
|
2024-05-05 22:33:24 -07:00
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2024-05-26 18:37:36 -07:00
|
|
|
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
2024-05-05 22:33:24 -07:00
|
|
|
},
|
|
|
|
body: JSON.stringify({ sandboxId, email }),
|
|
|
|
}
|
2024-05-26 12:18:09 -07:00
|
|
|
)
|
|
|
|
const text = await res.text()
|
2024-05-01 01:53:49 -04:00
|
|
|
|
|
|
|
if (res.status !== 200) {
|
2024-05-26 12:18:09 -07:00
|
|
|
return { success: false, message: text }
|
2024-05-01 01:29:16 -04:00
|
|
|
}
|
2024-05-01 01:53:49 -04:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
revalidatePath(`/code/${sandboxId}`)
|
|
|
|
return { success: true, message: "Shared successfully." }
|
2024-05-01 01:29:16 -04:00
|
|
|
}
|
2024-05-01 02:22:02 -04:00
|
|
|
|
|
|
|
export async function unshareSandbox(sandboxId: string, userId: string) {
|
2024-05-26 18:37:36 -07:00
|
|
|
await fetch(
|
|
|
|
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox/share`,
|
|
|
|
{
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ sandboxId, userId }),
|
|
|
|
}
|
|
|
|
)
|
2024-05-01 02:22:02 -04:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
revalidatePath(`/code/${sandboxId}`)
|
2024-05-17 23:18:15 -07:00
|
|
|
}
|