-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Relocate settings subcomponents out of route file (#242)
- Loading branch information
Showing
21 changed files
with
488 additions
and
342 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
67 changes: 67 additions & 0 deletions
67
src/components/settings/DeleteAccountSetting/DeleteAccountSetting.tsx
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,67 @@ | ||
import { Button, Modal, ModalFooter, ModalHeader } from "@/components/common"; | ||
import { SettingsItem } from "@/components/settings"; | ||
import { useAuthActions } from "@convex-dev/auth/react"; | ||
import { api } from "@convex/_generated/api"; | ||
import { useMutation } from "convex/react"; | ||
import { Trash } from "lucide-react"; | ||
import { useState } from "react"; | ||
|
||
type DeleteAccountModalProps = { | ||
isOpen: boolean; | ||
onOpenChange: (isOpen: boolean) => void; | ||
onSubmit: () => void; | ||
}; | ||
|
||
const DeleteAccountModal = ({ | ||
isOpen, | ||
onOpenChange, | ||
onSubmit, | ||
}: DeleteAccountModalProps) => { | ||
const { signOut } = useAuthActions(); | ||
|
||
const clearLocalStorage = () => { | ||
localStorage.removeItem("theme"); | ||
}; | ||
const deleteAccount = useMutation(api.users.deleteCurrentUser); | ||
|
||
const handleSubmit = () => { | ||
clearLocalStorage(); | ||
deleteAccount(); | ||
signOut(); | ||
onSubmit(); | ||
}; | ||
|
||
return ( | ||
<Modal isOpen={isOpen} onOpenChange={onOpenChange}> | ||
<ModalHeader title="Delete account?" /> | ||
<p>This will permanently erase your account and all data.</p> | ||
<ModalFooter> | ||
<Button onPress={() => onOpenChange(false)}>Cancel</Button> | ||
<Button variant="destructive" onPress={handleSubmit}> | ||
Delete | ||
</Button> | ||
</ModalFooter> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export const DeleteAccountSetting = () => { | ||
const [isDeleteAccountModalOpen, setIsDeleteAccountModalOpen] = | ||
useState(false); | ||
|
||
return ( | ||
<SettingsItem | ||
label="Delete account" | ||
description="Permanently delete your Namesake account and data." | ||
> | ||
<Button onPress={() => setIsDeleteAccountModalOpen(true)} icon={Trash}> | ||
Delete account | ||
</Button> | ||
<DeleteAccountModal | ||
isOpen={isDeleteAccountModalOpen} | ||
onOpenChange={setIsDeleteAccountModalOpen} | ||
onSubmit={() => setIsDeleteAccountModalOpen(false)} | ||
/> | ||
</SettingsItem> | ||
); | ||
}; |
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 @@ | ||
export * from "./DeleteAccountSetting"; |
96 changes: 96 additions & 0 deletions
96
src/components/settings/EditBirthplaceSetting/EditBirthplaceSetting.tsx
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,96 @@ | ||
import { | ||
Button, | ||
Form, | ||
Modal, | ||
ModalFooter, | ||
ModalHeader, | ||
Select, | ||
SelectItem, | ||
} from "@/components/common"; | ||
import { api } from "@convex/_generated/api"; | ||
import type { Doc } from "@convex/_generated/dataModel"; | ||
import { JURISDICTIONS, type Jurisdiction } from "@convex/constants"; | ||
import { useMutation } from "convex/react"; | ||
import { Pencil } from "lucide-react"; | ||
import { useState } from "react"; | ||
import { SettingsItem } from "../SettingsItem"; | ||
|
||
type EditBirthplaceModalProps = { | ||
defaultBirthplace: Jurisdiction; | ||
isOpen: boolean; | ||
onOpenChange: (isOpen: boolean) => void; | ||
onSubmit: () => void; | ||
}; | ||
|
||
const EditBirthplaceModal = ({ | ||
defaultBirthplace, | ||
isOpen, | ||
onOpenChange, | ||
onSubmit, | ||
}: EditBirthplaceModalProps) => { | ||
const updateBirthplace = useMutation(api.users.setBirthplace); | ||
const [birthplace, setBirthplace] = useState(defaultBirthplace); | ||
|
||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
updateBirthplace({ birthplace }); | ||
onSubmit(); | ||
}; | ||
|
||
return ( | ||
<Modal isOpen={isOpen} onOpenChange={onOpenChange}> | ||
<ModalHeader title="Edit birthplace" /> | ||
<Form onSubmit={handleSubmit} className="w-full"> | ||
<Select | ||
aria-label="Birthplace" | ||
name="birthplace" | ||
selectedKey={birthplace} | ||
onSelectionChange={(key) => setBirthplace(key as Jurisdiction)} | ||
placeholder="Select state" | ||
className="w-full" | ||
> | ||
{Object.entries(JURISDICTIONS).map(([value, label]) => ( | ||
<SelectItem key={value} id={value}> | ||
{label} | ||
</SelectItem> | ||
))} | ||
</Select> | ||
<ModalFooter> | ||
<Button variant="secondary" onPress={() => onOpenChange(false)}> | ||
Cancel | ||
</Button> | ||
<Button type="submit" variant="primary"> | ||
Save | ||
</Button> | ||
</ModalFooter> | ||
</Form> | ||
</Modal> | ||
); | ||
}; | ||
|
||
type EditBirthplaceSettingProps = { | ||
user: Doc<"users">; | ||
}; | ||
|
||
export const EditBirthplaceSetting = ({ user }: EditBirthplaceSettingProps) => { | ||
const [isBirthplaceModalOpen, setIsBirthplaceModalOpen] = useState(false); | ||
|
||
return ( | ||
<SettingsItem | ||
label="Birthplace" | ||
description="Where were you born? This location is used to select the forms for your birth certificate." | ||
> | ||
<Button icon={Pencil} onPress={() => setIsBirthplaceModalOpen(true)}> | ||
{user?.birthplace | ||
? JURISDICTIONS[user.birthplace as Jurisdiction] | ||
: "Set birthplace"} | ||
</Button> | ||
<EditBirthplaceModal | ||
isOpen={isBirthplaceModalOpen} | ||
onOpenChange={setIsBirthplaceModalOpen} | ||
defaultBirthplace={user.birthplace as Jurisdiction} | ||
onSubmit={() => setIsBirthplaceModalOpen(false)} | ||
/> | ||
</SettingsItem> | ||
); | ||
}; |
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 @@ | ||
export * from "./EditBirthplaceSetting"; |
28 changes: 28 additions & 0 deletions
28
src/components/settings/EditMinorSetting/EditMinorSetting.tsx
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,28 @@ | ||
import { Switch } from "@/components/common"; | ||
import { SettingsItem } from "@/components/settings"; | ||
import { api } from "@convex/_generated/api"; | ||
import type { Doc } from "@convex/_generated/dataModel"; | ||
import { useMutation } from "convex/react"; | ||
|
||
type EditMinorSettingProps = { | ||
user: Doc<"users">; | ||
}; | ||
|
||
export const EditMinorSetting = ({ user }: EditMinorSettingProps) => { | ||
const updateIsMinor = useMutation(api.users.setCurrentUserIsMinor); | ||
|
||
return ( | ||
<SettingsItem | ||
label="Under 18" | ||
description="Are you under 18 years old or applying on behalf of someone who is?" | ||
> | ||
<Switch | ||
name="isMinor" | ||
isSelected={user.isMinor ?? false} | ||
onChange={() => updateIsMinor({ isMinor: !user.isMinor })} | ||
> | ||
<span className="sr-only">Is minor</span> | ||
</Switch> | ||
</SettingsItem> | ||
); | ||
}; |
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 @@ | ||
export * from "./EditMinorSetting"; |
85 changes: 85 additions & 0 deletions
85
src/components/settings/EditNameSetting/EditNameSetting.tsx
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,85 @@ | ||
import { | ||
Button, | ||
Form, | ||
Modal, | ||
ModalFooter, | ||
ModalHeader, | ||
TextField, | ||
} from "@/components/common"; | ||
import { api } from "@convex/_generated/api"; | ||
import type { Doc } from "@convex/_generated/dataModel"; | ||
import { useMutation } from "convex/react"; | ||
import { Pencil } from "lucide-react"; | ||
import { useState } from "react"; | ||
import { SettingsItem } from "../SettingsItem"; | ||
|
||
type EditNameModalProps = { | ||
defaultName: string; | ||
isOpen: boolean; | ||
onOpenChange: (isOpen: boolean) => void; | ||
onSubmit: () => void; | ||
}; | ||
|
||
const EditNameModal = ({ | ||
defaultName, | ||
isOpen, | ||
onOpenChange, | ||
onSubmit, | ||
}: EditNameModalProps) => { | ||
const updateName = useMutation(api.users.setName); | ||
const [name, setName] = useState(defaultName); | ||
|
||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
updateName({ name }); | ||
onSubmit(); | ||
}; | ||
|
||
return ( | ||
<Modal isOpen={isOpen} onOpenChange={onOpenChange}> | ||
<ModalHeader title="Edit name" /> | ||
<Form onSubmit={handleSubmit} className="w-full"> | ||
<TextField | ||
name="name" | ||
label="Name" | ||
value={name} | ||
onChange={setName} | ||
className="w-full" | ||
/> | ||
<ModalFooter> | ||
<Button variant="secondary" onPress={() => onOpenChange(false)}> | ||
Cancel | ||
</Button> | ||
<Button type="submit" variant="primary"> | ||
Save | ||
</Button> | ||
</ModalFooter> | ||
</Form> | ||
</Modal> | ||
); | ||
}; | ||
|
||
type EditNameSettingProps = { | ||
user: Doc<"users">; | ||
}; | ||
|
||
export const EditNameSetting = ({ user }: EditNameSettingProps) => { | ||
const [isNameModalOpen, setIsNameModalOpen] = useState(false); | ||
|
||
return ( | ||
<SettingsItem | ||
label="Name" | ||
description="How should Namesake refer to you? This can be different from your legal name." | ||
> | ||
<Button icon={Pencil} onPress={() => setIsNameModalOpen(true)}> | ||
{user?.name ?? "Set name"} | ||
</Button> | ||
<EditNameModal | ||
isOpen={isNameModalOpen} | ||
onOpenChange={setIsNameModalOpen} | ||
defaultName={user.name ?? ""} | ||
onSubmit={() => setIsNameModalOpen(false)} | ||
/> | ||
</SettingsItem> | ||
); | ||
}; |
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 @@ | ||
export * from "./EditNameSetting"; |
Oops, something went wrong.