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.

Add loading/success states

Tim Wilson5d28b1a5fdbb
diff --git a/apps/web/ui/layout/user-survey/index.tsx b/apps/web/ui/layout/user-survey/index.tsx
index 3ab28938b1..57fcfb8d85 100644
--- a/apps/web/ui/layout/user-survey/index.tsx
+++ b/apps/web/ui/layout/user-survey/index.tsx
@@ -1,12 +1,19 @@
"use client";
import { Popup, PopupContext, useResizeObserver } from "@dub/ui";
-import { motion } from "framer-motion";
-import { X } from "lucide-react";
+import { AnimatePresence, motion } from "framer-motion";
+import { CheckCircle, X } from "lucide-react";
import { useSession } from "next-auth/react";
-import { useContext, useRef } from "react";
+import { createContext, useContext, useRef, useState } from "react";
+import { toast } from "sonner";
import SurveyForm from "./survey-form";
+type UserSurveyStatus = "idle" | "loading" | "success";
+
+export const UserSurveyContext = createContext<{ status: UserSurveyStatus }>({
+ status: "idle",
+});
+
export default function UserSurveyPopup() {
const { data: session } = useSession();
@@ -25,6 +32,8 @@ export function UserSurveyPopupInner() {
const resizeObserverEntry = useResizeObserver(contentWrapperRef);
+ const [status, setStatus] = useState<UserSurveyStatus>("idle");
+
return (
<motion.div
animate={{
@@ -40,18 +49,40 @@ export function UserSurveyPopupInner() {
>
<X className="h-4 w-4 text-gray-500" />
</button>
- <SurveyForm
- onSubmit={(source) => {
- fetch("/api/user", {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ source }),
- });
- hidePopup();
- }}
- />
+ <UserSurveyContext.Provider value={{ status }}>
+ <SurveyForm
+ onSubmit={async (source) => {
+ setStatus("loading");
+ try {
+ await new Promise((resolve) => setTimeout(resolve, 2000));
+ await fetch("/api/user", {
+ method: "PUT",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ source }),
+ });
+ setStatus("success");
+ setTimeout(hidePopup, 2000);
+ } catch (e) {
+ toast.error("Failed to do it!");
+ setStatus("idle");
+ }
+ }}
+ />
+ <AnimatePresence>
+ {status === "success" && (
+ <motion.div
+ initial={{ opacity: 0, y: 10 }}
+ animate={{ opacity: 1, y: 0 }}
+ className="absolute inset-0 flex flex-col items-center justify-center space-y-3 bg-white text-sm"
+ >
+ <CheckCircle className="h-5 w-5 text-green-500" />
+ <p className="text-gray-500">Thank you!</p>
+ </motion.div>
+ )}
+ </AnimatePresence>
+ </UserSurveyContext.Provider>
</div>
</motion.div>
);
diff --git a/apps/web/ui/layout/user-survey/survey-form.tsx b/apps/web/ui/layout/user-survey/survey-form.tsx
index d2c886d921..c16b4f5df8 100644
--- a/apps/web/ui/layout/user-survey/survey-form.tsx
+++ b/apps/web/ui/layout/user-survey/survey-form.tsx
@@ -13,7 +13,8 @@ import {
} from "@dub/ui";
import { cn } from "@dub/utils";
import { ChevronRight, Globe } from "lucide-react";
-import { useState } from "react";
+import { useContext, useState } from "react";
+import { UserSurveyContext } from ".";
const options = [
{
@@ -58,6 +59,8 @@ export default function SurveyForm({
const [source, setSource] = useState<string | undefined>(undefined);
const [otherSource, setOtherSource] = useState<string | undefined>(undefined);
+ const { status } = useContext(UserSurveyContext);
+
return (
<div className="flex flex-col space-y-4">
<Logo className="h-8 w-8" />
@@ -149,8 +152,11 @@ export default function SurveyForm({
variant="primary"
type="submit"
text="Submit"
+ loading={status === "loading"}
disabled={
- !source.length || (source === "other" && !otherSource?.length)
+ status === "success" ||
+ !source.length ||
+ (source === "other" && !otherSource?.length)
}
/>
)}