SettingsDeleteAccount.tsx 3.3 KB

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