2021-11-23 18:46:33 +01:00
|
|
|
import React, { createContext, useContext, useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
export const TransactionModalContext = createContext({});
|
|
|
|
|
2022-02-01 18:53:03 +01:00
|
|
|
export const TransactionModalContextProvider = ({ children }) => {
|
2021-11-23 18:46:33 +01:00
|
|
|
const [openModals, setOpenModals] = useState([]);
|
|
|
|
|
2022-03-22 17:05:46 +01:00
|
|
|
const closeModal = (modalNames) => {
|
2022-01-06 23:56:51 +01:00
|
|
|
if (openModals < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-23 18:46:33 +01:00
|
|
|
const modals = [...openModals];
|
2022-03-22 17:05:46 +01:00
|
|
|
modalNames.forEach((modal) => {
|
|
|
|
const index = openModals.indexOf(modal);
|
|
|
|
modals.splice(index, 1);
|
|
|
|
});
|
2021-11-23 18:46:33 +01:00
|
|
|
setOpenModals(modals);
|
|
|
|
};
|
|
|
|
|
2021-11-29 18:40:48 +01:00
|
|
|
const closeAllModals = () => {
|
|
|
|
setOpenModals([]);
|
|
|
|
};
|
|
|
|
|
2021-11-23 18:46:33 +01:00
|
|
|
const openModal = (modalName) => {
|
2022-01-06 23:56:51 +01:00
|
|
|
if (openModals.includes(modalName)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-23 18:46:33 +01:00
|
|
|
const modals = [...openModals];
|
|
|
|
modals.push(modalName);
|
|
|
|
setOpenModals(modals);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TransactionModalContext.Provider
|
|
|
|
value={{
|
|
|
|
closeModal,
|
2021-11-29 18:40:48 +01:00
|
|
|
closeAllModals,
|
2021-11-23 18:46:33 +01:00
|
|
|
currentModal: openModals[openModals.length - 1],
|
|
|
|
openModal,
|
2022-01-06 03:47:26 +01:00
|
|
|
openModalCount: openModals.length,
|
2021-11-23 18:46:33 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</TransactionModalContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export function useTransactionModalContext() {
|
|
|
|
return useContext(TransactionModalContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionModalContextProvider.propTypes = {
|
|
|
|
children: PropTypes.node.isRequired,
|
|
|
|
};
|