EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
notifications.js
Go to the documentation of this file.
1import { apiFetch } from '../lib/api';
2
3/**
4 * Universal notification function
5 * Sends notifications to backend which:
6 * - Stores in database
7 * - Appears in header notification bell
8 * - Sends to Slack if configured
9 *
10 * @param {Object} params
11 * @param {string} params.title - Notification title
12 * @param {string} params.message - Notification message
13 * @param {string} params.type - Type: 'info', 'success', 'warning', 'error'
14 * @param {number} [params.user_id] - Optional specific user ID
15 * @param {number} [params.customer_id] - Optional customer ID
16 * @returns {Promise<boolean>} Success status
17 */
18export async function notify({ title, message, type = 'info', user_id = null, customer_id = null }) {
19 try {
20 const token = localStorage.getItem('token');
21 if (!token) {
22 console.warn('No auth token for notification');
23 return false;
24 }
25
26 const res = await apiFetch('/notifications', {
27 method: 'POST',
28 headers: {
29 'Content-Type': 'application/json',
30 Authorization: `Bearer ${token}`
31 },
32 body: JSON.stringify({
33 user_id,
34 customer_id,
35 title,
36 message,
37 type
38 })
39 });
40
41 if (!res.ok) {
42 console.error('Failed to send notification:', await res.text());
43 return false;
44 }
45
46 // Dispatch event to refresh notification badge in header
47 window.dispatchEvent(new CustomEvent('notificationCreated'));
48
49 return true;
50 } catch (err) {
51 console.error('Error sending notification:', err);
52 return false;
53 }
54}
55
56/**
57 * Quick notification helpers for common scenarios
58 */
59export const notifySuccess = (title, message) => notify({ title, message, type: 'success' });
60export const notifyError = (title, message) => notify({ title, message, type: 'error' });
61export const notifyWarning = (title, message) => notify({ title, message, type: 'warning' });
62export const notifyInfo = (title, message) => notify({ title, message, type: 'info' });
63
64/**
65 * Notification helper for action success
66 */
67export const notifyActionSuccess = (action, resource) =>
68 notifySuccess(`${action} Successful`, `${resource} has been ${action.toLowerCase()} successfully`);
69
70/**
71 * Notification helper for action errors
72 */
73export const notifyActionError = (action, resource, error) =>
74 notifyError(`${action} Failed`, `Failed to ${action.toLowerCase()} ${resource}: ${error}`);
75
76/**
77 * Notification helper for permission denied
78 */
79export const notifyPermissionDenied = (action, resource) =>
80 notifyWarning('Permission Denied', `You do not have permission to ${action.toLowerCase()} ${resource}`);