SettingsDomain.tsx 5.8 KB

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