import { Button, Checkbox, Spinner, Table } from '@/components/ui' import NotificationChannels from '@/constants/notification-channel.enum' import { NotificationRuleDto } from '@/proxy/notification/models' import { deleteMyNotificationRules, getMyNotificationRules, getMyNotificationTypes, postMyNotificationRule, } from '@/proxy/notification/notification-rule.service' import classNames from 'classnames' import isEmpty from 'lodash/isEmpty' import { useEffect, useState } from 'react' import { MdRestore } from 'react-icons/md' const { Tr, Th, Td, THead, TBody } = Table const NotificationSettings = () => { const [loading, setLoading] = useState(true) const [list, setList] = useState([]) const [notificationTypes, setNotificationTypes] = useState([]) const [channels, setChannels] = useState([]) const fetchData = async () => { setLoading(true) const channels = Object.keys(NotificationChannels) setChannels(channels) const response = await getMyNotificationTypes() const notificationTypes = response.data setNotificationTypes(response.data) const responseRules = await getMyNotificationRules() const rules = responseRules.data const finalList: NotificationRuleDto[][] = [] for (const notificationType of notificationTypes) { const x: NotificationRuleDto[] = [] for (const channel of channels) { const rule = rules.find( (a) => a.channel == channel && a.notificationType == notificationType, ) x.push( rule ?? ({ channel, notificationType, isActive: false, } as NotificationRuleDto), ) } finalList.push(x) } setList(finalList) setLoading(false) } useEffect(() => { if (isEmpty(list)) { fetchData() } }, []) const onClickCheckbox = async (checked: boolean, rule: NotificationRuleDto) => { await postMyNotificationRule({ channel: rule.channel, notificationType: rule.notificationType, isActive: checked, }) fetchData() } const resetMyNotifications = async (notificationType: string) => { await deleteMyNotificationRules(notificationType) fetchData() } return ( <> {loading ? (
) : ( <> {list.length ? ( {channels.map((a) => ( ))} {notificationTypes.map((a, m) => ( {channels.map((b, n) => ( ))} ))}
{a}
onClickCheckbox(e, list[m][n])} color={list[m][n].isCustomized ? 'red-500' : undefined} title={list[m][n].id} >
) : ( <> )} )} ) } export default NotificationSettings