forked from dubinc/dub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dubinc#777 from dubinc/survey
ENG-247: User Survey
- Loading branch information
Showing
20 changed files
with
399 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { fetcher } from "@dub/utils"; | ||
import useSWRImmutable from "swr/immutable"; | ||
import { UserProps } from "../types"; | ||
|
||
export default function useUser() { | ||
const { data, isLoading } = useSWRImmutable<UserProps>("/api/user", fetcher); | ||
|
||
return { | ||
user: data, | ||
loading: isLoading, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"use client"; | ||
|
||
import useUser from "@/lib/swr/use-user"; | ||
import { CheckCircleFill } from "@/ui/shared/icons"; | ||
import { Popup, PopupContext, useResizeObserver } from "@dub/ui"; | ||
import { AnimatePresence, motion } from "framer-motion"; | ||
import { X } from "lucide-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 { user } = useUser(); | ||
|
||
return ( | ||
user && | ||
!user.source && ( | ||
<Popup hiddenCookieId="hideUserSurveyPopup"> | ||
<UserSurveyPopupInner /> | ||
</Popup> | ||
) | ||
); | ||
} | ||
|
||
export function UserSurveyPopupInner() { | ||
const { hidePopup } = useContext(PopupContext); | ||
|
||
const contentWrapperRef = useRef<HTMLDivElement>(null); | ||
const resizeObserverEntry = useResizeObserver(contentWrapperRef); | ||
|
||
const [status, setStatus] = useState<UserSurveyStatus>("idle"); | ||
|
||
return ( | ||
<motion.div | ||
animate={{ | ||
height: resizeObserverEntry?.borderBoxSize[0].blockSize ?? "auto", | ||
}} | ||
transition={{ type: "spring", duration: 0.3 }} | ||
className="fixed bottom-4 z-50 mx-2 overflow-hidden rounded-lg border border-gray-200 bg-white shadow-md sm:left-4 sm:mx-auto sm:max-w-sm" | ||
> | ||
<div className="p-4" ref={contentWrapperRef}> | ||
<button | ||
className="absolute right-2.5 top-2.5 rounded-full p-1 transition-colors hover:bg-gray-100 active:scale-90" | ||
onClick={hidePopup} | ||
> | ||
<X className="h-4 w-4 text-gray-500" /> | ||
</button> | ||
<UserSurveyContext.Provider value={{ status }}> | ||
<SurveyForm | ||
onSubmit={async (source) => { | ||
setStatus("loading"); | ||
try { | ||
await fetch("/api/user", { | ||
method: "PUT", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ source }), | ||
}); | ||
setStatus("success"); | ||
setTimeout(hidePopup, 3000); | ||
} catch (e) { | ||
toast.error("Error saving response. Please try again."); | ||
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" | ||
> | ||
<CheckCircleFill className="h-8 w-8 text-green-500" /> | ||
<p className="text-gray-500">Thank you for your response!</p> | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
</UserSurveyContext.Provider> | ||
</div> | ||
</motion.div> | ||
); | ||
} |
Oops, something went wrong.