RedirectionHostModal.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { IconSettings } from "@tabler/icons-react";
  2. import cn from "classnames";
  3. import { Field, Form, Formik } from "formik";
  4. import { useState } from "react";
  5. import { Alert } from "react-bootstrap";
  6. import Modal from "react-bootstrap/Modal";
  7. import {
  8. Button,
  9. DomainNamesField,
  10. Loading,
  11. NginxConfigField,
  12. SSLCertificateField,
  13. SSLOptionsFields,
  14. } from "src/components";
  15. import { useRedirectionHost, useSetRedirectionHost } from "src/hooks";
  16. import { intl } from "src/locale";
  17. import { validateString } from "src/modules/Validations";
  18. import { showSuccess } from "src/notifications";
  19. interface Props {
  20. id: number | "new";
  21. onClose: () => void;
  22. }
  23. export function RedirectionHostModal({ id, onClose }: Props) {
  24. const { data, isLoading, error } = useRedirectionHost(id);
  25. const { mutate: setRedirectionHost } = useSetRedirectionHost();
  26. const [errorMsg, setErrorMsg] = useState<string | null>(null);
  27. const [isSubmitting, setIsSubmitting] = useState(false);
  28. const onSubmit = async (values: any, { setSubmitting }: any) => {
  29. if (isSubmitting) return;
  30. setIsSubmitting(true);
  31. setErrorMsg(null);
  32. const { ...payload } = {
  33. id: id === "new" ? undefined : id,
  34. ...values,
  35. };
  36. setRedirectionHost(payload, {
  37. onError: (err: any) => setErrorMsg(err.message),
  38. onSuccess: () => {
  39. showSuccess(intl.formatMessage({ id: "notification.redirection-host-saved" }));
  40. onClose();
  41. },
  42. onSettled: () => {
  43. setIsSubmitting(false);
  44. setSubmitting(false);
  45. },
  46. });
  47. };
  48. return (
  49. <Modal show onHide={onClose} animation={false}>
  50. {!isLoading && error && (
  51. <Alert variant="danger" className="m-3">
  52. {error?.message || "Unknown error"}
  53. </Alert>
  54. )}
  55. {isLoading && <Loading noLogo />}
  56. {!isLoading && data && (
  57. <Formik
  58. initialValues={
  59. {
  60. // Details tab
  61. domainNames: data?.domainNames || [],
  62. forwardDomainName: data?.forwardDomainName || "",
  63. forwardScheme: data?.forwardScheme || "auto",
  64. forwardHttpCode: data?.forwardHttpCode || 301,
  65. preservePath: data?.preservePath || false,
  66. blockExploits: data?.blockExploits || false,
  67. // SSL tab
  68. certificateId: data?.certificateId || 0,
  69. sslForced: data?.sslForced || false,
  70. http2Support: data?.http2Support || false,
  71. hstsEnabled: data?.hstsEnabled || false,
  72. hstsSubdomains: data?.hstsSubdomains || false,
  73. // Advanced tab
  74. advancedConfig: data?.advancedConfig || "",
  75. meta: data?.meta || {},
  76. } as any
  77. }
  78. onSubmit={onSubmit}
  79. >
  80. {() => (
  81. <Form>
  82. <Modal.Header closeButton>
  83. <Modal.Title>
  84. {intl.formatMessage({
  85. id: data?.id ? "redirection-host.edit" : "redirection-host.new",
  86. })}
  87. </Modal.Title>
  88. </Modal.Header>
  89. <Modal.Body className="p-0">
  90. <Alert variant="danger" show={!!errorMsg} onClose={() => setErrorMsg(null)} dismissible>
  91. {errorMsg}
  92. </Alert>
  93. <div className="card m-0 border-0">
  94. <div className="card-header">
  95. <ul className="nav nav-tabs card-header-tabs" data-bs-toggle="tabs">
  96. <li className="nav-item" role="presentation">
  97. <a
  98. href="#tab-details"
  99. className="nav-link active"
  100. data-bs-toggle="tab"
  101. aria-selected="true"
  102. role="tab"
  103. >
  104. {intl.formatMessage({ id: "column.details" })}
  105. </a>
  106. </li>
  107. <li className="nav-item" role="presentation">
  108. <a
  109. href="#tab-ssl"
  110. className="nav-link"
  111. data-bs-toggle="tab"
  112. aria-selected="false"
  113. tabIndex={-1}
  114. role="tab"
  115. >
  116. {intl.formatMessage({ id: "column.ssl" })}
  117. </a>
  118. </li>
  119. <li className="nav-item ms-auto" role="presentation">
  120. <a
  121. href="#tab-advanced"
  122. className="nav-link"
  123. title="Settings"
  124. data-bs-toggle="tab"
  125. aria-selected="false"
  126. tabIndex={-1}
  127. role="tab"
  128. >
  129. <IconSettings size={20} />
  130. </a>
  131. </li>
  132. </ul>
  133. </div>
  134. <div className="card-body">
  135. <div className="tab-content">
  136. <div className="tab-pane active show" id="tab-details" role="tabpanel">
  137. <DomainNamesField isWildcardPermitted />
  138. <div className="row">
  139. <div className="col-md-4">
  140. <Field name="forwardScheme">
  141. {({ field, form }: any) => (
  142. <div className="mb-3">
  143. <label
  144. className="form-label"
  145. htmlFor="forwardScheme"
  146. >
  147. {intl.formatMessage({
  148. id: "host.forward-scheme",
  149. })}
  150. </label>
  151. <select
  152. id="forwardScheme"
  153. className={`form-control ${form.errors.forwardScheme && form.touched.forwardScheme ? "is-invalid" : ""}`}
  154. required
  155. {...field}
  156. >
  157. <option value="$scheme">Auto</option>
  158. <option value="http">http</option>
  159. <option value="https">https</option>
  160. </select>
  161. {form.errors.forwardScheme ? (
  162. <div className="invalid-feedback">
  163. {form.errors.forwardScheme &&
  164. form.touched.forwardScheme
  165. ? form.errors.forwardScheme
  166. : null}
  167. </div>
  168. ) : null}
  169. </div>
  170. )}
  171. </Field>
  172. </div>
  173. <div className="col-md-8">
  174. <Field
  175. name="forwardDomainName"
  176. validate={validateString(1, 255)}
  177. >
  178. {({ field, form }: any) => (
  179. <div className="mb-3">
  180. <label
  181. className="form-label"
  182. htmlFor="forwardDomainName"
  183. >
  184. {intl.formatMessage({
  185. id: "redirection-host.forward-domain",
  186. })}
  187. </label>
  188. <input
  189. id="forwardDomainName"
  190. type="text"
  191. className={`form-control ${form.errors.forwardDomainName && form.touched.forwardDomainName ? "is-invalid" : ""}`}
  192. required
  193. placeholder="example.com"
  194. {...field}
  195. />
  196. {form.errors.forwardDomainName ? (
  197. <div className="invalid-feedback">
  198. {form.errors.forwardDomainName &&
  199. form.touched.forwardDomainName
  200. ? form.errors.forwardDomainName
  201. : null}
  202. </div>
  203. ) : null}
  204. </div>
  205. )}
  206. </Field>
  207. </div>
  208. </div>
  209. <div className="my-3">
  210. <h4 className="py-2">
  211. {intl.formatMessage({ id: "host.flags.title" })}
  212. </h4>
  213. <div className="divide-y">
  214. <div>
  215. <label className="row" htmlFor="preservePath">
  216. <span className="col">
  217. {intl.formatMessage({
  218. id: "host.flags.preserve-path",
  219. })}
  220. </span>
  221. <span className="col-auto">
  222. <Field name="preservePath" type="checkbox">
  223. {({ field }: any) => (
  224. <label className="form-check form-check-single form-switch">
  225. <input
  226. {...field}
  227. id="preservePath"
  228. className={cn("form-check-input", {
  229. "bg-yellow": field.checked,
  230. })}
  231. type="checkbox"
  232. />
  233. </label>
  234. )}
  235. </Field>
  236. </span>
  237. </label>
  238. </div>
  239. <div>
  240. <label className="row" htmlFor="blockExploits">
  241. <span className="col">
  242. {intl.formatMessage({
  243. id: "host.flags.block-exploits",
  244. })}
  245. </span>
  246. <span className="col-auto">
  247. <Field name="blockExploits" type="checkbox">
  248. {({ field }: any) => (
  249. <label className="form-check form-check-single form-switch">
  250. <input
  251. {...field}
  252. id="blockExploits"
  253. className={cn("form-check-input", {
  254. "bg-yellow": field.checked,
  255. })}
  256. type="checkbox"
  257. />
  258. </label>
  259. )}
  260. </Field>
  261. </span>
  262. </label>
  263. </div>
  264. </div>
  265. </div>
  266. </div>
  267. <div className="tab-pane" id="tab-ssl" role="tabpanel">
  268. <SSLCertificateField
  269. name="certificateId"
  270. label="ssl-certificate"
  271. allowNew
  272. />
  273. <SSLOptionsFields color="bg-yellow" />
  274. </div>
  275. <div className="tab-pane" id="tab-advanced" role="tabpanel">
  276. <NginxConfigField />
  277. </div>
  278. </div>
  279. </div>
  280. </div>
  281. </Modal.Body>
  282. <Modal.Footer>
  283. <Button data-bs-dismiss="modal" onClick={onClose} disabled={isSubmitting}>
  284. {intl.formatMessage({ id: "cancel" })}
  285. </Button>
  286. <Button
  287. type="submit"
  288. actionType="primary"
  289. className="ms-auto bg-yellow"
  290. data-bs-dismiss="modal"
  291. isLoading={isSubmitting}
  292. disabled={isSubmitting}
  293. >
  294. {intl.formatMessage({ id: "save" })}
  295. </Button>
  296. </Modal.Footer>
  297. </Form>
  298. )}
  299. </Formik>
  300. )}
  301. </Modal>
  302. );
  303. }