Skip to content

Commit 2e354d3

Browse files
authored
Deleting endpoint working (#875)
1 parent dd879c8 commit 2e354d3

File tree

3 files changed

+88
-17
lines changed

3 files changed

+88
-17
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.environments/ConfigureEndpointSheet.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ export function ConfigureEndpointSheet({ slug, endpoint, onClose }: ConfigureEnd
4747
const refreshEndpointFetcher = useFetcher();
4848
const refreshingEndpoint = refreshEndpointFetcher.state !== "idle";
4949

50+
const deleteEndpointFetcher = useFetcher();
51+
const deletingEndpoint = deleteEndpointFetcher.state !== "idle";
52+
5053
const revalidator = useRevalidator();
5154
const events = useEventSource(endpointStreamingPath({ id: endpoint.environment.id }), {
5255
event: "message",
@@ -70,12 +73,30 @@ export function ConfigureEndpointSheet({ slug, endpoint, onClose }: ConfigureEnd
7073
>
7174
<SheetContent size="lg">
7275
<SheetHeader>
73-
<Header1>
74-
<div className="flex items-center gap-2">
75-
<EnvironmentLabel environment={{ type: endpoint.environment.type }} />
76-
<Header1>Configure endpoint</Header1>
77-
</div>
78-
</Header1>
76+
<div className="flex w-full items-center justify-between">
77+
<Header1>
78+
<div className="flex items-center gap-2">
79+
<EnvironmentLabel environment={{ type: endpoint.environment.type }} />
80+
<Header1>Configure endpoint</Header1>
81+
</div>
82+
</Header1>
83+
{endpoint.state === "configured" && (
84+
<deleteEndpointFetcher.Form
85+
method="post"
86+
action={`/resources/environments/${endpoint.environment.id}/endpoint/${endpoint.id}`}
87+
>
88+
<input type="hidden" name="action" value="delete" />
89+
<Button
90+
variant="danger/small"
91+
type="submit"
92+
disabled={deletingEndpoint}
93+
LeadingIcon={deletingEndpoint ? "spinner-white" : undefined}
94+
>
95+
{deletingEndpoint ? "Deleting" : "Delete"}
96+
</Button>
97+
</deleteEndpointFetcher.Form>
98+
)}
99+
</div>
79100
</SheetHeader>
80101
<SheetBody>
81102
<setEndpointUrlFetcher.Form
@@ -123,6 +144,7 @@ export function ConfigureEndpointSheet({ slug, endpoint, onClose }: ConfigureEnd
123144
method="post"
124145
action={`/resources/environments/${endpoint.environment.id}/endpoint/${endpoint.id}`}
125146
>
147+
<input type="hidden" name="action" value="refresh" />
126148
<Callout
127149
variant="info"
128150
icon={

apps/webapp/app/routes/resources.environments.$environmentParam.endpoint.$endpointParam.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
11
import { ActionFunctionArgs, json } from "@remix-run/server-runtime";
22
import { z } from "zod";
3+
import { DeleteEndpointIndexService } from "~/services/endpoints/deleteEndpointService";
34
import { IndexEndpointService } from "~/services/endpoints/indexEndpoint.server";
5+
import { requireUserId } from "~/services/session.server";
46
import { workerQueue } from "~/services/worker.server";
57

68
const ParamsSchema = z.object({
79
environmentParam: z.string(),
810
endpointParam: z.string(),
911
});
1012

11-
export async function action({ params }: ActionFunctionArgs) {
12-
const { endpointParam } = ParamsSchema.parse(params);
13+
const BodySchema = z.discriminatedUnion("action", [
14+
z.object({ action: z.literal("refresh") }),
15+
z.object({ action: z.literal("delete") }),
16+
]);
17+
18+
export async function action({ request, params }: ActionFunctionArgs) {
19+
const userId = await requireUserId(request);
20+
if (request.method !== "POST") {
21+
throw new Response(null, { status: 405 });
22+
}
1323

1424
try {
15-
const service = new IndexEndpointService();
16-
await service.call(endpointParam, "MANUAL");
25+
const { endpointParam } = ParamsSchema.parse(params);
26+
const form = await request.formData();
27+
const formObject = Object.fromEntries(form.entries());
28+
const { action } = BodySchema.parse(formObject);
29+
30+
switch (action) {
31+
case "refresh": {
32+
const service = new IndexEndpointService();
33+
await service.call(endpointParam, "MANUAL");
1734

18-
// Enqueue the endpoint to be probed in 10 seconds
19-
await workerQueue.enqueue(
20-
"probeEndpoint",
21-
{ id: endpointParam },
22-
{ jobKey: `probe:${endpointParam}`, runAt: new Date(Date.now() + 10000) }
23-
);
35+
// Enqueue the endpoint to be probed in 10 seconds
36+
await workerQueue.enqueue(
37+
"probeEndpoint",
38+
{ id: endpointParam },
39+
{ jobKey: `probe:${endpointParam}`, runAt: new Date(Date.now() + 10000) }
40+
);
2441

25-
return json({ success: true });
42+
return json({ success: true });
43+
}
44+
case "delete": {
45+
const service = new DeleteEndpointIndexService();
46+
await service.call(endpointParam, userId);
47+
return json({ success: true });
48+
}
49+
}
2650
} catch (e) {
2751
return json({ success: false, error: e }, { status: 400 });
2852
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { PrismaClient } from "@trigger.dev/database";
2+
import { prisma } from "~/db.server";
3+
4+
export class DeleteEndpointIndexService {
5+
#prismaClient: PrismaClient;
6+
7+
constructor(prismaClient: PrismaClient = prisma) {
8+
this.#prismaClient = prismaClient;
9+
}
10+
11+
public async call(id: string, userId: string): Promise<void> {
12+
await this.#prismaClient.endpoint.delete({
13+
where: {
14+
id,
15+
organization: {
16+
members: {
17+
some: {
18+
userId,
19+
},
20+
},
21+
},
22+
},
23+
});
24+
}
25+
}

0 commit comments

Comments
 (0)