message push config

This commit is contained in:
yoan
2024-09-23 23:13:34 +08:00
parent 5422f17fab
commit 4c9095400e
13 changed files with 727 additions and 60 deletions

View File

@@ -1,6 +1,6 @@
import { Access } from "@/domain/access";
import { ConfigData } from ".";
import { Setting } from "@/domain/settings";
import { EmailsSetting, Setting } from "@/domain/settings";
import { AccessGroup } from "@/domain/access_groups";
type Action =
@@ -57,7 +57,10 @@ export const configReducer = (
emails: {
...state.emails,
content: {
emails: [...state.emails.content.emails, action.payload],
emails: [
...(state.emails.content as EmailsSetting).emails,
action.payload,
],
},
},
};

View File

@@ -0,0 +1,68 @@
import { NotifyChannel, Setting } from "@/domain/settings";
import { getSetting } from "@/repository/settings";
import {
ReactNode,
useContext,
createContext,
useEffect,
useReducer,
useCallback,
} from "react";
import { notifyReducer } from "./reducer";
export type NotifyContext = {
config: Setting;
setChannel: (data: { channel: string; data: NotifyChannel }) => void;
setChannels: (data: Setting) => void;
};
const Context = createContext({} as NotifyContext);
export const useNotify = () => useContext(Context);
interface ContainerProps {
children: ReactNode;
}
export const NotifyProvider = ({ children }: ContainerProps) => {
const [notify, dispatchNotify] = useReducer(notifyReducer, {});
useEffect(() => {
const featchData = async () => {
const chanels = await getSetting("notifyChannels");
dispatchNotify({
type: "SET_CHANNELS",
payload: chanels,
});
};
featchData();
}, []);
const setChannel = useCallback(
(data: { channel: string; data: NotifyChannel }) => {
dispatchNotify({
type: "SET_CHANNEL",
payload: data,
});
},
[]
);
const setChannels = useCallback((setting: Setting) => {
dispatchNotify({
type: "SET_CHANNELS",
payload: setting,
});
}, []);
return (
<Context.Provider
value={{
config: notify,
setChannel,
setChannels,
}}
>
{children}
</Context.Provider>
);
};

View File

@@ -0,0 +1,35 @@
import { NotifyChannel, Setting } from "@/domain/settings";
type Action =
| {
type: "SET_CHANNEL";
payload: {
channel: string;
data: NotifyChannel;
};
}
| {
type: "SET_CHANNELS";
payload: Setting;
};
export const notifyReducer = (state: Setting, action: Action) => {
switch (action.type) {
case "SET_CHANNEL": {
const channel = action.payload.channel;
return {
...state,
content: {
...state.content,
[channel]: action.payload.data,
},
};
}
case "SET_CHANNELS": {
return { ...action.payload };
}
default:
return state;
}
};