1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

235 lines
9.1 KiB
JavaScript
Raw Normal View History

2020-01-17 02:50:57 -07:00
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";
2020-01-27 21:28:27 -07:00
import HelpModal from "~/components/HelpModal";
2020-01-17 02:50:57 -07:00
import QueryLocation from "~/components/QueryLocation";
import QueryType from "~/components/QueryType";
import QueryTarget from "~/components/QueryTarget";
import QueryVrf from "~/components/QueryVrf";
2020-01-27 21:28:27 -07:00
import ResolvedTarget from "~/components/ResolvedTarget";
2020-01-17 02:50:57 -07:00
import SubmitButton from "~/components/SubmitButton";
2020-01-20 00:37:04 -07:00
import useConfig from "~/components/HyperglassProvider";
2020-01-17 02:50:57 -07:00
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 })),
2020-01-17 02:50:57 -07:00
query_type: yup
.string()
.required(config.messages.no_input.format({ field: config.web.text.query_type })),
2020-01-17 02:50:57 -07:00
query_vrf: yup.string(),
query_target: yup
.string()
.required(config.messages.no_input.format({ field: config.web.text.query_target }))
2020-01-17 02:50:57 -07:00
});
const FormRow = ({ children, ...props }) => (
2020-01-20 00:37:04 -07:00
<Flex
flexDirection="row"
flexWrap="wrap"
w="100%"
justifyContent={["center", "center", "space-between", "space-between"]}
{...props}
>
2020-01-17 02:50:57 -07:00
{children}
</Flex>
);
2020-01-20 00:37:04 -07:00
const HyperglassForm = React.forwardRef(
({ isSubmitting, setSubmitting, setFormData, ...props }, ref) => {
const config = useConfig();
2020-01-17 02:50:57 -07:00
const { handleSubmit, register, setValue, errors } = useForm({
validationSchema: formSchema(config),
defaultValues: { query_vrf: "default" }
2020-01-17 02:50:57 -07:00
});
2020-01-27 21:28:27 -07:00
2020-01-17 02:50:57 -07:00
const [queryLocation, setQueryLocation] = useState([]);
const [queryType, setQueryType] = useState("");
const [queryVrf, setQueryVrf] = useState("");
2020-01-27 21:28:27 -07:00
const [queryTarget, setQueryTarget] = useState("");
2020-01-17 02:50:57 -07:00
const [availVrfs, setAvailVrfs] = useState([]);
2020-01-27 21:28:27 -07:00
const [fqdnTarget, setFqdnTarget] = useState("");
const [displayTarget, setDisplayTarget] = useState("");
const [families, setFamilies] = useState([]);
2020-01-17 02:50:57 -07:00
const onSubmit = values => {
setFormData(values);
setSubmitting(true);
};
const handleLocChange = locObj => {
setQueryLocation(locObj.value);
const allVrfs = [];
const deviceVrfs = [];
2020-01-17 02:50:57 -07:00
locObj.value.map(loc => {
const locVrfs = [];
config.devices[loc].vrfs.map(vrf => {
locVrfs.push({
label: vrf.display_name,
value: vrf.id
});
deviceVrfs.push([{ id: vrf.id, ipv4: vrf.ipv4, ipv6: vrf.ipv6 }]);
2020-01-17 02:50:57 -07:00
});
allVrfs.push(locVrfs);
});
2020-01-17 02:50:57 -07:00
const intersecting = lodash.intersectionWith(...allVrfs, lodash.isEqual);
setAvailVrfs(intersecting);
!intersecting.includes(queryVrf) && queryVrf !== "default" && setQueryVrf("default");
let ipv4 = 0;
let ipv6 = 0;
deviceVrfs.length !== 0 &&
intersecting.length !== 0 &&
deviceVrfs
.filter(v => intersecting.every(i => i.id === v.id))
.reduce((a, b) => a.concat(b))
.filter(v => v.id === "default")
.map(v => {
v.ipv4 === true && ipv4++;
v.ipv6 === true && ipv6++;
});
if (ipv4 !== 0 && ipv4 === ipv6) {
setFamilies([4, 6]);
} else if (ipv4 > ipv6) {
setFamilies([4]);
} else if (ipv4 < ipv6) {
setFamilies([6]);
} else {
setFamilies([]);
}
2020-01-17 02:50:57 -07:00
};
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)
2020-01-27 21:28:27 -07:00
: e.field === "query_target"
? setQueryTarget(e.value)
2020-01-17 02:50:57 -07:00
: null;
};
2020-01-27 21:28:27 -07:00
const vrfContent = config.content.vrf[queryVrf]?.[queryType];
2020-01-27 21:28:27 -07:00
const validFqdnQueryType =
["ping", "traceroute", "bgp_route"].includes(queryType) &&
fqdnTarget &&
queryVrf === "default"
? fqdnTarget
: null;
2020-01-17 02:50:57 -07:00
useEffect(() => {
register({ name: "query_location" });
2020-01-27 21:28:27 -07:00
register({ name: "query_target" });
2020-01-17 02:50:57 -07:00
register({ name: "query_type" });
register({ name: "query_vrf" });
});
return (
<Box
2020-01-27 21:28:27 -07:00
maxW={["100%", "100%", "75%", "75%"]}
2020-01-17 02:50:57 -07:00
w="100%"
p={0}
mx="auto"
my={4}
textAlign="left"
ref={ref}
{...props}
>
<form onSubmit={handleSubmit(onSubmit)}>
<FormRow>
<FormField
label={config.web.text.query_location}
2020-01-17 02:50:57 -07:00
name="query_location"
error={errors.query_location}
>
<QueryLocation onChange={handleChange} locations={config.networks} />
</FormField>
<FormField
label={config.web.text.query_type}
2020-01-17 02:50:57 -07:00
name="query_type"
error={errors.query_type}
2020-01-27 21:28:27 -07:00
labelAddOn={
vrfContent && <HelpModal item={vrfContent} name="query_type" />
}
2020-01-17 02:50:57 -07:00
>
2020-01-28 12:03:47 -07:00
<QueryType onChange={handleChange} queryTypes={config.queries.list} />
2020-01-17 02:50:57 -07:00
</FormField>
</FormRow>
<FormRow>
2020-03-22 11:43:14 -07:00
{availVrfs.length > 1 && (
2020-01-17 02:50:57 -07:00
<FormField
label={config.web.text.query_vrf}
2020-01-17 02:50:57 -07:00
name="query_vrf"
error={errors.query_vrf}
>
<QueryVrf
placeholder={config.web.text.query_vrf}
2020-01-17 02:50:57 -07:00
vrfs={availVrfs}
onChange={handleChange}
/>
</FormField>
)}
<FormField
label={config.web.text.query_target}
2020-01-17 02:50:57 -07:00
name="query_target"
error={errors.query_target}
2020-01-27 21:28:27 -07:00
fieldAddOn={
queryLocation.length !== 0 &&
2020-01-27 21:28:27 -07:00
validFqdnQueryType && (
<ResolvedTarget
2020-01-28 18:42:28 -10:00
queryTarget={queryTarget}
fqdnTarget={validFqdnQueryType}
2020-01-27 21:28:27 -07:00
setTarget={handleChange}
families={families}
2020-01-27 21:28:27 -07:00
/>
)
}
2020-01-17 02:50:57 -07:00
>
<QueryTarget
2020-01-27 21:28:27 -07:00
name="query_target"
placeholder={config.web.text.query_target}
2020-01-17 02:50:57 -07:00
register={register}
2020-01-27 21:28:27 -07:00
resolveTarget={["ping", "traceroute", "bgp_route"].includes(
queryType
)}
value={queryTarget}
setFqdn={setFqdnTarget}
2020-01-28 18:42:28 -10:00
setTarget={handleChange}
2020-01-27 21:28:27 -07:00
displayValue={displayTarget}
setDisplayValue={setDisplayTarget}
2020-01-17 02:50:57 -07:00
/>
</FormField>
2020-01-20 00:37:04 -07:00
</FormRow>
<FormRow mt={0} justifyContent="flex-end">
<Flex
w="100%"
maxW="100%"
ml="auto"
my={2}
mr={[0, 0, 2, 2]}
flexDirection="column"
flex="0 0 0"
2020-01-17 02:50:57 -07:00
>
<SubmitButton isLoading={isSubmitting} />
2020-01-20 00:37:04 -07:00
</Flex>
2020-01-17 02:50:57 -07:00
</FormRow>
</form>
</Box>
);
}
);
2020-01-20 00:37:04 -07:00
HyperglassForm.displayName = "HyperglassForm";
export default HyperglassForm;