Skip to content

Commit

Permalink
update API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Oct 16, 2024
1 parent 68d9e67 commit dad8c30
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { updateUTMTemplateBodySchema } from "@/lib/zod/schemas/utm-templates";
import { updateUTMTemplateBodySchema } from "@/lib/zod/schemas/utm";
import { NextResponse } from "next/server";

// PATCH /api/utm-templates/[id] – update a UTM template
// PATCH /api/utm/[id] – update a UTM template
export const PATCH = withWorkspace(
async ({ req, params, workspace }) => {
const { id } = params;
Expand Down Expand Up @@ -52,9 +52,7 @@ export const PATCH = withWorkspace(
},
);

export const PUT = PATCH;

// DELETE /api/utm-templates/[id] – delete a UTM template for a workspace
// DELETE /api/utm/[id] – delete a UTM template for a workspace
export const DELETE = withWorkspace(
async ({ params, workspace }) => {
const { id } = params;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { createUTMTemplateBodySchema } from "@/lib/zod/schemas/utm-templates";
import { createUTMTemplateBodySchema } from "@/lib/zod/schemas/utm";
import { NextResponse } from "next/server";

// GET /api/utm-templates - get all UTM templates for a workspace
// GET /api/utm - get all UTM templates for a workspace
export const GET = withWorkspace(
async ({ workspace, headers }) => {
const templates = await prisma.utmTemplate.findMany({
Expand All @@ -27,7 +27,7 @@ export const GET = withWorkspace(
},
);

// POST /api/utm-templates - create or update a UTM template for a workspace
// POST /api/utm - create a new UTM template for a workspace
export const POST = withWorkspace(
async ({ req, workspace, session, headers }) => {
const props = createUTMTemplateBodySchema.parse(await req.json());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function WorkspaceUtmTemplatesClient() {
const { id: workspaceId } = useWorkspace();

const { data: templates, isLoading } = useSWR<UtmTemplateWithUserProps[]>(
workspaceId && `/api/utm-templates?workspaceId=${workspaceId}`,
workspaceId && `/api/utm?workspaceId=${workspaceId}`,
fetcher,
{
dedupingInterval: 60000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export function TemplateCard({
if (!confirm("Are you sure you want to delete this template?")) return;

setProcessing(true);
fetch(`/api/utm-templates/${template.id}?workspaceId=${id}`, {
fetch(`/api/utm/${template.id}?workspaceId=${id}`, {
method: "DELETE",
})
.then(async (res) => {
if (res.ok) {
await mutate(`/api/utm-templates?workspaceId=${id}`);
await mutate(`/api/utm?workspaceId=${id}`);
toast.success("Template deleted");
} else {
const { error } = await res.json();
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions apps/web/ui/modals/add-edit-utm-template.modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ function AddEditUtmTemplateModal({
id
? {
method: "PATCH",
url: `/api/utm-templates/${id}?workspaceId=${workspaceId}`,
url: `/api/utm/${id}?workspaceId=${workspaceId}`,
successMessage: "Successfully updated template!",
}
: {
method: "POST",
url: `/api/utm-templates?workspaceId=${workspaceId}`,
url: `/api/utm?workspaceId=${workspaceId}`,
successMessage: "Successfully added template!",
},
[id],
Expand Down Expand Up @@ -97,7 +97,7 @@ function AddEditUtmTemplateModal({
utmTemplateName: data.name,
},
);
await mutate(`/api/utm-templates?workspaceId=${workspaceId}`);
await mutate(`/api/utm?workspaceId=${workspaceId}`);
toast.success(endpoint.successMessage);
setShowAddEditUtmTemplateModal(false);
} catch (e) {
Expand Down
14 changes: 8 additions & 6 deletions apps/web/ui/modals/archive-domain-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ function ArchiveDomainModal({
}

await mutate(
(key) =>
typeof key === "string" &&
key.startsWith(`/api/domains?workspaceId=${workspaceId}`),
(key) => typeof key === "string" && key.startsWith("/api/domains"),
undefined,
{
revalidate: true,
},
);
setShowArchiveDomainModal(false);
toastWithUndo({
Expand All @@ -89,9 +91,9 @@ function ArchiveDomainModal({
error: "Failed to roll back changes. An error occurred.",
success: async () => {
await mutate(
(key) =>
typeof key === "string" &&
key.startsWith(`/api/domains?workspaceId=${workspaceId}`),
(key) => typeof key === "string" && key.startsWith("/api/domains"),
undefined,
{ revalidate: true },
);
return "Undo successful! Changes reverted.";
},
Expand Down
9 changes: 9 additions & 0 deletions apps/web/ui/modals/link-builder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ function LinkBuilderInner({
),
// Mutate workspace to update usage stats
mutate(`/api/workspaces/${slug}`),
// if updating root domain link, mutate domains as well
key === "_root" &&
mutate(
(key) =>
typeof key === "string" &&
key.startsWith("/api/domains"),
undefined,
{ revalidate: true },
),
]);
const data = await res.json();
posthog.capture(
Expand Down
2 changes: 1 addition & 1 deletion apps/web/ui/modals/link-builder/utm-templates-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function UTMTemplatesButton({
const { id: workspaceId } = useWorkspace();

const { data, isLoading } = useSWR<UtmTemplateProps[]>(
workspaceId && `/api/utm-templates?workspaceId=${workspaceId}`,
workspaceId && `/api/utm?workspaceId=${workspaceId}`,
fetcher,
{
dedupingInterval: 60000,
Expand Down
21 changes: 9 additions & 12 deletions apps/web/ui/modals/link-builder/utm-templates-combo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function UTMTemplatesCombo({
const { setValue, getValues } = useFormContext();

const { data } = useSWR<UtmTemplateProps[]>(
workspaceId && `/api/utm-templates?workspaceId=${workspaceId}`,
workspaceId && `/api/utm?workspaceId=${workspaceId}`,
fetcher,
{
dedupingInterval: 60000,
Expand Down Expand Up @@ -89,23 +89,20 @@ export function UTMTemplatesCombo({
createLabel={(search) => `Save new template: "${search}"`}
onCreate={async (search) => {
try {
const res = await fetch(
`/api/utm-templates?workspaceId=${workspaceId}`,
{
method: "POST",
body: JSON.stringify({
name: search,
...getParamsFromURL(getValues("url")),
}),
},
);
const res = await fetch(`/api/utm?workspaceId=${workspaceId}`, {
method: "POST",
body: JSON.stringify({
name: search,
...getParamsFromURL(getValues("url")),
}),
});
if (!res.ok) {
const { error } = await res.json();
toast.error(error.message);
return false;
}

mutate(`/api/utm-templates?workspaceId=${workspaceId}`);
mutate(`/api/utm?workspaceId=${workspaceId}`);
toast.success("Template saved successfully");
return true;
} catch (e) {
Expand Down

0 comments on commit dad8c30

Please sign in to comment.