| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- import { IconSettings } from "@tabler/icons-react";
- import cn from "classnames";
- import { Field, Form, Formik } from "formik";
- import { useState } from "react";
- import { Alert } from "react-bootstrap";
- import Modal from "react-bootstrap/Modal";
- import {
- Button,
- DomainNamesField,
- Loading,
- NginxConfigField,
- SSLCertificateField,
- SSLOptionsFields,
- } from "src/components";
- import { useRedirectionHost, useSetRedirectionHost } from "src/hooks";
- import { intl } from "src/locale";
- import { validateString } from "src/modules/Validations";
- import { showSuccess } from "src/notifications";
- interface Props {
- id: number | "new";
- onClose: () => void;
- }
- export function RedirectionHostModal({ id, onClose }: Props) {
- const { data, isLoading, error } = useRedirectionHost(id);
- const { mutate: setRedirectionHost } = useSetRedirectionHost();
- const [errorMsg, setErrorMsg] = useState<string | null>(null);
- const [isSubmitting, setIsSubmitting] = useState(false);
- const onSubmit = async (values: any, { setSubmitting }: any) => {
- if (isSubmitting) return;
- setIsSubmitting(true);
- setErrorMsg(null);
- const { ...payload } = {
- id: id === "new" ? undefined : id,
- ...values,
- };
- setRedirectionHost(payload, {
- onError: (err: any) => setErrorMsg(err.message),
- onSuccess: () => {
- showSuccess(intl.formatMessage({ id: "notification.redirection-host-saved" }));
- onClose();
- },
- onSettled: () => {
- setIsSubmitting(false);
- setSubmitting(false);
- },
- });
- };
- return (
- <Modal show onHide={onClose} animation={false}>
- {!isLoading && error && (
- <Alert variant="danger" className="m-3">
- {error?.message || "Unknown error"}
- </Alert>
- )}
- {isLoading && <Loading noLogo />}
- {!isLoading && data && (
- <Formik
- initialValues={
- {
- // Details tab
- domainNames: data?.domainNames || [],
- forwardDomainName: data?.forwardDomainName || "",
- forwardScheme: data?.forwardScheme || "auto",
- forwardHttpCode: data?.forwardHttpCode || 301,
- preservePath: data?.preservePath || false,
- blockExploits: data?.blockExploits || false,
- // SSL tab
- certificateId: data?.certificateId || 0,
- sslForced: data?.sslForced || false,
- http2Support: data?.http2Support || false,
- hstsEnabled: data?.hstsEnabled || false,
- hstsSubdomains: data?.hstsSubdomains || false,
- // Advanced tab
- advancedConfig: data?.advancedConfig || "",
- meta: data?.meta || {},
- } as any
- }
- onSubmit={onSubmit}
- >
- {() => (
- <Form>
- <Modal.Header closeButton>
- <Modal.Title>
- {intl.formatMessage({
- id: data?.id ? "redirection-host.edit" : "redirection-host.new",
- })}
- </Modal.Title>
- </Modal.Header>
- <Modal.Body className="p-0">
- <Alert variant="danger" show={!!errorMsg} onClose={() => setErrorMsg(null)} dismissible>
- {errorMsg}
- </Alert>
- <div className="card m-0 border-0">
- <div className="card-header">
- <ul className="nav nav-tabs card-header-tabs" data-bs-toggle="tabs">
- <li className="nav-item" role="presentation">
- <a
- href="#tab-details"
- className="nav-link active"
- data-bs-toggle="tab"
- aria-selected="true"
- role="tab"
- >
- {intl.formatMessage({ id: "column.details" })}
- </a>
- </li>
- <li className="nav-item" role="presentation">
- <a
- href="#tab-ssl"
- className="nav-link"
- data-bs-toggle="tab"
- aria-selected="false"
- tabIndex={-1}
- role="tab"
- >
- {intl.formatMessage({ id: "column.ssl" })}
- </a>
- </li>
- <li className="nav-item ms-auto" role="presentation">
- <a
- href="#tab-advanced"
- className="nav-link"
- title="Settings"
- data-bs-toggle="tab"
- aria-selected="false"
- tabIndex={-1}
- role="tab"
- >
- <IconSettings size={20} />
- </a>
- </li>
- </ul>
- </div>
- <div className="card-body">
- <div className="tab-content">
- <div className="tab-pane active show" id="tab-details" role="tabpanel">
- <DomainNamesField isWildcardPermitted />
- <div className="row">
- <div className="col-md-4">
- <Field name="forwardScheme">
- {({ field, form }: any) => (
- <div className="mb-3">
- <label
- className="form-label"
- htmlFor="forwardScheme"
- >
- {intl.formatMessage({
- id: "host.forward-scheme",
- })}
- </label>
- <select
- id="forwardScheme"
- className={`form-control ${form.errors.forwardScheme && form.touched.forwardScheme ? "is-invalid" : ""}`}
- required
- {...field}
- >
- <option value="$scheme">Auto</option>
- <option value="http">http</option>
- <option value="https">https</option>
- </select>
- {form.errors.forwardScheme ? (
- <div className="invalid-feedback">
- {form.errors.forwardScheme &&
- form.touched.forwardScheme
- ? form.errors.forwardScheme
- : null}
- </div>
- ) : null}
- </div>
- )}
- </Field>
- </div>
- <div className="col-md-8">
- <Field
- name="forwardDomainName"
- validate={validateString(1, 255)}
- >
- {({ field, form }: any) => (
- <div className="mb-3">
- <label
- className="form-label"
- htmlFor="forwardDomainName"
- >
- {intl.formatMessage({
- id: "redirection-host.forward-domain",
- })}
- </label>
- <input
- id="forwardDomainName"
- type="text"
- className={`form-control ${form.errors.forwardDomainName && form.touched.forwardDomainName ? "is-invalid" : ""}`}
- required
- placeholder="example.com"
- {...field}
- />
- {form.errors.forwardDomainName ? (
- <div className="invalid-feedback">
- {form.errors.forwardDomainName &&
- form.touched.forwardDomainName
- ? form.errors.forwardDomainName
- : null}
- </div>
- ) : null}
- </div>
- )}
- </Field>
- </div>
- </div>
- <div className="my-3">
- <h4 className="py-2">
- {intl.formatMessage({ id: "host.flags.title" })}
- </h4>
- <div className="divide-y">
- <div>
- <label className="row" htmlFor="preservePath">
- <span className="col">
- {intl.formatMessage({
- id: "host.flags.preserve-path",
- })}
- </span>
- <span className="col-auto">
- <Field name="preservePath" type="checkbox">
- {({ field }: any) => (
- <label className="form-check form-check-single form-switch">
- <input
- {...field}
- id="preservePath"
- className={cn("form-check-input", {
- "bg-yellow": field.checked,
- })}
- type="checkbox"
- />
- </label>
- )}
- </Field>
- </span>
- </label>
- </div>
- <div>
- <label className="row" htmlFor="blockExploits">
- <span className="col">
- {intl.formatMessage({
- id: "host.flags.block-exploits",
- })}
- </span>
- <span className="col-auto">
- <Field name="blockExploits" type="checkbox">
- {({ field }: any) => (
- <label className="form-check form-check-single form-switch">
- <input
- {...field}
- id="blockExploits"
- className={cn("form-check-input", {
- "bg-yellow": field.checked,
- })}
- type="checkbox"
- />
- </label>
- )}
- </Field>
- </span>
- </label>
- </div>
- </div>
- </div>
- </div>
- <div className="tab-pane" id="tab-ssl" role="tabpanel">
- <SSLCertificateField
- name="certificateId"
- label="ssl-certificate"
- allowNew
- />
- <SSLOptionsFields color="bg-yellow" />
- </div>
- <div className="tab-pane" id="tab-advanced" role="tabpanel">
- <NginxConfigField />
- </div>
- </div>
- </div>
- </div>
- </Modal.Body>
- <Modal.Footer>
- <Button data-bs-dismiss="modal" onClick={onClose} disabled={isSubmitting}>
- {intl.formatMessage({ id: "cancel" })}
- </Button>
- <Button
- type="submit"
- actionType="primary"
- className="ms-auto bg-yellow"
- data-bs-dismiss="modal"
- isLoading={isSubmitting}
- disabled={isSubmitting}
- >
- {intl.formatMessage({ id: "save" })}
- </Button>
- </Modal.Footer>
- </Form>
- )}
- </Formik>
- )}
- </Modal>
- );
- }
|