modals state is not updated when executing openModal Zustand

35 Views Asked by At

When using the openModal function, the contents of the modals stack are not changed in any way. The updatedModals array is created and looks as it should, but the state is not updated. What am I doing wrong?

"use client"

import {create} from 'zustand';
import {ModalState} from "@/entities/modal/model";
import {Modal} from "../model";

const useModalStore = create<ModalState>((set, getState) => ({
    modals: [],
    addModal: (newModal: Modal) => {
        set((state) => {
            const existingModal = state.modals.find(modal => modal.name === newModal.name);
            if (existingModal) {
                return state;
            }
            return { modals: [...state.modals, newModal] };
        });
    },
    openModal: (name: string | undefined) => {
        set((state) => {
            const updatedModals = state.modals.map(modal => {
                if (modal.name === name) {
                    return { ...modal, isOpen: true };
                }
                return modal;
            });

            return { modals: updatedModals };
        });
    },
    closeModal: (name: string | undefined) => {
        set((state) => {
            const updatedModals = state.modals.map(modal => {
                if (modal.name === name) {
                    return { ...modal, isOpen: false };
                }
                return modal;
            });
            return { modals: updatedModals };
        });
    },
}));

export default useModalStore;

I expect the modals state to change

0

There are 0 best solutions below