SettingsDeleteAccount.tsx 2.1 KB

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