Compare commits

..

1 Commits

Author SHA1 Message Date
Allan Carr
15ea4e6afa IO-3310 Shop Data Preservation on Hidden Fields
Signed-off-by: Allan Carr <allan@imexsystems.ca>
2025-07-22 17:40:51 -07:00
7 changed files with 236 additions and 31 deletions

View File

@@ -1,6 +1,6 @@
import { Card, Checkbox, Input, Space, Table } from "antd";
import queryString from "query-string";
import { useState } from "react";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
@@ -16,13 +16,12 @@ import PayableExportAll from "../payable-export-all-button/payable-export-all-bu
import PayableExportButton from "../payable-export-button/payable-export-button.component";
import BillMarkSelectedExported from "../payable-mark-selected-exported/payable-mark-selected-exported.component";
import QboAuthorizeComponent from "../qbo-authorize/qbo-authorize.component";
import useLocalStorage from "./../../utils/useLocalStorage";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
const mapDispatchToProps = (dispatch) => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
});
@@ -32,7 +31,7 @@ export function AccountingPayablesTableComponent({ bodyshop, loading, bills, ref
const { t } = useTranslation();
const [selectedBills, setSelectedBills] = useState([]);
const [transInProgress, setTransInProgress] = useState(false);
const [state, setState] = useLocalStorage("accounting-payables-table-state", {
const [state, setState] = useState({
sortedInfo: {},
search: ""
});
@@ -182,7 +181,7 @@ export function AccountingPayablesTableComponent({ bodyshop, loading, bills, ref
onChange={handleTableChange}
rowSelection={{
onSelectAll: (selected, selectedRows) => setSelectedBills(selectedRows.map((i) => i.id)),
onSelect: (record, selected, selectedRows) => {
onSelect: (record, selected, selectedRows, nativeEvent) => {
setSelectedBills(selectedRows.map((i) => i.id));
},
getCheckboxProps: (record) => ({

View File

@@ -1,5 +1,5 @@
import { Card, Input, Space, Table } from "antd";
import { useState } from "react";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
@@ -10,7 +10,6 @@ import CurrencyFormatter from "../../utils/CurrencyFormatter";
import { DateFormatter, DateTimeFormatter } from "../../utils/DateFormatter";
import { exportPageLimit } from "../../utils/config";
import { alphaSort, dateSort } from "../../utils/sorters";
import useLocalStorage from "../../utils/useLocalStorage";
import ExportLogsCountDisplay from "../export-logs-count-display/export-logs-count-display.component";
import OwnerNameDisplay, { OwnerNameDisplayFunction } from "../owner-name-display/owner-name-display.component";
import PaymentExportButton from "../payment-export-button/payment-export-button.component";
@@ -22,7 +21,7 @@ const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
const mapDispatchToProps = (dispatch) => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
});
@@ -32,7 +31,7 @@ export function AccountingPayablesTableComponent({ bodyshop, loading, payments,
const { t } = useTranslation();
const [selectedPayments, setSelectedPayments] = useState([]);
const [transInProgress, setTransInProgress] = useState(false);
const [state, setState] = useLocalStorage("accounting-payments-table-state", {
const [state, setState] = useState({
sortedInfo: {},
search: ""
});
@@ -195,7 +194,7 @@ export function AccountingPayablesTableComponent({ bodyshop, loading, payments,
onChange={handleTableChange}
rowSelection={{
onSelectAll: (selected, selectedRows) => setSelectedPayments(selectedRows.map((i) => i.id)),
onSelect: (record, selected, selectedRows) => {
onSelect: (record, selected, selectedRows, nativeEvent) => {
setSelectedPayments(selectedRows.map((i) => i.id));
},
getCheckboxProps: (record) => ({

View File

@@ -1,5 +1,5 @@
import { Button, Card, Input, Space, Table } from "antd";
import { useState } from "react";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
@@ -10,7 +10,6 @@ import { exportPageLimit } from "../../utils/config";
import CurrencyFormatter from "../../utils/CurrencyFormatter";
import { DateFormatter } from "../../utils/DateFormatter";
import { alphaSort, dateSort, statusSort } from "../../utils/sorters";
import useLocalStorage from "../../utils/useLocalStorage";
import ExportLogsCountDisplay from "../export-logs-count-display/export-logs-count-display.component";
import JobExportButton from "../jobs-close-export-button/jobs-close-export-button.component";
import JobsExportAllButton from "../jobs-export-all-button/jobs-export-all-button.component";
@@ -21,7 +20,7 @@ import QboAuthorizeComponent from "../qbo-authorize/qbo-authorize.component";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
const mapDispatchToProps = (dispatch) => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(AccountingReceivablesTableComponent);
@@ -31,7 +30,7 @@ export function AccountingReceivablesTableComponent({ bodyshop, loading, jobs, r
const [selectedJobs, setSelectedJobs] = useState([]);
const [transInProgress, setTransInProgress] = useState(false);
const [state, setState] = useLocalStorage("accounting-receivables-table-state", {
const [state, setState] = useState({
sortedInfo: {},
search: ""
});
@@ -208,7 +207,7 @@ export function AccountingReceivablesTableComponent({ bodyshop, loading, jobs, r
onChange={handleTableChange}
rowSelection={{
onSelectAll: (selected, selectedRows) => setSelectedJobs(selectedRows.map((i) => i.id)),
onSelect: (record, selected, selectedRows) => {
onSelect: (record, selected, selectedRows, nativeEvent) => {
setSelectedJobs(selectedRows.map((i) => i.id));
},
getCheckboxProps: (record) => ({

View File

@@ -1,15 +1,16 @@
import { useMutation, useQuery } from "@apollo/client";
import { Form } from "antd";
import dayjs from "../../utils/day";
import React, { useEffect, useState } from "react";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
import { logImEXEvent } from "../../firebase/firebase.utils";
import { QUERY_BODYSHOP, UPDATE_SHOP } from "../../graphql/bodyshop.queries";
import dayjs from "../../utils/day";
import AlertComponent from "../alert/alert.component";
import FormsFieldChanged from "../form-fields-changed-alert/form-fields-changed-alert.component";
import LoadingSpinner from "../loading-spinner/loading-spinner.component";
import ShopInfoComponent from "./shop-info.component";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
import { FEATURE_CONFIGS, useFormDataPreservation } from "./useFormDataPreservation";
export default function ShopInfoContainer() {
const [form] = Form.useForm();
@@ -22,16 +23,24 @@ export default function ShopInfoContainer() {
});
const notification = useNotification();
const handleFinish = (values) => {
const combinedFeatureConfig = {
...FEATURE_CONFIGS.general,
...FEATURE_CONFIGS.responsibilitycenters
};
// Use form data preservation for all shop-info features
const { createSubmissionHandler } = useFormDataPreservation(form, data?.bodyshops[0], combinedFeatureConfig);
const handleFinish = createSubmissionHandler((values) => {
setSaveLoading(true);
logImEXEvent("shop_update");
updateBodyshop({
variables: { id: data.bodyshops[0].id, shop: values }
})
.then((r) => {
.then(() => {
notification["success"]({ message: t("bodyshop.successes.save") });
refetch().then((_) => form.resetFields());
refetch().then(() => form.resetFields());
})
.catch((error) => {
notification["error"]({
@@ -39,7 +48,7 @@ export default function ShopInfoContainer() {
});
});
setSaveLoading(false);
};
});
useEffect(() => {
if (data) form.resetFields();

View File

@@ -0,0 +1,140 @@
import { useEffect } from "react";
import { HasFeatureAccess } from "./../feature-wrapper/feature-wrapper.component";
/**
* Custom hook to preserve form data for conditionally hidden fields based on feature access
* @param {Object} form - Ant Design form instance
* @param {Object} bodyshop - Bodyshop data for feature access checks (also contains existing database values)
* @param {Object} featureConfig - Configuration object defining which features and their associated fields to preserve
*/
export const useFormDataPreservation = (form, bodyshop, featureConfig) => {
const getNestedValue = (obj, path) => {
return path.reduce((current, key) => current?.[key], obj);
};
const setNestedValue = (obj, path, value) => {
const lastKey = path[path.length - 1];
const parentPath = path.slice(0, -1);
const parent = parentPath.reduce((current, key) => {
if (!current[key]) current[key] = {};
return current[key];
}, obj);
parent[lastKey] = value;
};
const preserveHiddenFormData = () => {
const preservationData = {};
let hasDataToPreserve = false;
Object.entries(featureConfig).forEach(([featureName, fieldPaths]) => {
const hasAccess = HasFeatureAccess({ featureName, bodyshop });
if (!hasAccess) {
fieldPaths.forEach((fieldPath) => {
const currentValues = form.getFieldsValue();
let value = getNestedValue(currentValues, fieldPath);
if (value === undefined || value === null) {
value = getNestedValue(bodyshop, fieldPath);
}
if (value !== undefined && value !== null) {
setNestedValue(preservationData, fieldPath, value);
hasDataToPreserve = true;
}
});
}
});
if (hasDataToPreserve) {
form.setFieldsValue(preservationData);
}
};
const getCompleteFormValues = () => {
const currentFormValues = form.getFieldsValue();
const completeValues = { ...currentFormValues };
Object.entries(featureConfig).forEach(([featureName, fieldPaths]) => {
const hasAccess = HasFeatureAccess({ featureName, bodyshop });
if (!hasAccess) {
fieldPaths.forEach((fieldPath) => {
let value = getNestedValue(currentFormValues, fieldPath);
if (value === undefined || value === null) {
value = getNestedValue(bodyshop, fieldPath);
}
if (value !== undefined && value !== null) {
setNestedValue(completeValues, fieldPath, value);
}
});
}
});
return completeValues;
};
const createSubmissionHandler = (originalHandler) => {
return () => {
const completeValues = getCompleteFormValues();
// Call the original handler with complete values including hidden data
return originalHandler(completeValues);
};
};
useEffect(() => {
preserveHiddenFormData();
}, [bodyshop]);
return { preserveHiddenFormData, getCompleteFormValues, createSubmissionHandler };
};
/**
* Predefined feature configurations for common shop-info components
*/
export const FEATURE_CONFIGS = {
responsibilitycenters: {
export: [
["md_responsibility_centers", "costs"],
["md_responsibility_centers", "profits"],
["md_responsibility_centers", "defaults"],
["md_responsibility_centers", "dms_defaults"],
["md_responsibility_centers", "taxes", "itemexemptcode"],
["md_responsibility_centers", "taxes", "invoiceexemptcode"],
["md_responsibility_centers", "ar"],
["md_responsibility_centers", "refund"],
["md_responsibility_centers", "sales_tax_codes"],
["md_responsibility_centers", "ttl_adjustment"],
["md_responsibility_centers", "ttl_tax_adjustment"]
]
},
general: {
export: [
["accountingconfig", "qbo"],
["accountingconfig", "qbo_usa"],
["accountingconfig", "qbo_departmentid"],
["accountingconfig", "tiers"],
["accountingconfig", "twotierpref"],
["accountingconfig", "printlater"],
["accountingconfig", "emaillater"],
["accountingconfig", "ReceivableCustomField1"],
["accountingconfig", "ReceivableCustomField2"],
["accountingconfig", "ReceivableCustomField3"],
["md_classes"],
["enforce_class"],
["accountingconfig", "ClosingPeriod"],
["accountingconfig", "companyCode"],
["accountingconfig", "batchID"]
],
bills: [
["bill_tax_rates", "federal_tax_rate"],
["bill_tax_rates", "state_tax_rate"],
["bill_tax_rates", "local_tax_rate"]
],
timetickets: [["tt_allow_post_to_invoiced"], ["tt_enforce_hours_for_tech_console"], ["bill_allow_post_to_closed"]]
}
};

View File

@@ -32,6 +32,7 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
const socketRef = useRef(null);
const [clientId, setClientId] = useState(null);
const [isConnected, setIsConnected] = useState(false);
const [socketInitialized, setSocketInitialized] = useState(false);
const notification = useNotification();
const userAssociationId = bodyshop?.associations?.[0]?.id;
const { t } = useTranslation();
@@ -147,6 +148,13 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
onError: (err) => console.error("MARK_ALL_NOTIFICATIONS_READ error:", err)
});
const checkAndReconnect = () => {
if (socketRef.current && !socketRef.current.connected) {
console.log("Attempting manual reconnect due to event trigger");
socketRef.current.connect();
}
};
useEffect(() => {
const initializeSocket = async (token) => {
if (!bodyshop || !bodyshop.id || socketRef.current) return;
@@ -158,13 +166,14 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
auth: { token, bodyshopId: bodyshop.id },
reconnectionAttempts: Infinity,
reconnectionDelay: 2000,
reconnectionDelayMax: 60000
// randomizationFactor: 0.5,
// transports: ["websocket", "polling"], // Add this to prefer WebSocket with polling fallback
// rememberUpgrade: true
reconnectionDelayMax: 60000,
randomizationFactor: 0.5,
transports: ["websocket", "polling"], // Add this to prefer WebSocket with polling fallback
rememberUpgrade: true
});
socketRef.current = socketInstance;
setSocketInitialized(true);
const handleBodyshopMessage = (message) => {
if (!message || !message.type) return;
@@ -252,7 +261,7 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
break;
}
};
const handleConnect = () => {
socketInstance.emit("join-bodyshop-room", bodyshop.id);
setClientId(socketInstance.id);
@@ -549,6 +558,57 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
t
]);
useEffect(() => {
if (!socketInitialized) return;
const onVisibilityChange = () => {
if (document.visibilityState === "visible") {
checkAndReconnect();
}
};
const onFocus = () => {
checkAndReconnect();
};
const onOnline = () => {
checkAndReconnect();
};
const onPageShow = (event) => {
if (event.persisted) {
checkAndReconnect();
}
};
document.addEventListener("visibilitychange", onVisibilityChange);
window.addEventListener("focus", onFocus);
window.addEventListener("online", onOnline);
window.addEventListener("pageshow", onPageShow);
// Sleep/wake detection using timer
let lastTime = Date.now();
const intervalMs = 1000; // Check every second
const thresholdMs = 2000; // If more than 2 seconds elapsed, assume sleep/wake
const sleepCheckInterval = setInterval(() => {
const currentTime = Date.now();
if (currentTime > lastTime + intervalMs + thresholdMs) {
console.log("Detected potential wake from sleep/hibernate");
checkAndReconnect();
}
lastTime = currentTime;
}, intervalMs);
return () => {
document.removeEventListener("visibilitychange", onVisibilityChange);
window.removeEventListener("focus", onFocus);
window.removeEventListener("online", onOnline);
window.removeEventListener("pageshow", onPageShow);
clearInterval(sleepCheckInterval);
};
}, [socketInitialized]);
return (
<SocketContext.Provider
value={{

View File

@@ -2,7 +2,7 @@ import { EditFilled, SyncOutlined } from "@ant-design/icons";
import { Button, Card, Checkbox, Input, Space, Table, Typography } from "antd";
import axios from "axios";
import queryString from "query-string";
import { useEffect, useState } from "react";
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { Link, useLocation, useNavigate } from "react-router-dom";
@@ -13,9 +13,8 @@ import { setModalContext } from "../../redux/modals/modals.actions";
import CurrencyFormatter from "../../utils/CurrencyFormatter";
import { DateFormatter } from "../../utils/DateFormatter";
import { TemplateList } from "../../utils/TemplateConstants";
import { pageLimit } from "../../utils/config";
import { alphaSort, dateSort } from "../../utils/sorters";
import useLocalStorage from "../../utils/useLocalStorage";
import { pageLimit } from "../../utils/config";
const mapDispatchToProps = (dispatch) => ({
setPartsOrderContext: (context) => dispatch(setModalContext({ context: context, modal: "partsOrder" })),
@@ -28,7 +27,7 @@ export function BillsListPage({ loading, data, refetch, total, setPartsOrderCont
const [searchLoading, setSearchLoading] = useState(false);
const { page } = search;
const history = useNavigate();
const [state, setState] = useLocalStorage("bills_list_sort", {
const [state, setState] = useState({
sortedInfo: {},
filteredInfo: { text: "" }
});