|
| 1 | +import type { QueryClient, UseMutationOptions } from "react-query"; |
| 2 | +import { API } from "api/api"; |
| 3 | +import type { |
| 4 | + NotificationPreference, |
| 5 | + NotificationTemplate, |
| 6 | + UpdateNotificationTemplateMethod, |
| 7 | + UpdateUserNotificationPreferences, |
| 8 | +} from "api/typesGenerated"; |
| 9 | + |
| 10 | +export const userNotificationPreferencesKey = (userId: string) => [ |
| 11 | + "users", |
| 12 | + userId, |
| 13 | + "notifications", |
| 14 | + "preferences", |
| 15 | +]; |
| 16 | + |
| 17 | +export const userNotificationPreferences = (userId: string) => { |
| 18 | + return { |
| 19 | + queryKey: userNotificationPreferencesKey(userId), |
| 20 | + queryFn: () => API.getUserNotificationPreferences(userId), |
| 21 | + }; |
| 22 | +}; |
| 23 | + |
| 24 | +export const updateUserNotificationPreferences = ( |
| 25 | + userId: string, |
| 26 | + queryClient: QueryClient, |
| 27 | +) => { |
| 28 | + return { |
| 29 | + mutationFn: (req) => { |
| 30 | + return API.putUserNotificationPreferences(userId, req); |
| 31 | + }, |
| 32 | + onMutate: (data) => { |
| 33 | + queryClient.setQueryData( |
| 34 | + userNotificationPreferencesKey(userId), |
| 35 | + Object.entries(data.template_disabled_map).map( |
| 36 | + ([id, disabled]) => |
| 37 | + ({ |
| 38 | + id, |
| 39 | + disabled, |
| 40 | + updated_at: new Date().toISOString(), |
| 41 | + }) satisfies NotificationPreference, |
| 42 | + ), |
| 43 | + ); |
| 44 | + }, |
| 45 | + } satisfies UseMutationOptions< |
| 46 | + NotificationPreference[], |
| 47 | + unknown, |
| 48 | + UpdateUserNotificationPreferences |
| 49 | + >; |
| 50 | +}; |
| 51 | + |
| 52 | +export const systemNotificationTemplatesKey = [ |
| 53 | + "notifications", |
| 54 | + "templates", |
| 55 | + "system", |
| 56 | +]; |
| 57 | + |
| 58 | +export const systemNotificationTemplates = () => { |
| 59 | + return { |
| 60 | + queryKey: systemNotificationTemplatesKey, |
| 61 | + queryFn: () => API.getSystemNotificationTemplates(), |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | +export function selectTemplatesByGroup( |
| 66 | + data: NotificationTemplate[], |
| 67 | +): Record<string, NotificationTemplate[]> { |
| 68 | + const grouped = data.reduce( |
| 69 | + (acc, tpl) => { |
| 70 | + if (!acc[tpl.group]) { |
| 71 | + acc[tpl.group] = []; |
| 72 | + } |
| 73 | + acc[tpl.group].push(tpl); |
| 74 | + return acc; |
| 75 | + }, |
| 76 | + {} as Record<string, NotificationTemplate[]>, |
| 77 | + ); |
| 78 | + |
| 79 | + // Sort templates within each group |
| 80 | + for (const group in grouped) { |
| 81 | + grouped[group].sort((a, b) => a.name.localeCompare(b.name)); |
| 82 | + } |
| 83 | + |
| 84 | + // Sort groups by name |
| 85 | + const sortedGroups = Object.keys(grouped).sort((a, b) => a.localeCompare(b)); |
| 86 | + const sortedGrouped: Record<string, NotificationTemplate[]> = {}; |
| 87 | + for (const group of sortedGroups) { |
| 88 | + sortedGrouped[group] = grouped[group]; |
| 89 | + } |
| 90 | + |
| 91 | + return sortedGrouped; |
| 92 | +} |
| 93 | + |
| 94 | +export const notificationDispatchMethodsKey = [ |
| 95 | + "notifications", |
| 96 | + "dispatchMethods", |
| 97 | +]; |
| 98 | + |
| 99 | +export const notificationDispatchMethods = () => { |
| 100 | + return { |
| 101 | + staleTime: Infinity, |
| 102 | + queryKey: notificationDispatchMethodsKey, |
| 103 | + queryFn: () => API.getNotificationDispatchMethods(), |
| 104 | + }; |
| 105 | +}; |
| 106 | + |
| 107 | +export const updateNotificationTemplateMethod = ( |
| 108 | + templateId: string, |
| 109 | + queryClient: QueryClient, |
| 110 | +) => { |
| 111 | + return { |
| 112 | + mutationFn: (req: UpdateNotificationTemplateMethod) => |
| 113 | + API.updateNotificationTemplateMethod(templateId, req), |
| 114 | + onMutate: (data) => { |
| 115 | + const prevData = queryClient.getQueryData<NotificationTemplate[]>( |
| 116 | + systemNotificationTemplatesKey, |
| 117 | + ); |
| 118 | + if (!prevData) { |
| 119 | + return; |
| 120 | + } |
| 121 | + queryClient.setQueryData( |
| 122 | + systemNotificationTemplatesKey, |
| 123 | + prevData.map((tpl) => |
| 124 | + tpl.id === templateId |
| 125 | + ? { |
| 126 | + ...tpl, |
| 127 | + method: data.method, |
| 128 | + } |
| 129 | + : tpl, |
| 130 | + ), |
| 131 | + ); |
| 132 | + }, |
| 133 | + } satisfies UseMutationOptions< |
| 134 | + void, |
| 135 | + unknown, |
| 136 | + UpdateNotificationTemplateMethod |
| 137 | + >; |
| 138 | +}; |
0 commit comments