init
This commit is contained in:
90
src/context/CampaignRuleContext.tsx
Normal file
90
src/context/CampaignRuleContext.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
import { createContext, useContext, useState } from "react";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
export type RuleCondition = {
|
||||
id: string;
|
||||
attribute: string;
|
||||
label: string;
|
||||
operator: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
export type RuleGroup = {
|
||||
id: string;
|
||||
operator: "AND" | "OR";
|
||||
conditions: RuleCondition[];
|
||||
};
|
||||
|
||||
export type CampaignRuleState = {
|
||||
general: {
|
||||
id: string;
|
||||
name: string;
|
||||
type: string;
|
||||
effectiveDateStart: string;
|
||||
effectiveDateEnd: string;
|
||||
};
|
||||
criteria: {
|
||||
globalOperator: "AND" | "OR";
|
||||
groups: RuleGroup[];
|
||||
};
|
||||
formula: any;
|
||||
notification: any;
|
||||
};
|
||||
|
||||
type CampaignRuleContextType = {
|
||||
state: CampaignRuleState;
|
||||
updateState: (section: keyof CampaignRuleState, payload: any) => void;
|
||||
currentStep: number;
|
||||
setStep: (step: number) => void;
|
||||
};
|
||||
|
||||
const initialState: CampaignRuleState = {
|
||||
general: {
|
||||
id: "",
|
||||
name: "",
|
||||
type: "",
|
||||
effectiveDateStart: "",
|
||||
effectiveDateEnd: "",
|
||||
},
|
||||
criteria: {
|
||||
globalOperator: "AND",
|
||||
groups: [],
|
||||
},
|
||||
formula: {},
|
||||
notification: {},
|
||||
};
|
||||
|
||||
const CampaignRuleContext = createContext<CampaignRuleContextType | undefined>(undefined);
|
||||
|
||||
export const CampaignRuleProvider = ({ children }: { children: ReactNode }) => {
|
||||
const [state, setState] = useState<CampaignRuleState>(initialState);
|
||||
const [currentStep, setCurrentStep] = useState(0);
|
||||
|
||||
const updateState = (section: keyof CampaignRuleState, payload: any) => {
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
[section]: {
|
||||
...prev[section],
|
||||
...payload,
|
||||
},
|
||||
}));
|
||||
};
|
||||
|
||||
const setStep = (step: number) => {
|
||||
setCurrentStep(step);
|
||||
};
|
||||
|
||||
return (
|
||||
<CampaignRuleContext.Provider value={{ state, updateState, currentStep, setStep }}>
|
||||
{children}
|
||||
</CampaignRuleContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useCampaignRule = () => {
|
||||
const context = useContext(CampaignRuleContext);
|
||||
if (!context) {
|
||||
throw new Error("useCampaignRule must be used within a CampaignRuleProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user