Delete account support

This commit is contained in:
Paulo Gustavo Veiga 2021-02-15 17:15:54 -08:00
parent dc01dfb7d0
commit 02fae8e18d
4 changed files with 63 additions and 6 deletions

View File

@ -65,6 +65,7 @@ export type AccountInfo = {
} }
interface Client { interface Client {
deleteAccount(): Promise<void>
importMap(model: ImportMapInfo): Promise<number> importMap(model: ImportMapInfo): Promise<number>
createMap(map: BasicMapInfo): Promise<number>; createMap(map: BasicMapInfo): Promise<number>;
deleteMaps(ids: number[]): Promise<void>; deleteMaps(ids: number[]): Promise<void>;

View File

@ -34,8 +34,13 @@ class MockClient implements Client {
]; ];
} }
deleteAccount(): Promise<void> {
return Promise.resolve();
}
updateAccountInfo(firstname: string, lastname: string): Promise<void> { updateAccountInfo(firstname: string, lastname: string): Promise<void> {
throw new Error('Method not implemented.'); return Promise.resolve();
} }
updateAccountPassword(pasword: string): Promise<void> { updateAccountPassword(pasword: string): Promise<void> {

View File

@ -10,6 +10,19 @@ export default class RestClient implements Client {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.sessionExpired = sessionExpired; this.sessionExpired = sessionExpired;
} }
deleteAccount(): Promise<void> {
const handler = (success: () => void, reject: (error: ErrorInfo) => void) => {
axios.delete(this.baseUrl + `/c/restful/account`,
{ headers: { 'Content-Type': 'text/plain' } }
).then(() => {
success();
}).catch(error => {
const errorInfo = this.parseResponseOnError(error.response);
reject(errorInfo);
});
}
return new Promise(handler);
}
updateAccountInfo(firstname: string, lastname: string): Promise<void> { updateAccountInfo(firstname: string, lastname: string): Promise<void> {
const handler = (success: () => void, reject: (error: ErrorInfo) => void) => { const handler = (success: () => void, reject: (error: ErrorInfo) => void) => {

View File

@ -1,12 +1,13 @@
import { FormControl } from "@material-ui/core"; import { FormControl, FormControlLabel, FormGroup, FormHelperText, FormLabel, Switch } from "@material-ui/core";
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import { useIntl } from "react-intl"; import { FormattedMessage, useIntl } from "react-intl";
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import Client, { ErrorInfo } from "../../../../classes/client"; import Client, { ErrorInfo } from "../../../../classes/client";
import Input from "../../../form/input"; import Input from "../../../form/input";
import BaseDialog from "../../action-dispatcher/base-dialog"; import BaseDialog from "../../action-dispatcher/base-dialog";
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import { activeInstance, fetchAccount } from "../../../../redux/clientSlice"; import { activeInstance, fetchAccount } from "../../../../redux/clientSlice";
import { Alert } from "@material-ui/lab";
type AccountInfoDialogProps = { type AccountInfoDialogProps = {
@ -23,12 +24,13 @@ const defaultModel: AccountInfoModel = { firstname: '', lastname: '', email: ''
const AccountInfoDialog = ({ onClose }: AccountInfoDialogProps) => { const AccountInfoDialog = ({ onClose }: AccountInfoDialogProps) => {
const client: Client = useSelector(activeInstance); const client: Client = useSelector(activeInstance);
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const [remove, setRemove] = React.useState<boolean>(false);
const [model, setModel] = React.useState<AccountInfoModel>(defaultModel); const [model, setModel] = React.useState<AccountInfoModel>(defaultModel);
const [error, setError] = React.useState<ErrorInfo>(); const [error, setError] = React.useState<ErrorInfo>();
const intl = useIntl(); const intl = useIntl();
const mutation = useMutation<void, ErrorInfo, AccountInfoModel>((model: AccountInfoModel) => { const mutationChangeName = useMutation<void, ErrorInfo, AccountInfoModel>((model: AccountInfoModel) => {
return client.updateAccountInfo(model.firstname, model.lastname); return client.updateAccountInfo(model.firstname, model.lastname);
}, },
{ {
@ -42,6 +44,20 @@ const AccountInfoDialog = ({ onClose }: AccountInfoDialogProps) => {
} }
); );
const mutationRemove = useMutation<void, ErrorInfo, void>(() => {
return client.deleteAccount();
},
{
onSuccess: () => {
window.location.href = '/c/logout'
onClose()
},
onError: (error) => {
setError(error);
}
}
);
const account = fetchAccount(); const account = fetchAccount();
useEffect(() => { useEffect(() => {
if (account) { if (account) {
@ -62,7 +78,11 @@ const AccountInfoDialog = ({ onClose }: AccountInfoDialogProps) => {
const handleOnSubmit = (event: React.FormEvent<HTMLFormElement>): void => { const handleOnSubmit = (event: React.FormEvent<HTMLFormElement>): void => {
event.preventDefault(); event.preventDefault();
mutation.mutate(model); if (remove) {
mutationRemove.mutate();
} else {
mutationChangeName.mutate(model);
}
}; };
const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>): void => { const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
@ -72,10 +92,15 @@ const AccountInfoDialog = ({ onClose }: AccountInfoDialogProps) => {
const value = event.target.value; const value = event.target.value;
setModel({ ...model, [name as keyof AccountInfoModel]: value }); setModel({ ...model, [name as keyof AccountInfoModel]: value });
} }
const handleOnRemoveChange = (event) => {
setRemove(event.target.checked);
};
return ( return (
<BaseDialog onClose={handleOnClose} onSubmit={handleOnSubmit} error={error} <BaseDialog onClose={handleOnClose} onSubmit={handleOnSubmit} error={error}
title={intl.formatMessage({ id: 'accountinfo.title', defaultMessage: 'Account info' })} title={intl.formatMessage({ id: 'accountinfo.title', defaultMessage: 'Account info' })}
submitButton={intl.formatMessage({ id: 'accountinfo.button', defaultMessage: 'Save' })}> submitButton={intl.formatMessage({ id: 'accountinfo.button', defaultMessage: 'Accept' })}>
<FormControl fullWidth={true}> <FormControl fullWidth={true}>
<Input name="email" type="text" disabled={true} label={intl.formatMessage({ id: "accountinfo.email", defaultMessage: "Email" })} <Input name="email" type="text" disabled={true} label={intl.formatMessage({ id: "accountinfo.email", defaultMessage: "Email" })}
@ -86,7 +111,20 @@ const AccountInfoDialog = ({ onClose }: AccountInfoDialogProps) => {
<Input name="lastname" type="text" label={intl.formatMessage({ id: "accountinfo.lastname", defaultMessage: "Last Name" })} <Input name="lastname" type="text" label={intl.formatMessage({ id: "accountinfo.lastname", defaultMessage: "Last Name" })}
value={model.lastname} onChange={handleOnChange} required={true} fullWidth={true} /> value={model.lastname} onChange={handleOnChange} required={true} fullWidth={true} />
<FormGroup>
{remove &&
<Alert severity="error">
<FormattedMessage id="account.delete-warning" defaultMessage=" Keep in mind that you will not be able retrieve any mindmap you have added.If you would still like your account deleted." />
</Alert>
}
<FormControlLabel
control={<Switch checked={remove} onChange={(handleOnRemoveChange)} name="remove" color="primary" />}
label="Delete Account"
/>
</FormGroup>
</FormControl> </FormControl>
</BaseDialog> </BaseDialog>
); );
} }