import React, { useState, useEffect } from "react"; import { Box, Flex } from "@chakra-ui/core"; import { useForm } from "react-hook-form"; import lodash from "lodash"; import * as yup from "yup"; import format from "string-format"; import FormField from "~/components/FormField"; import HelpModal from "~/components/HelpModal"; import QueryLocation from "~/components/QueryLocation"; import QueryType from "~/components/QueryType"; import QueryTarget from "~/components/QueryTarget"; import QueryVrf from "~/components/QueryVrf"; import ResolvedTarget from "~/components/ResolvedTarget"; import SubmitButton from "~/components/SubmitButton"; import useConfig from "~/components/HyperglassProvider"; format.extend(String.prototype, {}); const formSchema = config => yup.object().shape({ query_location: yup .array() .of(yup.string()) .required(config.messages.no_input.format({ field: config.web.text.query_location })), query_type: yup .string() .required(config.messages.no_input.format({ field: config.web.text.query_type })), query_vrf: yup.string(), query_target: yup .string() .required(config.messages.no_input.format({ field: config.web.text.query_target })) }); const FormRow = ({ children, ...props }) => ( {children} ); const HyperglassForm = React.forwardRef( ({ isSubmitting, setSubmitting, setFormData, ...props }, ref) => { const config = useConfig(); const { handleSubmit, register, setValue, errors } = useForm({ validationSchema: formSchema(config) }); const [queryLocation, setQueryLocation] = useState([]); const [queryType, setQueryType] = useState(""); const [queryVrf, setQueryVrf] = useState(""); const [queryTarget, setQueryTarget] = useState(""); const [availVrfs, setAvailVrfs] = useState([]); const [fqdnTarget, setFqdnTarget] = useState(""); const [displayTarget, setDisplayTarget] = useState(""); const onSubmit = values => { console.log(values); setFormData(values); setSubmitting(true); }; const handleLocChange = locObj => { setQueryLocation(locObj.value); const allVrfs = []; locObj.value.map(loc => { const locVrfs = []; config.devices[loc].vrfs.map(vrf => { locVrfs.push({ label: vrf.display_name, value: vrf.id }); }); allVrfs.push(locVrfs); }); const intersecting = lodash.intersectionWith(...allVrfs, lodash.isEqual); setAvailVrfs(intersecting); !intersecting.includes(queryVrf) && setQueryVrf(""); }; const handleChange = e => { setValue(e.field, e.value); e.field === "query_location" ? handleLocChange(e) : e.field === "query_type" ? setQueryType(e.value) : e.field === "query_vrf" ? setQueryVrf(e.value) : e.field === "query_target" ? setQueryTarget(e.value) : null; }; const vrfContent = config.content.vrf[queryVrf]?.[queryType]; const validFqdnQueryType = ["ping", "traceroute", "bgp_route"].includes(queryType) && fqdnTarget && queryVrf === "default" ? fqdnTarget : null; useEffect(() => { register({ name: "query_location" }); register({ name: "query_target" }); register({ name: "query_type" }); register({ name: "query_vrf" }); }); return (
} > {availVrfs.length > 0 && ( )} ) } >
); } ); HyperglassForm.displayName = "HyperglassForm"; export default HyperglassForm;