This commit is contained in:
Moaz Dawalibi 2024-09-12 13:43:45 +03:00
parent d56c4a06bf
commit 089160a4ae
6 changed files with 40 additions and 27 deletions

View File

@ -1,7 +1,8 @@
import { create } from "zustand";
interface FilterState {
filterState: any[];
filterState: {};
InitialValue: {};
setFilterState: (data: any) => void;
clearFilterState: () => void;
setWithOldValue: (data: any) => void;
@ -9,15 +10,14 @@ interface FilterState {
}
export const useFilterState = create<FilterState>((set, get) => ({
filterState: [],
filterState: {},
InitialValue:{name:""},
setFilterState: (data) => set(() => ({ filterState: data })),
clearFilterState: () => set(() => ({ filterState: [] })),
setWithOldValue: (data) =>
set((state) => ({ filterState: [...state.filterState, data] })),
set((state) => ({ filterState: {...state.filterState, data} })),
setInitialValue: (data) => {
if (get().filterState.length < 1) {
set(() => ({ filterState: data }));
}
set(() => ({ InitialValue: data }));
},
}));

View File

@ -1,6 +1,6 @@
import React, { ReactNode, useEffect, useState } from "react";
import { FaFilter } from "react-icons/fa";
import { Form, Formik, FormikConfig, FormikHelpers } from "formik";
import { Form, Formik, FormikConfig, FormikHelpers, useFormikContext } from "formik";
import { Button, ButtonProps, Divider, Modal } from "antd";
import { useTranslation } from "react-i18next";
import { useModalState } from "./Modal";
@ -35,11 +35,14 @@ interface SubmitButtonProps extends Omit<ButtonProps, "loading"> {}
const useFilter = () => {
const { setIsOpen, isOpen } = useModalState((state) => state);
const { filterState, setFilterState, clearFilterState } = useFilterState();
const { filterState, setFilterState, clearFilterState ,InitialValue} = useFilterState();
const [t] = useTranslation();
const [formValues, setFormValues] = useState({});
const formik = useFormikContext()
// Define the type for the callback
type SubmitCallback = () => void;
// console.log(formik?.values);
// console.log(InitialValue);
const FilterButton = () => {
const handleState = () => {
@ -64,6 +67,9 @@ const useFilter = () => {
setFilterState({})
}, [])
const FilterBody = ({
onSubmit,
children,
@ -79,10 +85,7 @@ const useFilter = () => {
}: FormikFormProps) => {
const handleSubmit = (values: any) => {
setFilterState(values);
console.log(formValues,"prev");
setFormValues(values);
console.log(formValues,"next");
if (onSubmit) {
onSubmit(values);
}
@ -101,6 +104,7 @@ const useFilter = () => {
setFormValues({});
};
const handleOpen = () => {
setIsOpen(true);
// setObjectToEdit({});
@ -123,11 +127,15 @@ const useFilter = () => {
<Formik
enableReinitialize={true}
onSubmit={handleSubmit}
initialValues={formValues}
initialValues={{}}
onReset={handleCancel}
{...formikProps}
>
{(formik) => {
return (
<Form>
<div>
<header> {t("models.filter")} <FaXmark onClick={()=>handleCancel(true)} /> </header>
<Divider />
@ -137,6 +145,9 @@ const useFilter = () => {
</main>
</div>
</Form>
)
}}
</Formik>
</Modal>
</>

View File

@ -4,7 +4,6 @@ import useFormField from "../../../Hooks/useFormField";
import { Field } from "formik";
import { ValidationFieldLabel } from "../components/ValidationFieldLabel";
import { ValidationFieldContainer } from "../components/ValidationFieldContainer";
import { FieldProps } from "../utils/types";
const Default = ({
name,

View File

@ -7,7 +7,7 @@ interface FormValues {
interface FormikFormProps {
handleSubmit: (values: any) => void;
initialValues: FormValues;
validationSchema: any;
validationSchema?: any;
title?: string;
children: ReactNode;
ButtonName?: string;

View File

@ -1,8 +1,11 @@
import React from "react";
import ValidationField from "../../../../Components/ValidationField/ValidationField";
import { Col, Row } from "reactstrap";
import { useFormikContext } from "formik";
const FilterForm = () => {
const formik = useFormikContext();
return (
<div>
<Row>

View File

@ -27,7 +27,7 @@ interface ModelState {
}
export const useObjectToEdit = create<ModelState>((set) => ({
objectToEdit: null,
c: null,
setObjectToEdit: (data) => set(() => ({ objectToEdit: data })),
OldObjectToEdit: null,
setOldObjectToEdit: (data) => set(() => ({ OldObjectToEdit: data })),