Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update updatePartnerProfileAction

Kiran Kb485b0cf7850
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx
index 917072bd89..e8178b36cd 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx
@@ -1,6 +1,6 @@
"use client";
-import { updatePartnerProfile } from "@/lib/actions/partners/update-partner-profile";
+import { updatePartnerProfileAction } from "@/lib/actions/partners/update-partner-profile";
import usePartnerProfile from "@/lib/swr/use-partner-profile";
import { PartnerProps } from "@/lib/types";
import {
@@ -12,6 +12,7 @@ import {
useEnterSubmit,
} from "@dub/ui";
import { cn, DICEBEAR_AVATAR_URL } from "@dub/utils";
+import { useAction } from "next-safe-action/hooks";
import { useParams } from "next/navigation";
import { PropsWithChildren, useRef } from "react";
import { Controller, useForm } from "react-hook-form";
@@ -68,21 +69,24 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {
const formRef = useRef<HTMLFormElement>(null);
const { handleKeyDown } = useEnterSubmit(formRef);
+ const { executeAsync, isExecuting } = useAction(updatePartnerProfileAction, {
+ onSuccess: async () => {
+ toast.success("Profile updated successfully.");
+ },
+ onError({ error }) {
+ setError("root.serverError", {
+ message: error.serverError,
+ });
+
+ toast.error(error.serverError);
+ },
+ });
+
return (
<form
ref={formRef}
onSubmit={handleSubmit(async (data) => {
- try {
- const result = await updatePartnerProfile({ ...data, partnerId });
- if (!result?.data?.ok) throw new Error("Failed to save profile");
- toast.success("Profile updated successfully");
- } catch (error) {
- console.error(error);
- setError("root.serverError", {
- message: "Failed to save profile. Please try again.",
- });
- toast.error("Failed to save profile. Please try again.");
- }
+ await executeAsync({ ...data, partnerId });
})}
>
<div className="px-5">
@@ -183,7 +187,7 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {
type="submit"
text="Save changes"
className="h-8 w-fit px-2.5"
- loading={isSubmitting}
+ loading={isSubmitting || isExecuting}
/>
</div>
</form>
diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.ts
index a280c7c683..2176b594ef 100644
--- a/apps/web/lib/actions/partners/update-partner-profile.ts
+++ b/apps/web/lib/actions/partners/update-partner-profile.ts
@@ -1,12 +1,11 @@
"use server";
-import { getPartnerOrThrow } from "@/lib/api/partners/get-partner-or-throw";
import { userIsInBeta } from "@/lib/edge-config";
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import { nanoid } from "@dub/utils";
import z from "../../zod";
-import { authUserActionClient } from "../safe-action";
+import { authPartnerActionClient } from "../safe-action";
const updatePartnerProfileSchema = z.object({
partnerId: z.string(),
@@ -16,53 +15,36 @@ const updatePartnerProfileSchema = z.object({
});
// Update a partner profile
-export const updatePartnerProfile = authUserActionClient
+export const updatePartnerProfileAction = authPartnerActionClient
.schema(updatePartnerProfileSchema)
.action(async ({ ctx, parsedInput }) => {
- const { user } = ctx;
-
- const { partner } = await getPartnerOrThrow({
- partnerId: parsedInput.partnerId,
- userId: user.id,
- });
+ const { user, partner } = ctx;
+ const { name, image, description } = parsedInput;
const partnersPortalEnabled = await userIsInBeta(
user.email,
"partnersPortal",
);
+
if (!partnersPortalEnabled) {
- return {
- ok: false,
- error: "Partners portal feature flag disabled.",
- };
+ throw new Error("Partners portal feature flag disabled.");
}
- const { name, image, description } = parsedInput;
-
- try {
- let imageUrl: string | null = null;
- if (image)
- imageUrl = (
+ const imageUrl = image
+ ? (
await storage.upload(
`partners/${partner.id}/image_${nanoid(7)}`,
image,
)
- ).url;
-
- await prisma.partner.update({
- where: { id: partner.id },
- data: {
- name,
- bio: description,
- image: imageUrl,
- },
- });
-
- return { ok: true };
- } catch (e) {
- console.error(e);
- return {
- ok: false,
- };
- }
+ ).url
+ : null;
+
+ await prisma.partner.update({
+ where: { id: partner.id },
+ data: {
+ name,
+ bio: description,
+ image: imageUrl,
+ },
+ });
});