From d0ebd4a2f1e1a845485b8a30826b3407eeb0e703 Mon Sep 17 00:00:00 2001 From: mathieu Date: Wed, 23 Oct 2024 22:48:57 +0200 Subject: [PATCH] simplify onColorChange Signed-off-by: mathieu --- .../EnvironmentColor/index.js | 61 ++++--------------- 1 file changed, 12 insertions(+), 49 deletions(-) diff --git a/packages/bruno-app/src/components/Environments/EnvironmentSettings/EnvironmentList/EnvironmentDetails/EnvironmentColor/index.js b/packages/bruno-app/src/components/Environments/EnvironmentSettings/EnvironmentList/EnvironmentDetails/EnvironmentColor/index.js index cee2b91e92..5277ecfb4f 100644 --- a/packages/bruno-app/src/components/Environments/EnvironmentSettings/EnvironmentList/EnvironmentDetails/EnvironmentColor/index.js +++ b/packages/bruno-app/src/components/Environments/EnvironmentSettings/EnvironmentList/EnvironmentDetails/EnvironmentColor/index.js @@ -1,66 +1,29 @@ import React from 'react'; -import { useEffect } from 'react'; +import { useCallback } from 'react'; import toast from 'react-hot-toast'; import { useDispatch } from 'react-redux'; import { saveEnvironmentColor } from 'providers/ReduxStore/slices/collections/actions'; -import { useFormik } from 'formik'; -import * as Yup from 'yup'; import { CirclePicker } from 'react-color'; -import { selectEnvironment as _selectEnvironment } from 'providers/ReduxStore/slices/collections'; const EnvironmentColor = ({ environment, collectionUid }) => { const dispatch = useDispatch(); - const formik = useFormik({ - enableReinitialize: true, - initialValues: { - color: environment.color || '' - }, - validationSchema: Yup.object({ - color: Yup.string().optional() - }), - onSubmit: (values) => { - if (!formik.dirty) { - toast.error('Nothing to save'); - return; - } - dispatch(saveEnvironmentColor(values.color, environment.uid, collectionUid)) - .then(() => { - toast.success('Environment color changed successfully'); - }) - .catch((e) => { - console.log(e); - toast.error('An error occurred while changing the environment color'); - }); - } - }); - - useEffect(() => { - if (formik.dirty) { - formik.handleSubmit(); - } - }, [formik.values.color]); + const onColorChange = useCallback( + (color) => ( + dispatch(saveEnvironmentColor(color, environment.uid, collectionUid)) + .then(() => toast.success('Environment color changed successfully')) + .catch((e) => toast.error('An error occurred while changing the environment color')) + ) + ); return ( formik.setFieldValue('color', color.hex)} - value={formik.values.color || ''} + onChangeComplete={(color) => onColorChange(color.hex)} /> ); };