2022-03-13 22:13:36 +01:00
|
|
|
import React, { useRef, useEffect } from 'react';
|
|
|
|
import { useFormik } from 'formik';
|
|
|
|
import * as Yup from 'yup';
|
2022-03-22 13:48:20 +01:00
|
|
|
import { uuid } from 'utils/common';;
|
2022-03-13 22:13:36 +01:00
|
|
|
import Modal from 'components/Modal';
|
2022-03-18 16:40:42 +01:00
|
|
|
import { useDispatch } from 'react-redux';
|
2022-03-22 13:48:20 +01:00
|
|
|
import { newHttpRequest, newEphermalHttpRequest } from 'providers/ReduxStore/slices/collections';
|
2022-03-18 16:40:42 +01:00
|
|
|
import { addTab } from 'providers/ReduxStore/slices/tabs';
|
2022-03-22 13:48:20 +01:00
|
|
|
import HttpMethodSelector from 'components/RequestPane/QueryUrl/HttpMethodSelector';
|
|
|
|
import StyledWrapper from './StyledWrapper';
|
2022-03-13 22:13:36 +01:00
|
|
|
|
2022-03-22 13:48:20 +01:00
|
|
|
const NewRequest = ({collection, item, isEphermal, onClose}) => {
|
2022-03-18 16:40:42 +01:00
|
|
|
const dispatch = useDispatch();
|
2022-03-13 22:13:36 +01:00
|
|
|
const inputRef = useRef();
|
|
|
|
const formik = useFormik({
|
|
|
|
enableReinitialize: true,
|
|
|
|
initialValues: {
|
2022-03-22 13:48:20 +01:00
|
|
|
requestName: '',
|
|
|
|
requestType: 'http-request',
|
|
|
|
requestUrl: '',
|
|
|
|
requestMethod: 'get'
|
2022-03-13 22:13:36 +01:00
|
|
|
},
|
|
|
|
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) => {
|
2022-03-22 13:48:20 +01:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2022-03-19 10:53:20 +01:00
|
|
|
onClose();
|
2022-03-13 22:13:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if(inputRef && inputRef.current) {
|
|
|
|
inputRef.current.focus();
|
|
|
|
}
|
|
|
|
}, [inputRef]);
|
|
|
|
|
|
|
|
const onSubmit = () => formik.handleSubmit();
|
|
|
|
|
|
|
|
return (
|
2022-03-22 13:48:20 +01:00
|
|
|
<StyledWrapper>
|
|
|
|
<Modal
|
|
|
|
size="md"
|
|
|
|
title='New Request'
|
|
|
|
confirmText='Create'
|
|
|
|
handleConfirm={onSubmit}
|
|
|
|
handleCancel={onClose}
|
|
|
|
>
|
|
|
|
<form className="bruno-form" onSubmit={formik.handleSubmit}>
|
|
|
|
<div className="hidden">
|
|
|
|
<label htmlFor="requestName" className="block font-semibold">Type</label>
|
|
|
|
|
|
|
|
<div className="flex items-center mt-2">
|
|
|
|
<input
|
|
|
|
id="http-request"
|
|
|
|
className="cursor-pointer"
|
|
|
|
type="radio" name="requestType"
|
|
|
|
onChange={formik.handleChange}
|
|
|
|
value="http-request"
|
|
|
|
checked={formik.values.requestType === 'http-request'}
|
|
|
|
/>
|
|
|
|
<label htmlFor="http-request" className="ml-1 cursor-pointer select-none">Http</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
id="graphql-request"
|
|
|
|
className="ml-4 cursor-pointer"
|
|
|
|
type="radio" name="requestType"
|
|
|
|
onChange={(event) => {
|
|
|
|
formik.setFieldValue('requestMethod', 'post')
|
|
|
|
formik.handleChange(event);
|
|
|
|
}}
|
|
|
|
value="graphql-request"
|
|
|
|
checked={formik.values.requestType === 'graphql-request'}
|
|
|
|
/>
|
|
|
|
<label htmlFor="graphql-request" className="ml-1 cursor-pointer select-none">Graphql</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label htmlFor="requestName" className="block font-semibold">Name</label>
|
|
|
|
<input
|
|
|
|
id="collection-name" type="text" name="requestName"
|
|
|
|
ref={inputRef}
|
|
|
|
className="block textbox mt-2 w-full"
|
|
|
|
autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false"
|
|
|
|
onChange={formik.handleChange}
|
|
|
|
value={formik.values.requestName || ''}
|
|
|
|
/>
|
|
|
|
{formik.touched.requestName && formik.errors.requestName ? (
|
|
|
|
<div className="text-red-500">{formik.errors.requestName}</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="mt-4">
|
|
|
|
<label htmlFor="request-url" className="block font-semibold">Url</label>
|
|
|
|
|
|
|
|
<div className="flex items-center mt-2 ">
|
|
|
|
<div className="flex items-center h-full method-selector-container">
|
|
|
|
<HttpMethodSelector method={formik.values.requestMethod} onMethodSelect={(val) => formik.setFieldValue('requestMethod', val)}/>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center flex-grow input-container h-full">
|
|
|
|
<input
|
|
|
|
id="request-url" type="text" name="requestUrl"
|
|
|
|
className="px-3 w-full "
|
|
|
|
autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false"
|
|
|
|
onChange={formik.handleChange}
|
|
|
|
value={formik.values.requestUrl || ''}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{formik.touched.requestUrl && formik.errors.requestUrl ? (
|
|
|
|
<div className="text-red-500">{formik.errors.requestUrl}</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</Modal>
|
|
|
|
</StyledWrapper>
|
2022-03-13 22:13:36 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NewRequest;
|