mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-29 15:44:13 +00:00
76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
import Modal from 'components/Modal/index';
|
|
import Portal from 'components/Portal/index';
|
|
import { useFormik } from 'formik';
|
|
import { copyEnvironment } from 'providers/ReduxStore/slices/collections/actions';
|
|
import { useEffect, useRef } from 'react';
|
|
import toast from 'react-hot-toast';
|
|
import { useDispatch } from 'react-redux';
|
|
import * as Yup from 'yup';
|
|
|
|
const CopyEnvironment = ({ collection, environment, onClose }) => {
|
|
const dispatch = useDispatch();
|
|
const inputRef = useRef();
|
|
const formik = useFormik({
|
|
enableReinitialize: true,
|
|
initialValues: {
|
|
name: environment.name + ' - Copy'
|
|
},
|
|
validationSchema: Yup.object({
|
|
name: Yup.string()
|
|
.min(1, 'must be atleast 1 characters')
|
|
.max(50, 'must be 50 characters or less')
|
|
.required('name is required')
|
|
}),
|
|
onSubmit: (values) => {
|
|
dispatch(copyEnvironment(values.name, environment.uid, collection.uid))
|
|
.then(() => {
|
|
toast.success('Environment created in collection');
|
|
onClose();
|
|
})
|
|
.catch(() => toast.error('An error occurred while created the environment'));
|
|
}
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (inputRef && inputRef.current) {
|
|
inputRef.current.focus();
|
|
}
|
|
}, [inputRef]);
|
|
|
|
const onSubmit = () => {
|
|
formik.handleSubmit();
|
|
};
|
|
|
|
return (
|
|
<Portal>
|
|
<Modal size="sm" title={'Copy Environment'} confirmText="Copy" handleConfirm={onSubmit} handleCancel={onClose}>
|
|
<form className="bruno-form" onSubmit={formik.handleSubmit}>
|
|
<div>
|
|
<label htmlFor="name" className="block font-semibold">
|
|
New Environment Name
|
|
</label>
|
|
<input
|
|
id="environment-name"
|
|
type="text"
|
|
name="name"
|
|
ref={inputRef}
|
|
className="block textbox mt-2 w-full"
|
|
autoComplete="off"
|
|
autoCorrect="off"
|
|
autoCapitalize="off"
|
|
spellCheck="false"
|
|
onChange={formik.handleChange}
|
|
value={formik.values.name || ''}
|
|
/>
|
|
{formik.touched.name && formik.errors.name ? (
|
|
<div className="text-red-500">{formik.errors.name}</div>
|
|
) : null}
|
|
</div>
|
|
</form>
|
|
</Modal>
|
|
</Portal>
|
|
);
|
|
};
|
|
|
|
export default CopyEnvironment;
|