SettingsChangeEmail.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { useFormState } from "react-use-form-state";
  2. import React, { FC, useState } from "react";
  3. import { Flex } from "rebass";
  4. import axios from "axios";
  5. import { getAxiosConfig } from "../../utils";
  6. import { useMessage } from "../../hooks";
  7. import { APIv2 } from "../../consts";
  8. import { TextInput } from "../Input";
  9. import Text, { H2 } from "../Text";
  10. import { Button } from "../Button";
  11. import { Col } from "../Layout";
  12. import Icon from "../Icon";
  13. const SettingsChangeEmail: FC = () => {
  14. const [loading, setLoading] = useState(false);
  15. const [message, setMessage] = useMessage(5000);
  16. const [formState, { password, email, label }] = useFormState<{
  17. changeemailpass: string;
  18. changeemailaddress: string;
  19. }>(null, {
  20. withIds: true
  21. });
  22. const onSubmit = async (e) => {
  23. e.preventDefault();
  24. if (loading) return;
  25. setLoading(true);
  26. try {
  27. const res = await axios.post(
  28. APIv2.AuthChangeEmail,
  29. {
  30. password: formState.values.changeemailpass,
  31. email: formState.values.changeemailaddress
  32. },
  33. getAxiosConfig()
  34. );
  35. setMessage(res.data.message, "green");
  36. } catch (error) {
  37. setMessage(error?.response?.data?.error || "Couldn't send email.");
  38. }
  39. setLoading(false);
  40. };
  41. return (
  42. <Col alignItems="flex-start" maxWidth="100%">
  43. <H2 mb={4} bold>
  44. Change email address
  45. </H2>
  46. <Col alignItems="flex-start" onSubmit={onSubmit} width={1} as="form">
  47. <Flex width={1} flexDirection={["column", "row"]}>
  48. <Col mr={[0, 2]} mb={[3, 0]} flex="0 0 auto">
  49. <Text
  50. {...label("changeemailpass")}
  51. as="label"
  52. mb={[2, 3]}
  53. fontSize={[15, 16]}
  54. bold
  55. >
  56. Password:
  57. </Text>
  58. <TextInput
  59. {...password("changeemailpass")}
  60. placeholder="Password..."
  61. maxWidth="240px"
  62. required
  63. />
  64. </Col>
  65. <Col ml={[0, 2]} flex="0 0 auto">
  66. <Text
  67. {...label("changeemailaddress")}
  68. as="label"
  69. mb={[2, 3]}
  70. fontSize={[15, 16]}
  71. bold
  72. >
  73. New email address:
  74. </Text>
  75. <TextInput
  76. {...email("changeemailaddress")}
  77. placeholder="john@example.com"
  78. flex="1 1 auto"
  79. maxWidth="240px"
  80. />
  81. </Col>
  82. </Flex>
  83. <Button type="submit" color="blue" mt={[24, 3]} disabled={loading}>
  84. <Icon name={loading ? "spinner" : "refresh"} mr={2} stroke="white" />
  85. {loading ? "Sending..." : "Update"}
  86. </Button>
  87. </Col>
  88. <Text fontSize={15} color={message.color} mt={3}>
  89. {message.text}
  90. </Text>
  91. </Col>
  92. );
  93. };
  94. export default SettingsChangeEmail;