refactor alertContext
This commit is contained in:
parent
ab1a4c9801
commit
bf8d477815
1 changed files with 138 additions and 93 deletions
|
|
@ -3,101 +3,146 @@ import { alertDropdownItem } from "../alerts/alertDropDown";
|
||||||
|
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
type alertContextType=
|
//types for alertContextType
|
||||||
{
|
type alertContextType = {
|
||||||
errorData: {title: string, list?: Array<string>};
|
errorData: { title: string; list?: Array<string> };
|
||||||
setErrorData:(newState:{title: string, list?: Array<string>})=>void;
|
setErrorData: (newState: { title: string; list?: Array<string> }) => void;
|
||||||
errorOpen: boolean;
|
errorOpen: boolean;
|
||||||
setErrorOpen:(newState:boolean)=>void;
|
setErrorOpen: (newState: boolean) => void;
|
||||||
noticeData: {title: string, link?: string};
|
noticeData: { title: string; link?: string };
|
||||||
setNoticeData:(newState:{title: string, link?: string})=>void;
|
setNoticeData: (newState: { title: string; link?: string }) => void;
|
||||||
noticeOpen: boolean;
|
noticeOpen: boolean;
|
||||||
setNoticeOpen:(newState:boolean)=>void;
|
setNoticeOpen: (newState: boolean) => void;
|
||||||
successData: {title: string};
|
successData: { title: string };
|
||||||
setSuccessData:(newState:{title: string})=>void;
|
setSuccessData: (newState: { title: string }) => void;
|
||||||
successOpen: boolean;
|
successOpen: boolean;
|
||||||
setSuccessOpen:(newState:boolean)=>void;
|
setSuccessOpen: (newState: boolean) => void;
|
||||||
notificationCenter:boolean
|
notificationCenter: boolean;
|
||||||
setNotificationCenter:(newState:boolean)=>void;
|
setNotificationCenter: (newState: boolean) => void;
|
||||||
notificationList:Array<alertDropdownItem>;
|
notificationList: Array<alertDropdownItem>;
|
||||||
pushNotificationList:(Object)=>void
|
pushNotificationList: (Object) => void;
|
||||||
clearNotificationList:()=>void,
|
clearNotificationList: () => void;
|
||||||
removeFromNotificationList:(index:number)=>void
|
removeFromNotificationList: (index: number) => void;
|
||||||
}
|
};
|
||||||
|
|
||||||
const initialValue= {
|
//initial values to alertContextType
|
||||||
errorData: {title:"", list:[]},
|
const initialValue = {
|
||||||
setErrorData:()=>{},
|
errorData: { title: "", list: [] },
|
||||||
errorOpen: false,
|
setErrorData: () => {},
|
||||||
setErrorOpen:()=>{},
|
errorOpen: false,
|
||||||
noticeData: {title:"", link:""},
|
setErrorOpen: () => {},
|
||||||
setNoticeData:()=>{},
|
noticeData: { title: "", link: "" },
|
||||||
noticeOpen: false,
|
setNoticeData: () => {},
|
||||||
setNoticeOpen:()=>{},
|
noticeOpen: false,
|
||||||
successData: {title:""},
|
setNoticeOpen: () => {},
|
||||||
setSuccessData:()=>{},
|
successData: { title: "" },
|
||||||
successOpen: false,
|
setSuccessData: () => {},
|
||||||
setSuccessOpen:()=>{},
|
successOpen: false,
|
||||||
notificationCenter:false,
|
setSuccessOpen: () => {},
|
||||||
setNotificationCenter:()=>{},
|
notificationCenter: false,
|
||||||
notificationList:[],
|
setNotificationCenter: () => {},
|
||||||
pushNotificationList:()=>{},
|
notificationList: [],
|
||||||
clearNotificationList:()=>{},
|
pushNotificationList: () => {},
|
||||||
removeFromNotificationList:()=>{}
|
clearNotificationList: () => {},
|
||||||
}
|
removeFromNotificationList: () => {},
|
||||||
|
};
|
||||||
|
|
||||||
export const alertContext = createContext<alertContextType>(initialValue);
|
export const alertContext = createContext<alertContextType>(initialValue);
|
||||||
|
|
||||||
export function AlertProvider({children}){
|
export function AlertProvider({ children }) {
|
||||||
const [errorData, setErrorDataState] = useState<{title:string,list?:Array<string>}>({title:"",list:[]});
|
const [errorData, setErrorDataState] = useState<{
|
||||||
const [errorOpen, setErrorOpen] = useState(false);
|
title: string;
|
||||||
const [noticeData, setNoticeDataState] = useState<{title:string,link?:string}>({title:"",link:""});
|
list?: Array<string>;
|
||||||
const [noticeOpen, setNoticeOpen] = useState(false);
|
}>({ title: "", list: [] });
|
||||||
const [successData, setSuccessDataState] = useState<{title:string}>({title:""});
|
const [errorOpen, setErrorOpen] = useState(false);
|
||||||
const [successOpen, setSuccessOpen] = useState(false);
|
const [noticeData, setNoticeDataState] = useState<{
|
||||||
const [notificationCenter,setNotificationCenter]=useState(false);
|
title: string;
|
||||||
const [notificationList,setNotificationList] = useState([])
|
link?: string;
|
||||||
const pushNotificationList = (notification:alertDropdownItem)=>{
|
}>({ title: "", link: "" });
|
||||||
setNotificationList((old) => {
|
const [noticeOpen, setNoticeOpen] = useState(false);
|
||||||
let newNotificationList = _.cloneDeep(old);
|
const [successData, setSuccessDataState] = useState<{ title: string }>({
|
||||||
newNotificationList.unshift(notification);
|
title: "",
|
||||||
return newNotificationList;
|
});
|
||||||
})
|
const [successOpen, setSuccessOpen] = useState(false);
|
||||||
}
|
const [notificationCenter, setNotificationCenter] = useState(false);
|
||||||
function setErrorData (newState:{title: string, list?: Array<string>}){
|
const [notificationList, setNotificationList] = useState([]);
|
||||||
setErrorDataState(newState)
|
const pushNotificationList = (notification: alertDropdownItem) => {
|
||||||
setErrorOpen(true)
|
setNotificationList((old) => {
|
||||||
if(newState.title){
|
let newNotificationList = _.cloneDeep(old);
|
||||||
setNotificationCenter(true)
|
newNotificationList.unshift(notification);
|
||||||
pushNotificationList({type:"error",title:newState.title,list:newState.list, id:_.uniqueId()})
|
return newNotificationList;
|
||||||
}
|
});
|
||||||
}
|
};
|
||||||
function setNoticeData(newState:{title: string, link?: string}){
|
function setErrorData(newState: { title: string; list?: Array<string> }) {
|
||||||
setNoticeDataState(newState)
|
setErrorDataState(newState);
|
||||||
setNoticeOpen(true)
|
setErrorOpen(true);
|
||||||
if(newState.title){
|
if (newState.title) {
|
||||||
setNotificationCenter(true)
|
setNotificationCenter(true);
|
||||||
pushNotificationList({type:"notice",title:newState.title,link:newState.link, id:_.uniqueId()})
|
pushNotificationList({
|
||||||
}
|
type: "error",
|
||||||
}
|
title: newState.title,
|
||||||
function setSuccessData(newState:{title: string}){
|
list: newState.list,
|
||||||
setSuccessDataState(newState)
|
id: _.uniqueId(),
|
||||||
setSuccessOpen(true)
|
});
|
||||||
if(newState.title){
|
}
|
||||||
setNotificationCenter(true)
|
}
|
||||||
pushNotificationList({type:"success",title:newState.title, id:_.uniqueId()})
|
function setNoticeData(newState: { title: string; link?: string }) {
|
||||||
}
|
setNoticeDataState(newState);
|
||||||
}
|
setNoticeOpen(true);
|
||||||
function clearNotificationList(){
|
if (newState.title) {
|
||||||
setNotificationList([])
|
setNotificationCenter(true);
|
||||||
}
|
pushNotificationList({
|
||||||
function removeFromNotificationList(index:number){
|
type: "notice",
|
||||||
setNotificationList((prevAlertsList) => prevAlertsList.filter((alert) => alert.id !== index));
|
title: newState.title,
|
||||||
}
|
link: newState.link,
|
||||||
return (
|
id: _.uniqueId(),
|
||||||
<alertContext.Provider value={{ removeFromNotificationList,clearNotificationList,notificationList,pushNotificationList,setNotificationCenter,notificationCenter,errorData, setErrorData,
|
});
|
||||||
errorOpen,setErrorOpen, noticeData, setNoticeData, noticeOpen, setNoticeOpen, successData, setSuccessData, successOpen, setSuccessOpen}}>
|
}
|
||||||
{children}
|
}
|
||||||
</alertContext.Provider>
|
function setSuccessData(newState: { title: string }) {
|
||||||
)
|
setSuccessDataState(newState);
|
||||||
|
setSuccessOpen(true);
|
||||||
|
if (newState.title) {
|
||||||
|
setNotificationCenter(true);
|
||||||
|
pushNotificationList({
|
||||||
|
type: "success",
|
||||||
|
title: newState.title,
|
||||||
|
id: _.uniqueId(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function clearNotificationList() {
|
||||||
|
setNotificationList([]);
|
||||||
|
}
|
||||||
|
function removeFromNotificationList(index: number) {
|
||||||
|
setNotificationList((prevAlertsList) =>
|
||||||
|
prevAlertsList.filter((alert) => alert.id !== index)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<alertContext.Provider
|
||||||
|
value={{
|
||||||
|
removeFromNotificationList,
|
||||||
|
clearNotificationList,
|
||||||
|
notificationList,
|
||||||
|
pushNotificationList,
|
||||||
|
setNotificationCenter,
|
||||||
|
notificationCenter,
|
||||||
|
errorData,
|
||||||
|
setErrorData,
|
||||||
|
errorOpen,
|
||||||
|
setErrorOpen,
|
||||||
|
noticeData,
|
||||||
|
setNoticeData,
|
||||||
|
noticeOpen,
|
||||||
|
setNoticeOpen,
|
||||||
|
successData,
|
||||||
|
setSuccessData,
|
||||||
|
successOpen,
|
||||||
|
setSuccessOpen,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</alertContext.Provider>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue