feature/IO-3096-GlobalNotifications - Check-point
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useMutation, useQuery } from "@apollo/client";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { Button, Card, Col, Form, Row, Switch } from "antd";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Button, Card, Col, Form, Row } from "antd";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { connect } from "react-redux";
|
||||
@@ -8,9 +8,34 @@ import { createStructuredSelector } from "reselect";
|
||||
import { selectCurrentUser } from "../../redux/user/user.selectors";
|
||||
import AlertComponent from "../alert/alert.component";
|
||||
import { QUERY_NOTIFICATION_SETTINGS, UPDATE_NOTIFICATION_SETTINGS } from "../../graphql/user.queries.js";
|
||||
import notificationScenarios from "../../utils/jobNotificationScenarios.js";
|
||||
import { notificationScenarios, notificationChannels } from "../../utils/jobNotificationScenarios.js";
|
||||
import LoadingSpinner from "../loading-spinner/loading-spinner.component.jsx";
|
||||
|
||||
const NotificationMethodButtonGroup = ({ value = {}, onChange }) => {
|
||||
const { t } = useTranslation();
|
||||
const toggleMethod = (method) => {
|
||||
const newValue = { ...value, [method]: !value[method] };
|
||||
if (onChange) {
|
||||
onChange(newValue);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Button.Group>
|
||||
{notificationChannels.map((method) => (
|
||||
<Button
|
||||
disabled={method === "fcm"}
|
||||
key={method}
|
||||
type={value[method] ? "primary" : "default"}
|
||||
onClick={() => toggleMethod(method)}
|
||||
>
|
||||
{t(`notifications.channels.${method}`)}
|
||||
</Button>
|
||||
))}
|
||||
</Button.Group>
|
||||
);
|
||||
};
|
||||
|
||||
function NotificationSettingsForm({ currentUser }) {
|
||||
const { t } = useTranslation();
|
||||
const [form] = Form.useForm();
|
||||
@@ -31,8 +56,10 @@ function NotificationSettingsForm({ currentUser }) {
|
||||
useEffect(() => {
|
||||
if (data?.associations?.length > 0) {
|
||||
const settings = data.associations[0].notification_settings || {};
|
||||
// For each scenario, expect an object with keys { app, email, fcm }.
|
||||
// If not present in the fetched data, default to all false.
|
||||
const formattedValues = notificationScenarios.reduce((acc, scenario) => {
|
||||
acc[scenario] = settings[scenario] ?? false;
|
||||
acc[scenario] = settings[scenario] ?? { app: false, email: false, fcm: false };
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
@@ -45,6 +72,7 @@ function NotificationSettingsForm({ currentUser }) {
|
||||
const handleSave = async (values) => {
|
||||
if (data?.associations?.length > 0) {
|
||||
const userId = data.associations[0].id;
|
||||
// `values` now contains, for each scenario, an object with keys { app, email, fcm }
|
||||
await updateNotificationSettings({ variables: { id: userId, ns: values } });
|
||||
setInitialValues(values);
|
||||
setIsDirty(false);
|
||||
@@ -88,8 +116,8 @@ function NotificationSettingsForm({ currentUser }) {
|
||||
<Row gutter={[16, 16]}>
|
||||
{notificationScenarios.map((scenario) => (
|
||||
<Col xs={24} sm={12} md={8} key={scenario}>
|
||||
<Form.Item name={scenario} label={t(`notifications.scenarios.${scenario}`)} valuePropName="checked">
|
||||
<Switch />
|
||||
<Form.Item name={scenario} label={t(`notifications.scenarios.${scenario}`)}>
|
||||
<NotificationMethodButtonGroup />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
))}
|
||||
|
||||
@@ -3761,7 +3761,7 @@
|
||||
},
|
||||
"notifications": {
|
||||
"labels": {
|
||||
"notificationscenarios": "Notification Scenarios",
|
||||
"notificationscenarios": "Job Notification Scenarios",
|
||||
"save": "Save Scenarios",
|
||||
"watching-issue": "Watching",
|
||||
"add-watchers": "Add Watchers",
|
||||
@@ -3795,6 +3795,11 @@
|
||||
"job-status-change": "Job Status Changed",
|
||||
"payment-collected-completed": "Payment Collected / Completed",
|
||||
"alternate-transport-changed": "Alternate Transport Changed"
|
||||
},
|
||||
"channels": {
|
||||
"app": "App",
|
||||
"email": "Email",
|
||||
"fcm": "Push"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3795,6 +3795,11 @@
|
||||
"job-status-change": "",
|
||||
"payment-collected-completed": "",
|
||||
"alternate-transport-changed": ""
|
||||
},
|
||||
"channels": {
|
||||
"app": "",
|
||||
"email": "",
|
||||
"fcm": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3795,6 +3795,11 @@
|
||||
"job-status-change": "",
|
||||
"payment-collected-completed": "",
|
||||
"alternate-transport-changed": ""
|
||||
},
|
||||
"channels": {
|
||||
"app": "",
|
||||
"email": "",
|
||||
"fcm": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,6 @@ const notificationScenarios = [
|
||||
"alternate-transport-changed"
|
||||
];
|
||||
|
||||
export default notificationScenarios;
|
||||
const notificationChannels = ["app", "email", "fcm"];
|
||||
|
||||
export { notificationScenarios, notificationChannels };
|
||||
|
||||
Reference in New Issue
Block a user