SettingsDeleteAccount.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, RowCenterH } from "../Layout";
  7. import Text, { H2, Span } from "../Text";
  8. import { useMessage } from "../../hooks";
  9. import { TextInput } from "../Input";
  10. import { APIv2, Colors } from "../../consts";
  11. import { Button } from "../Button";
  12. import Icon from "../Icon";
  13. import Modal from "../Modal";
  14. const SettingsDeleteAccount: FC = () => {
  15. const [message, setMessage] = useMessage(1500);
  16. const [loading, setLoading] = useState(false);
  17. const [modal, setModal] = useState(false);
  18. const [formState, { password, label }] = useFormState<{ accpass: string }>(
  19. null,
  20. {
  21. withIds: true
  22. }
  23. );
  24. const onSubmit = async e => {
  25. e.preventDefault();
  26. if (loading) return;
  27. setModal(true);
  28. };
  29. const onDelete = async e => {
  30. e.preventDefault();
  31. if (loading) return;
  32. setLoading(true);
  33. try {
  34. await axios.post(
  35. `${APIv2.Users}/delete`,
  36. { password: formState.values.accpass },
  37. getAxiosConfig()
  38. );
  39. Router.push("/logout");
  40. } catch (error) {
  41. setMessage(error.response.data.error);
  42. }
  43. setLoading(false);
  44. };
  45. return (
  46. <Col alignItems="flex-start" maxWidth="100%">
  47. <H2 mb={4} bold>
  48. Delete account
  49. </H2>
  50. <Text mb={4}>Delete your account from {process.env.SITE_NAME}.</Text>
  51. <Text
  52. {...label("password")}
  53. as="label"
  54. mb={[2, 3]}
  55. fontSize={[15, 16]}
  56. bold
  57. >
  58. Password
  59. </Text>
  60. <RowCenterV as="form" onSubmit={onSubmit}>
  61. <TextInput
  62. {...password("accpass")}
  63. placeholder="Password..."
  64. autocomplete="off"
  65. mr={3}
  66. />
  67. <Button color="red" type="submit" disabled={loading}>
  68. <Icon name={loading ? "spinner" : "trash"} mr={2} stroke="white" />
  69. Delete
  70. </Button>
  71. </RowCenterV>
  72. <Modal
  73. id="delete-account"
  74. show={modal}
  75. closeHandler={() => setModal(false)}
  76. >
  77. <>
  78. <H2 mb={24} textAlign="center" bold>
  79. Delete account?
  80. </H2>
  81. <Text textAlign="center">
  82. All of your data including your <Span bold>LINKS</Span> and{" "}
  83. <Span bold>STATS</Span> will be deleted.
  84. </Text>
  85. <RowCenterH mt={44}>
  86. {loading ? (
  87. <>
  88. <Icon name="spinner" size={20} stroke={Colors.Spinner} />
  89. </>
  90. ) : message.text ? (
  91. <Text fontSize={15} color={message.color}>
  92. {message.text}
  93. </Text>
  94. ) : (
  95. <>
  96. <Button color="gray" mr={3} onClick={() => setModal(false)}>
  97. Cancel
  98. </Button>
  99. <Button color="red" ml={3} onClick={onDelete}>
  100. <Icon name="trash" stroke="white" mr={2} />
  101. Delete
  102. </Button>
  103. </>
  104. )}
  105. </RowCenterH>
  106. </>
  107. </Modal>
  108. </Col>
  109. );
  110. };
  111. export default SettingsDeleteAccount;