import React, { useRef, useEffect } from 'react'; import { useFormik } from 'formik'; import * as Yup from 'yup'; import { uuid } from 'utils/common';; import Modal from 'components/Modal'; import { useDispatch } from 'react-redux'; import { newHttpRequest, newEphermalHttpRequest } from 'providers/ReduxStore/slices/collections'; import { addTab } from 'providers/ReduxStore/slices/tabs'; import HttpMethodSelector from 'components/RequestPane/QueryUrl/HttpMethodSelector'; import StyledWrapper from './StyledWrapper'; const NewRequest = ({collection, item, isEphermal, onClose}) => { const dispatch = useDispatch(); const inputRef = useRef(); const formik = useFormik({ enableReinitialize: true, initialValues: { requestName: '', requestType: 'http-request', requestUrl: '', requestMethod: 'get' }, validationSchema: Yup.object({ requestName: Yup.string() .min(1, 'must be atleast 1 characters') .max(50, 'must be 50 characters or less') .required('name is required') }), onSubmit: (values) => { if(isEphermal) { const uid = uuid(); dispatch(newEphermalHttpRequest({ uid: uid, requestName: values.requestName, requestType: values.requestType, requestUrl: values.requestUrl, requestMethod: values.requestMethod, collectionUid: collection.uid })) dispatch(addTab({ uid: uid, collectionUid: collection.uid })); } else { dispatch(newHttpRequest({ requestName: values.requestName, requestType: values.requestType, requestUrl: values.requestUrl, requestMethod: values.requestMethod, collectionUid: collection.uid, itemUid: item ? item.uid : null })) .then((action) => { dispatch(addTab({ uid: action.payload.item.uid, collectionUid: collection.uid })); }); } onClose(); } }); useEffect(() => { if(inputRef && inputRef.current) { inputRef.current.focus(); } }, [inputRef]); const onSubmit = () => formik.handleSubmit(); return (
{ formik.setFieldValue('requestMethod', 'post') formik.handleChange(event); }} value="graphql-request" checked={formik.values.requestType === 'graphql-request'} />
{formik.touched.requestName && formik.errors.requestName ? (
{formik.errors.requestName}
) : null}
formik.setFieldValue('requestMethod', val)}/>
{formik.touched.requestUrl && formik.errors.requestUrl ? (
{formik.errors.requestUrl}
) : null}
); }; export default NewRequest;