import { Form, Formik, FormikHelpers } from 'formik'; import React, { useEffect, useState } from 'react'; import tw from 'twin.macro'; import { array, number, object, string } from 'yup'; import getAllocations from '@/api/admin/nodes/getAllocations'; import Button from '@/components/elements/Button'; import SelectField, { Option } from '@/components/elements/SelectField'; interface Values { ips: string[]; ports: number[]; } const distinct = (value: any, index: any, self: any) => { return self.indexOf(value) === index; }; function CreateAllocationForm ({ nodeId }: { nodeId: string | number }) { const [ ips, setIPs ] = useState([]); const [ ports ] = useState([]); useEffect(() => { getAllocations(nodeId) .then(allocations => { setIPs(allocations.map(a => a.ip).filter(distinct).map(ip => { return { value: ip, label: ip }; })); }); }, [ nodeId ]); const isValidIP = (inputValue: string): boolean => { // TODO: Better way of checking for a valid ip (and CIDR). return inputValue.match(/^([0-9a-f.:/]+)$/) !== null; }; const isValidPort = (inputValue: string): boolean => { // TODO: Better way of checking for a valid port (and port range) return inputValue.match(/^([0-9-]+)$/) !== null; }; const submit = (values: Values, { setSubmitting }: FormikHelpers) => { setSubmitting(false); }; return ( { ({ isSubmitting, isValid }) => (
) }
); } export default CreateAllocationForm;