import { createContext, useContext } from 'solid-js';
import type { JSX, ParentProps, Context } from 'solid-js';
export type DIALOGS = { NEWMEMBER: 1, RXPRICING: 2, FINDMYRX: 3, ACTION2: 4, ACTION3: 5 };
export const Dialogs: DIALOGS = { NEWMEMBER: 1, RXPRICING: 2, FINDMYRX: 3, ACTION2: 4, ACTION3: 5 };
export type DialogType = DIALOGS[keyof DIALOGS] | string;
type DialogStateFn = (dialog: DialogType, visible?: boolean) => void;
type TChildFunc = (state: DialogStateFn) => JSX.Element;
let Dialog: Context<DialogStateFn> | undefined = undefined;
export default function Context (props: ParentProps<{ callback: DialogStateFn }>) {
if (Dialog === undefined) {
Dialog = createContext<DialogStateFn>(props.callback);
console.log("Initializing?");
}
return (
<Dialog.Provider value={props.callback}>
{props.children}
</Dialog.Provider>
);
};
export function useDialogCtx() {
if (Dialog === undefined) {
throw new Error("'Dialog' context provider not yet initialized.");
}
else return useContext(Dialog);
}
export const withDialogProvider = (callback: TChildFunc) => callback(useDialogCtx());