fix: Fixed Dialog not reseting state

This commit is contained in:
Tania Gutierrez 2024-03-19 14:13:37 -04:00
parent 34e9009e96
commit d59130930f
Signed by: riobuenoDevelops
GPG Key ID: 53133EB28EB7E801
2 changed files with 340 additions and 350 deletions

View File

@ -3,17 +3,21 @@ import { Avatar } from "./ui/avatar";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { EditIcon, FingerPrintIcon } from "./icons"; import { EditIcon, FingerPrintIcon } from "./icons";
const ManagementCardAvatar = ({ src, onClick }: { src?: string; onClick?: () => void }) => { const ManagementCardAvatar = ({ src, button, onClick }: { src?: string; button?: React.ReactNode; onClick?: () => void }) => {
return ( return (
<div className="flex justify-center"> <div className="flex justify-center">
<div className="relative w-fit h-fit"> <div className="relative w-fit h-fit">
<Avatar className="border-2 border-ring h-28 w-28" /> <Avatar className="border-2 border-ring h-28 w-28" />
<Button {!button
? <Button
onClick={onClick} onClick={onClick}
variant="outline" variant="outline"
className="absolute bottom-0 right-0 z-50 flex items-center w-10 h-10 p-0 border-white rounded-full justiyf-center hover:bg-secondary-2"> className="absolute bottom-0 right-0 z-50 flex items-center w-10 h-10 p-0 border-white rounded-full justiyf-center hover:bg-secondary-2">
<EditIcon /> <EditIcon />
</Button> </Button>
: button
}
</div> </div>
</div> </div>
); );

View File

@ -8,11 +8,18 @@ import {
useUpdate, useUpdate,
useUpdatePassword, useUpdatePassword,
} from "@refinedev/core"; } from "@refinedev/core";
import { useState } from "react"; import { useMemo, useState } from "react";
import { z } from "zod"; import { z } from "zod";
import { Field } from "~/components/forms"; import { Field } from "~/components/forms";
import { GeneralLayout } from "~/components/general-layout"; import { GeneralLayout } from "~/components/general-layout";
import { AddIcon, CloudCheckIcon, CloudIcon, CloudUploadIcon, CrownIcon, TrashIcon } from "~/components/icons"; import {
AddIcon,
CloudCheckIcon,
CloudIcon,
CloudUploadIcon,
CrownIcon,
EditIcon,
} from "~/components/icons";
import { useUppy } from "~/components/lib/uppy"; import { useUppy } from "~/components/lib/uppy";
import { import {
ManagementCard, ManagementCard,
@ -27,6 +34,7 @@ import {
DialogContent, DialogContent,
DialogHeader, DialogHeader,
DialogTitle, DialogTitle,
DialogTrigger,
} from "~/components/ui/dialog"; } from "~/components/ui/dialog";
import { Input } from "~/components/ui/input"; import { Input } from "~/components/ui/input";
import { UsageCard } from "~/components/usage-card"; import { UsageCard } from "~/components/usage-card";
@ -46,6 +54,17 @@ export default function MyAccount() {
return ( return (
<GeneralLayout> <GeneralLayout>
<h1 className="text-lg font-bold mb-4">My Account</h1> <h1 className="text-lg font-bold mb-4">My Account</h1>
<Dialog
onOpenChange={(open) => {
if (!open) {
setModal({
changeEmail: false,
changePassword: false,
setupTwoFactor: false,
changeAvatar: false,
});
}
}}>
<UsageCard <UsageCard
label="Usage" label="Usage"
currentUsage={2} currentUsage={2}
@ -62,7 +81,16 @@ export default function MyAccount() {
<div className="grid grid-cols-3 gap-x-8"> <div className="grid grid-cols-3 gap-x-8">
<ManagementCard> <ManagementCard>
<ManagementCardAvatar <ManagementCardAvatar
button={
<DialogTrigger className="absolute bottom-0 right-0 z-50">
<Button
onClick={() => setModal({ ...openModal, changeAvatar: true })} onClick={() => setModal({ ...openModal, changeAvatar: true })}
variant="outline"
className=" flex items-center w-10 h-10 p-0 border-white rounded-full justiyf-center hover:bg-secondary-2">
<EditIcon />
</Button>
</DialogTrigger>
}
/> />
</ManagementCard> </ManagementCard>
<ManagementCard> <ManagementCard>
@ -71,12 +99,14 @@ export default function MyAccount() {
{identity?.email} {identity?.email}
</ManagementCardContent> </ManagementCardContent>
<ManagementCardFooter> <ManagementCardFooter>
<DialogTrigger>
<Button <Button
className="h-12 gap-x-2" className="h-12 gap-x-2"
onClick={() => setModal({ ...openModal, changeEmail: true })}> onClick={() => setModal({ ...openModal, changeEmail: true })}>
<AddIcon /> <AddIcon />
Change Email Address Change Email Address
</Button> </Button>
</DialogTrigger>
</ManagementCardFooter> </ManagementCardFooter>
</ManagementCard> </ManagementCard>
<ManagementCard> <ManagementCard>
@ -101,12 +131,16 @@ export default function MyAccount() {
<PasswordDots className="mt-6" /> <PasswordDots className="mt-6" />
</ManagementCardContent> </ManagementCardContent>
<ManagementCardFooter> <ManagementCardFooter>
<DialogTrigger>
<Button <Button
className="h-12 gap-x-2" className="h-12 gap-x-2"
onClick={() => setModal({ ...openModal, changePassword: true })}> onClick={() =>
setModal({ ...openModal, changePassword: true })
}>
<AddIcon /> <AddIcon />
Change Password Change Password
</Button> </Button>
</DialogTrigger>
</ManagementCardFooter> </ManagementCardFooter>
</ManagementCard> </ManagementCard>
<ManagementCard> <ManagementCard>
@ -115,12 +149,16 @@ export default function MyAccount() {
Improve security by enabling 2FA. Improve security by enabling 2FA.
</ManagementCardContent> </ManagementCardContent>
<ManagementCardFooter> <ManagementCardFooter>
<DialogTrigger>
<Button <Button
className="h-12 gap-x-2" className="h-12 gap-x-2"
onClick={() => setModal({ ...openModal, setupTwoFactor: true })}> onClick={() =>
setModal({ ...openModal, setupTwoFactor: true })
}>
<AddIcon /> <AddIcon />
Enable Two-Factor Authorization Enable Two-Factor Authorization
</Button> </Button>
</DialogTrigger>
</ManagementCardFooter> </ManagementCardFooter>
</ManagementCard> </ManagementCard>
</div> </div>
@ -163,32 +201,15 @@ export default function MyAccount() {
</ManagementCardFooter> </ManagementCardFooter>
</ManagementCard> </ManagementCard>
</div> </div>
{/* Dialogs must be near to body as possible to open the modal, otherwise will be restricted to parent height-width */} <DialogContent>
<ChangeAvatarForm {openModal.changeAvatar && <ChangeAvatarForm />}
open={openModal.changeAvatar} {openModal.changeEmail && (
setOpen={(value: boolean) => <ChangeEmailForm currentValue={identity?.email || ""} />
setModal({ ...openModal, changeAvatar: value }) )}
} {openModal.changePassword && <ChangePasswordForm />}
/> {openModal.setupTwoFactor && <SetupTwoFactorDialog />}
<ChangeEmailForm </DialogContent>
open={openModal.changeEmail} </Dialog>
setOpen={(value: boolean) =>
setModal({ ...openModal, changeEmail: value })
}
currentValue={identity?.email || ""}
/>
<ChangePasswordForm
open={openModal.changePassword}
setOpen={(value: boolean) =>
setModal({ ...openModal, changePassword: value })
}
/>
<SetupTwoFactorDialog
open={openModal.setupTwoFactor}
setOpen={(value: boolean) =>
setModal({ ...openModal, setupTwoFactor: value })
}
/>
</GeneralLayout> </GeneralLayout>
); );
} }
@ -210,15 +231,7 @@ const ChangeEmailSchema = z
return true; return true;
}); });
const ChangeEmailForm = ({ const ChangeEmailForm = ({ currentValue }: { currentValue: string }) => {
open,
setOpen,
currentValue,
}: {
open: boolean;
setOpen: (value: boolean) => void;
currentValue: string;
}) => {
const { data: identity } = useGetIdentity<{ id: BaseKey }>(); const { data: identity } = useGetIdentity<{ id: BaseKey }>();
const { mutate: updateEmail } = useUpdate(); const { mutate: updateEmail } = useUpdate();
const [form, fields] = useForm({ const [form, fields] = useForm({
@ -244,10 +257,10 @@ const ChangeEmailForm = ({
}); });
return ( return (
<Dialog open={open} onOpenChange={setOpen}> <>
<DialogContent className="p-8" forceMount>
<DialogHeader> <DialogHeader>
<DialogTitle className="mb-8">Change Email</DialogTitle> <DialogTitle className="mb-8">Change Email</DialogTitle>
</DialogHeader>
<div className="rounded-full px-4 py-2 w-fit text-sm bg-ring font-bold text-secondary-1"> <div className="rounded-full px-4 py-2 w-fit text-sm bg-ring font-bold text-secondary-1">
{currentValue} {currentValue}
</div> </div>
@ -273,9 +286,7 @@ const ChangeEmailForm = ({
/> />
<Button className="w-full h-14">Change Email Address</Button> <Button className="w-full h-14">Change Email Address</Button>
</form> </form>
</DialogHeader> </>
</DialogContent>
</Dialog>
); );
}; };
@ -296,13 +307,7 @@ const ChangePasswordSchema = z
return true; return true;
}); });
const ChangePasswordForm = ({ const ChangePasswordForm = () => {
open,
setOpen,
}: {
open: boolean;
setOpen: (value: boolean) => void;
}) => {
const { mutate: updatePassword } = useUpdatePassword<{ password: string }>(); const { mutate: updatePassword } = useUpdatePassword<{ password: string }>();
const [form, fields] = useForm({ const [form, fields] = useForm({
id: "login", id: "login",
@ -323,10 +328,10 @@ const ChangePasswordForm = ({
}); });
return ( return (
<Dialog open={open} onOpenChange={setOpen}> <>
<DialogContent className="p-8">
<DialogHeader> <DialogHeader>
<DialogTitle className="mb-8">Change Password</DialogTitle> <DialogTitle className="mb-8">Change Password</DialogTitle>
</DialogHeader>
<form {...getFormProps(form)}> <form {...getFormProps(form)}>
<Field <Field
inputProps={{ inputProps={{
@ -351,31 +356,18 @@ const ChangePasswordForm = ({
/> />
<Button className="w-full h-14">Change Password</Button> <Button className="w-full h-14">Change Password</Button>
</form> </form>
</DialogHeader> </>
</DialogContent>
</Dialog>
); );
}; };
const SetupTwoFactorDialog = ({ const SetupTwoFactorDialog = () => {
open,
setOpen,
}: {
open: boolean;
setOpen: (value: boolean) => void;
}) => {
const [continueModal, setContinue] = useState<boolean>(false); const [continueModal, setContinue] = useState<boolean>(false);
return ( return (
<Dialog <>
open={open}
onOpenChange={(value) => {
setOpen(value);
setContinue(false);
}}>
<DialogContent className="p-8">
<DialogHeader> <DialogHeader>
<DialogTitle className="mb-8">Setup Two-Factor</DialogTitle> <DialogTitle className="mb-8">Setup Two-Factor</DialogTitle>
</DialogHeader>
<div className="flex flex-col gap-y-6"> <div className="flex flex-col gap-y-6">
{continueModal ? ( {continueModal ? (
<> <>
@ -397,27 +389,17 @@ const SetupTwoFactorDialog = ({
<div className="p-4 border text-primary-2 text-center font-bold"> <div className="p-4 border text-primary-2 text-center font-bold">
HHH7MFGAMPJ44OM44FGAMPJ44O232 HHH7MFGAMPJ44OM44FGAMPJ44O232
</div> </div>
<Button <Button className="w-full h-14" onClick={() => setContinue(true)}>
className="w-full h-14"
onClick={() => setContinue(true)}>
Continue Continue
</Button> </Button>
</> </>
)} )}
</div> </div>
</DialogHeader> </>
</DialogContent>
</Dialog>
); );
}; };
const ChangeAvatarForm = ({ const ChangeAvatarForm = () => {
open,
setOpen,
}: {
open: boolean;
setOpen: (value: boolean) => void;
}) => {
const { const {
getRootProps, getRootProps,
getInputProps, getInputProps,
@ -437,13 +419,16 @@ const ChangeAvatarForm = ({
const isCompleted = state === "completed"; const isCompleted = state === "completed";
const hasStarted = state !== "idle" && state !== "initializing"; const hasStarted = state !== "idle" && state !== "initializing";
const file = getFiles()?.[0];
const imagePreview = useMemo(() => {
if (file) {
return URL.createObjectURL(file.data);
}
}, [file]);
return ( return (
<Dialog <>
open={open}
onOpenChange={(value) => {
setOpen(value);
}}>
<DialogContent className="p-8">
<DialogHeader className="mb-6"> <DialogHeader className="mb-6">
<DialogTitle>Edit Avatar</DialogTitle> <DialogTitle>Edit Avatar</DialogTitle>
</DialogHeader> </DialogHeader>
@ -464,23 +449,25 @@ const ChangeAvatarForm = ({
</div> </div>
) : null} ) : null}
{(!hasStarted && getFiles().length > 0) && ( {!hasStarted && file && (
<div className="border border-border rounded p-4 bg-primary-dark relative"> <div className="border border-border rounded p-4 bg-primary-dark relative">
<Button <Button
className="absolute top-4 right-4 rounded-full aspect-square bg-primary-dark hover:bg-primary p-2 text-sm" className="absolute top-1/2 right-1/2 rounded-full bg-gray-800/50 hover:bg-primary p-2 text-sm"
onClick={() => removeFile(getFiles()[0].id)}> onClick={() => removeFile(file?.id)}>
<Cross2Icon /> <Cross2Icon className="size-4" />
</Button> </Button>
<img className="w-full h-48" src={URL.createObjectURL(getFiles()[0].data)} alt="New Avatar Preview" /> <img
className="w-full h-48 object-contain"
src={imagePreview}
alt="New Avatar Preview"
/>
</div> </div>
)} )}
{hasStarted ? ( {hasStarted ? (
<div className="flex flex-col items-center gap-y-2 w-full text-primary-1"> <div className="flex flex-col items-center gap-y-2 w-full text-primary-1">
<CloudCheckIcon className="w-32 h-32" /> <CloudCheckIcon className="w-32 h-32" />
{isCompleted {isCompleted ? "Upload completed" : `0% completed`}
? "Upload completed"
: `0% completed`}
</div> </div>
) : null} ) : null}
@ -505,8 +492,7 @@ const ChangeAvatarForm = ({
Upload Upload
</Button> </Button>
) : null} ) : null}
</DialogContent> </>
</Dialog>
); );
}; };