account deletion

This commit is contained in:
Jorrin
2024-04-21 17:00:33 +02:00
parent a89ef8a901
commit 7cce25a261
5 changed files with 113 additions and 4 deletions

View File

@@ -100,7 +100,7 @@ export default function Page() {
Login Login
</MWButton> </MWButton>
{mutation.isError && ( {mutation.isError && (
<Text color="$red100" textAlign="center"> <Text color="$rose200" textAlign="center">
{mutation.error.message} {mutation.error.message}
</Text> </Text>
)} )}

View File

@@ -102,7 +102,7 @@ export default function Page() {
<MWCard.Footer justifyContent="center" flexDirection="column" gap="$4"> <MWCard.Footer justifyContent="center" flexDirection="column" gap="$4">
{errorMessage && ( {errorMessage && (
<Paragraph color="$red100" textAlign="center"> <Paragraph color="$rose200" textAlign="center">
{errorMessage} {errorMessage}
</Paragraph> </Paragraph>
)} )}

View File

@@ -88,7 +88,7 @@ export default function Page() {
<MWCard.Footer justifyContent="center" flexDirection="column" gap="$4"> <MWCard.Footer justifyContent="center" flexDirection="column" gap="$4">
{mutation.isError && ( {mutation.isError && (
<Paragraph color="$red100" textAlign="center"> <Paragraph color="$rose200" textAlign="center">
{mutation.error.message} {mutation.error.message}
</Paragraph> </Paragraph>
)} )}

View File

@@ -1,6 +1,6 @@
import { useMemo } from "react"; import { useMemo } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Spinner, Text, XStack, YStack } from "tamagui"; import { H3, Spinner, Text, XStack, YStack } from "tamagui";
import { import {
base64ToBuffer, base64ToBuffer,
@@ -17,6 +17,7 @@ import { MWCard } from "../ui/Card";
import { MWInput } from "../ui/Input"; import { MWInput } from "../ui/Input";
import { MWSeparator } from "../ui/Separator"; import { MWSeparator } from "../ui/Separator";
import { Avatar } from "./Avatar"; import { Avatar } from "./Avatar";
import { DeleteAccountAlert } from "./DeleteAccountAlert";
import { getExpoIconFromDbIcon } from "./UserIconPicker"; import { getExpoIconFromDbIcon } from "./UserIconPicker";
export function AccountInformation() { export function AccountInformation() {
@@ -115,6 +116,11 @@ export function AccountInformation() {
</Text> </Text>
<MWSeparator /> <MWSeparator />
{sessions.isLoading && <Spinner />} {sessions.isLoading && <Spinner />}
{sessions.isError && (
<Text fontWeight="$bold" color="$rose200">
Error loading sessions
</Text>
)}
{deviceListSorted.map((device) => ( {deviceListSorted.map((device) => (
<MWCard bordered padded key={device.id}> <MWCard bordered padded key={device.id}>
<XStack gap="$4" alignItems="center"> <XStack gap="$4" alignItems="center">
@@ -136,6 +142,23 @@ export function AccountInformation() {
</MWCard> </MWCard>
))} ))}
</YStack> </YStack>
<YStack gap="$4">
<Text fontSize="$7" fontWeight="$bold">
Actions
</Text>
<MWSeparator />
<MWCard bordered padded>
<YStack gap="$3">
<H3 fontWeight="$bold">Delete account</H3>
<Text color="$ash300" fontWeight="$semibold">
This action is irreversible. All data will be deleted and
nothing can be recovered.
</Text>
<DeleteAccountAlert />
</YStack>
</MWCard>
</YStack>
</YStack> </YStack>
</ScreenLayout> </ScreenLayout>
); );

View File

@@ -0,0 +1,86 @@
import { useMutation } from "@tanstack/react-query";
import { AlertDialog, XStack, YStack } from "tamagui";
import { deleteUser } from "@movie-web/api";
import { useAuth } from "~/hooks/useAuth";
import { useAuthStore } from "~/stores/settings";
import { MWButton } from "../ui/Button";
export function DeleteAccountAlert() {
const account = useAuthStore((state) => state.account);
const backendUrl = useAuthStore((state) => state.backendUrl);
const { logout } = useAuth();
const logoutMutation = useMutation({
mutationKey: ["logout"],
mutationFn: logout,
});
const deleteAccountMutation = useMutation({
mutationKey: ["deleteAccount"],
mutationFn: () => deleteUser(backendUrl, account!),
onSuccess: () => {
logoutMutation.mutate();
},
});
return (
<AlertDialog native>
<AlertDialog.Trigger asChild>
<MWButton type="danger" width="$14" alignSelf="flex-end">
Delete account
</MWButton>
</AlertDialog.Trigger>
<AlertDialog.Portal>
<AlertDialog.Overlay
key="overlay"
animation="quick"
opacity={0.5}
enterStyle={{ opacity: 0 }}
exitStyle={{ opacity: 0 }}
/>
<AlertDialog.Content
bordered
elevate
key="content"
animation={[
"quick",
{
opacity: {
overshootClamping: true,
},
},
]}
enterStyle={{ x: 0, y: -20, opacity: 0, scale: 0.9 }}
exitStyle={{ x: 0, y: 10, opacity: 0, scale: 0.95 }}
x={0}
scale={1}
opacity={1}
y={0}
>
<YStack gap="$4">
<AlertDialog.Title>Are you sure?</AlertDialog.Title>
<AlertDialog.Description>
This action is irreversible. All data will be deleted and nothing
can be recovered.
</AlertDialog.Description>
<XStack gap="$3" justifyContent="flex-end">
<AlertDialog.Cancel asChild>
<MWButton>Cancel</MWButton>
</AlertDialog.Cancel>
<AlertDialog.Action
asChild
onPress={() => deleteAccountMutation.mutate()}
>
<MWButton type="purple">I am sure</MWButton>
</AlertDialog.Action>
</XStack>
</YStack>
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog>
);
}