mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-02 08:58:32 +00:00
66 lines
2.0 KiB
JavaScript
66 lines
2.0 KiB
JavaScript
import React, { useRef, useEffect } from 'react';
|
|
import { useFormik } from 'formik';
|
|
import * as Yup from 'yup';
|
|
import Modal from 'components/Modal';
|
|
import { useDispatch } from 'react-redux';
|
|
import toast from 'react-hot-toast';
|
|
import { renameCollection } from 'providers/ReduxStore/slices/collections/actions';
|
|
|
|
const RenameCollection = ({ collection, onClose }) => {
|
|
const dispatch = useDispatch();
|
|
const inputRef = useRef();
|
|
const formik = useFormik({
|
|
enableReinitialize: true,
|
|
initialValues: {
|
|
name: collection.name
|
|
},
|
|
validationSchema: Yup.object({
|
|
name: Yup.string()
|
|
.min(1, 'must be at least 1 character')
|
|
.max(50, 'must be 50 characters or less')
|
|
.required('name is required')
|
|
}),
|
|
onSubmit: (values) => {
|
|
dispatch(renameCollection(values.name, collection.uid));
|
|
toast.success('Collection renamed!');
|
|
onClose();
|
|
}
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (inputRef && inputRef.current) {
|
|
inputRef.current.focus();
|
|
}
|
|
}, [inputRef]);
|
|
|
|
const onSubmit = () => formik.handleSubmit();
|
|
|
|
return (
|
|
<Modal size="sm" title="Rename Collection" confirmText="Rename" handleConfirm={onSubmit} handleCancel={onClose}>
|
|
<form className="bruno-form" onSubmit={formik.handleSubmit}>
|
|
<div>
|
|
<label htmlFor="name" className="block font-semibold">
|
|
Name
|
|
</label>
|
|
<input
|
|
id="collection-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>
|
|
);
|
|
};
|
|
|
|
export default RenameCollection;
|