SettingsDomain.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, { FC, useState } from "react";
  2. import styled from "styled-components";
  3. import { Flex } from "reflexbox/styled-components";
  4. import { useStoreState, useStoreActions } from "../../store";
  5. import { useFormState } from "react-use-form-state";
  6. import { useMessage } from "../../hooks";
  7. import TextInput from "../TextInput";
  8. import Button from "../Button";
  9. import Text from "../Text";
  10. // TODO: types
  11. interface Props {
  12. settings: {
  13. customDomain: string;
  14. domainInput: boolean;
  15. homepage: string;
  16. };
  17. handleCustomDomain: any;
  18. loading: boolean;
  19. showDomainInput: any;
  20. showModal: any;
  21. }
  22. const ButtonWrapper = styled(Flex).attrs({
  23. justifyContent: ["column", "column", "row"],
  24. alignItems: ["flex-start", "flex-start", "center"],
  25. my: 32
  26. })`
  27. display: flex;
  28. button {
  29. margin-right: 16px;
  30. }
  31. @media only screen and (max-width: 768px) {
  32. > * {
  33. margin: 8px 0;
  34. }
  35. }
  36. `;
  37. const Domain = styled.h4`
  38. margin: 0 16px 0 0;
  39. font-size: 20px;
  40. font-weight: bold;
  41. span {
  42. border-bottom: 2px dotted #999;
  43. }
  44. `;
  45. const Homepage = styled.h6`
  46. margin: 0 16px 0 0;
  47. font-size: 14px;
  48. font-weight: 300;
  49. span {
  50. border-bottom: 2px dotted #999;
  51. }
  52. `;
  53. const SettingsDomain: FC<Props> = ({ showDomainInput, showModal }) => {
  54. const [loading, setLoading] = useState(false);
  55. const [message, setMessage] = useMessage(2000);
  56. const domains = useStoreState(s => s.settings.domains);
  57. const { saveDomain } = useStoreActions(s => s.settings);
  58. const [formState, { text }] = useFormState<{
  59. customDomain: string;
  60. homepage: string;
  61. }>();
  62. const onSubmit = async e => {
  63. e.preventDefault();
  64. setLoading(true);
  65. try {
  66. await saveDomain(formState.values);
  67. } catch (err) {
  68. setMessage(err?.response?.data?.error || "Couldn't add domain.");
  69. }
  70. setLoading(false);
  71. };
  72. return (
  73. <Flex alignItems="flex-start" flexDirection="column">
  74. <Text as="h2" fontWeight={700} mb={4}>
  75. Custom domain
  76. </Text>
  77. <Text as="p" mb={3}>
  78. You can set a custom domain for your short URLs, so instead of{" "}
  79. <b>kutt.it/shorturl</b> you can have <b>example.com/shorturl.</b>
  80. </Text>
  81. <Text mb={4}>
  82. Point your domain A record to <b>192.64.116.170</b> then add the domain
  83. via form below:
  84. </Text>
  85. {domains.length ? (
  86. domains.map(d => (
  87. <Flex key={d.customDomain}>
  88. <Flex alignItems="center">
  89. <Domain>
  90. <span>{d.customDomain}</span>
  91. </Domain>
  92. <Homepage>
  93. (Homepage redirects to{" "}
  94. <span>{d.homepage || window.location.hostname}</span>)
  95. </Homepage>
  96. </Flex>
  97. <ButtonWrapper>
  98. <Button icon="edit" onClick={showDomainInput}>
  99. Change
  100. </Button>
  101. <Button color="gray" icon="x" onClick={showModal}>
  102. Delete
  103. </Button>
  104. </ButtonWrapper>
  105. </Flex>
  106. ))
  107. ) : (
  108. <Flex
  109. alignItems="flex-start"
  110. flexDirection="column"
  111. onSubmit={onSubmit}
  112. width={1}
  113. as="form"
  114. my={4}
  115. >
  116. <Flex width={1}>
  117. <Flex flexDirection="column" mr={2} flex="1 1 auto">
  118. <Text as="label" htmlFor="customdomain" fontWeight={700} mb={3}>
  119. Domain
  120. </Text>
  121. <TextInput
  122. {...text("customDomain")}
  123. placeholder="example.com"
  124. height={44}
  125. pl={24}
  126. pr={24}
  127. required
  128. />
  129. </Flex>
  130. <Flex flexDirection="column" ml={2} flex="1 1 auto">
  131. <Text as="label" htmlFor="customdomain" fontWeight={700} mb={3}>
  132. Homepage (optional)
  133. </Text>
  134. <TextInput
  135. {...text("homepage")}
  136. type="text"
  137. placeholder="Homepage URL"
  138. flex="1 1 auto"
  139. height={44}
  140. pl={24}
  141. pr={24}
  142. />
  143. </Flex>
  144. </Flex>
  145. <Button
  146. type="submit"
  147. color="purple"
  148. icon={loading ? "loader" : ""}
  149. mt={3}
  150. >
  151. Set domain
  152. </Button>
  153. </Flex>
  154. )}
  155. <Text color={message.color}>{message.text}</Text>
  156. </Flex>
  157. );
  158. };
  159. export default SettingsDomain;