Untitled

vai tomar no cu dessa escola
mail@pastecode.io avatar
unknown
tsx
18 days ago
2.8 kB
6
Indexable
Never
import { createContext, useState, ReactNode, useContext } from "react";
import { Bill, BillModalFields, Customer } from "../types";
import { ClientModalFields } from "../types/index";

interface IModalContext {
    isClientOpen: boolean;
    isBillOpen: boolean;
    isEditBillOpen: boolean;
    isUserOpen: boolean;
    rowData: Customer | Bill | BillModalFields | ClientModalFields | null;
    openBillModal: () => void;
    closeBillModal: () => void;
    openClientModal: () => void;
    closeClientModal: () => void;
    openEditBillModal: () => void;
    closeEditBillModal: () => void;
    openUserModal: () => void;
    closeUserModal: () => void;
    setRowData: (
        data: Customer | Bill | BillModalFields | ClientModalFields | null
    ) => void;
}

export const ModalContext = createContext<IModalContext | undefined>(undefined);

export const ModalProvider: React.FC<{ children: ReactNode }> = ({
    children,
}) => {
    const [isClientOpen, setIsClientOpen] = useState<boolean>(false);
    const [isEditBillOpen, setIsEditBillOpen] = useState<boolean>(false);
    const [isBillOpen, setIsBillOpen] = useState<boolean>(false);
    const [isUserOpen, setIsUserOpen] = useState<boolean>(false);
    const [rowData, setRowData] = useState<
        Customer | Bill | BillModalFields | ClientModalFields | null
    >(null);

    const openBillModal = () => {
        setIsBillOpen(true);
    };

    const closeBillModal = () => {
        setIsBillOpen(false);
        setRowData(null);
    };

    const openClientModal = () => {
        setIsClientOpen(true);
    };

    const closeClientModal = () => {
        setIsClientOpen(false);
    };

    const openEditBillModal = () => {
        setIsEditBillOpen(true);
    };

    const closeEditBillModal = () => {
        setIsEditBillOpen(false);
    };

    const openUserModal = () => {
        setIsUserOpen(true);
    };

    const closeUserModal = () => {
        setIsUserOpen(false);
    };

    return (
        <ModalContext.Provider
            value={{
                setRowData,
                rowData,
                isClientOpen,
                isBillOpen,
                isEditBillOpen,
                isUserOpen,
                openBillModal,
                closeBillModal,
                openClientModal,
                closeClientModal,
                openEditBillModal,
                closeEditBillModal,
                openUserModal,
                closeUserModal,
            }}
        >
            {children}
        </ModalContext.Provider>
    );
};

export function useModal() {
    const context = useContext(ModalContext);
    if (context === undefined) {
        throw new Error("useModal must be used within a ModalProvider");
    }
    return context;
}
Leave a Comment