SettingsDomain.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { useFormState } from "react-use-form-state";
  2. import { Flex } from "reflexbox/styled-components";
  3. import React, { FC, useState } from "react";
  4. import styled from "styled-components";
  5. import { useStoreState, useStoreActions } from "../../store";
  6. import { Domain } from "../../store/settings";
  7. import { useMessage } from "../../hooks";
  8. import Text, { H2, Span } from "../Text";
  9. import { Colors } from "../../consts";
  10. import { TextInput } from "../Input";
  11. import { Button } from "../Button";
  12. import { Col } from "../Layout";
  13. import Table from "../Table";
  14. import Modal from "../Modal";
  15. import Icon from "../Icon";
  16. import { errorMessage } from "../../utils";
  17. const Th = styled(Flex).attrs({ as: "th", py: 3, px: 3 })`
  18. font-size: 15px;
  19. `;
  20. const Td = styled(Flex).attrs({ as: "td", py: 12, px: 3 })`
  21. font-size: 15px;
  22. `;
  23. const SettingsDomain: FC = () => {
  24. const [modal, setModal] = useState(false);
  25. const [loading, setLoading] = useState(false);
  26. const [deleteLoading, setDeleteLoading] = useState(false);
  27. const [domainToDelete, setDomainToDelete] = useState<Domain>(null);
  28. const [message, setMessage] = useMessage(2000);
  29. const domains = useStoreState(s => s.settings.domains);
  30. const { saveDomain, deleteDomain } = useStoreActions(s => s.settings);
  31. const [formState, { label, text }] = useFormState<{
  32. customDomain: string;
  33. homepage: string;
  34. }>(null, { withIds: true });
  35. const onSubmit = async e => {
  36. e.preventDefault();
  37. setLoading(true);
  38. try {
  39. await saveDomain(formState.values);
  40. } catch (err) {
  41. setMessage(err?.response?.data?.error || "Couldn't add domain.");
  42. }
  43. formState.clear();
  44. setLoading(false);
  45. };
  46. const closeModal = () => {
  47. setDomainToDelete(null);
  48. setModal(false);
  49. };
  50. const onDelete = async () => {
  51. setDeleteLoading(true);
  52. await deleteDomain().catch(err =>
  53. setMessage(errorMessage(err, "Couldn't delete the domain."))
  54. );
  55. setMessage("Domain has been deleted successfully.", "green");
  56. closeModal();
  57. setDeleteLoading(false);
  58. };
  59. return (
  60. <Col alignItems="flex-start" maxWidth="100%">
  61. <H2 mb={4} bold>
  62. Custom domain
  63. </H2>
  64. <Text mb={3}>
  65. You can set a custom domain for your short URLs, so instead of{" "}
  66. <b>kutt.it/shorturl</b> you can have <b>example.com/shorturl.</b>
  67. </Text>
  68. <Text mb={4}>
  69. Point your domain A record to <b>192.64.116.170</b> then add the domain
  70. via form below:
  71. </Text>
  72. {domains.length ? (
  73. <Table my={3} scrollWidth="550px">
  74. <thead>
  75. <tr>
  76. <Th width={2 / 5}>Domain</Th>
  77. <Th width={2 / 5}>Homepage</Th>
  78. <Th width={1 / 5}></Th>
  79. </tr>
  80. </thead>
  81. <tbody>
  82. {domains.map(d => (
  83. <tr key={d.customDomain}>
  84. <Td width={2 / 5}>{d.customDomain}</Td>
  85. <Td width={2 / 5}>{d.homepage || "default"}</Td>
  86. <Td width={1 / 5} justifyContent="center">
  87. <Icon
  88. as="button"
  89. name="trash"
  90. stroke={Colors.TrashIcon}
  91. strokeWidth="2.5"
  92. backgroundColor={Colors.TrashIconBg}
  93. py={0}
  94. px={0}
  95. size={[23, 24]}
  96. p={["4px", "5px"]}
  97. onClick={() => {
  98. setDomainToDelete(d);
  99. setModal(true);
  100. }}
  101. />
  102. </Td>
  103. </tr>
  104. ))}
  105. </tbody>
  106. </Table>
  107. ) : (
  108. <Col
  109. alignItems="flex-start"
  110. onSubmit={onSubmit}
  111. width={1}
  112. as="form"
  113. my={[3, 4]}
  114. >
  115. <Flex width={1} flexDirection={["column", "row"]}>
  116. <Col mr={[0, 2]} mb={[3, 0]} flex="0 0 auto">
  117. <Text
  118. {...label("customDomain")}
  119. as="label"
  120. mb={[2, 3]}
  121. fontSize={[15, 16]}
  122. bold
  123. >
  124. Domain
  125. </Text>
  126. <TextInput
  127. {...text("customDomain")}
  128. placeholder="example.com"
  129. maxWidth="240px"
  130. required
  131. />
  132. </Col>
  133. <Col ml={[0, 2]} flex="0 0 auto">
  134. <Text
  135. {...label("homepage")}
  136. as="label"
  137. mb={[2, 3]}
  138. fontSize={[15, 16]}
  139. bold
  140. >
  141. Homepage (optional)
  142. </Text>
  143. <TextInput
  144. {...text("homepage")}
  145. placeholder="Homepage URL"
  146. flex="1 1 auto"
  147. maxWidth="240px"
  148. />
  149. </Col>
  150. </Flex>
  151. <Button type="submit" color="purple" mt={[24, 3]} disabled={loading}>
  152. <Icon name={loading ? "spinner" : "plus"} mr={2} stroke="white" />
  153. {loading ? "Setting..." : "Set domain"}
  154. </Button>
  155. </Col>
  156. )}
  157. <Text color={message.color}>{message.text}</Text>
  158. <Modal id="delete-custom-domain" show={modal} closeHandler={closeModal}>
  159. <H2 mb={24} textAlign="center" bold>
  160. Delete domain?
  161. </H2>
  162. <Text textAlign="center">
  163. Are you sure do you want to delete the domain{" "}
  164. <Span bold>"{domainToDelete && domainToDelete.customDomain}"</Span>?
  165. </Text>
  166. <Flex justifyContent="center" mt={44}>
  167. {deleteLoading ? (
  168. <>
  169. <Icon name="spinner" size={20} stroke={Colors.Spinner} />
  170. </>
  171. ) : (
  172. <>
  173. <Button color="gray" mr={3} onClick={closeModal}>
  174. Cancel
  175. </Button>
  176. <Button color="red" ml={3} onClick={onDelete}>
  177. <Icon name="trash" stroke="white" mr={2} />
  178. Delete
  179. </Button>
  180. </>
  181. )}
  182. </Flex>
  183. </Modal>
  184. </Col>
  185. );
  186. };
  187. export default SettingsDomain;