SettingsDeleteAccount.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { useFormState } from "react-use-form-state";
  2. import React, { FC, useState } from "react";
  3. import Router from "next/router";
  4. import axios from "axios";
  5. import { getAxiosConfig } from "../../utils";
  6. import { Col, RowCenterV } from "../Layout";
  7. import Text, { H2, Span } from "../Text";
  8. import { useMessage } from "../../hooks";
  9. import { TextInput } from "../Input";
  10. import { APIv2 } from "../../consts";
  11. import { Button } from "../Button";
  12. import Icon from "../Icon";
  13. const SettingsDeleteAccount: FC = () => {
  14. const [message, setMessage] = useMessage(1500);
  15. const [loading, setLoading] = useState(false);
  16. const [formState, { password, label }] = useFormState<{ accpass: string }>(
  17. null,
  18. {
  19. withIds: true
  20. }
  21. );
  22. const onSubmit = async e => {
  23. e.preventDefault();
  24. if (loading) return;
  25. setLoading(true);
  26. try {
  27. await axios.post(
  28. `${APIv2.Users}/delete`,
  29. { password: formState.values.accpass },
  30. getAxiosConfig()
  31. );
  32. Router.push("/logout");
  33. } catch (error) {
  34. setMessage(error.response.data.error);
  35. }
  36. setLoading(false);
  37. };
  38. return (
  39. <Col alignItems="flex-start" maxWidth="100%">
  40. <H2 mb={4} bold>
  41. Delete account
  42. </H2>
  43. <Text mb={4}>
  44. Delete your account from {process.env.SITE_NAME}. All of your data
  45. including your <Span bold>LINKS</Span> and <Span bold>STATS</Span> will
  46. be deleted.
  47. </Text>
  48. <Text
  49. {...label("password")}
  50. as="label"
  51. mb={[2, 3]}
  52. fontSize={[15, 16]}
  53. bold
  54. >
  55. Password
  56. </Text>
  57. <RowCenterV as="form" onSubmit={onSubmit}>
  58. <TextInput
  59. {...password("accpass")}
  60. placeholder="Password..."
  61. autocomplete="off"
  62. mr={3}
  63. />
  64. <Button color="red" type="submit" disabled={loading}>
  65. <Icon name={loading ? "spinner" : "trash"} mr={2} stroke="white" />
  66. {loading ? "Deleting..." : "Delete"}
  67. </Button>
  68. </RowCenterV>
  69. <Text fontSize={15} mt={3} color={message.color}>
  70. {message.text}
  71. </Text>
  72. </Col>
  73. );
  74. };
  75. export default SettingsDeleteAccount;