import { useCallback, useEffect, useRef, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { Form, Checkbox, Radio, Divider } from 'antd'; import { ArrowRightOutlined } from '@ant-design/icons'; import FormHeader from '../PageComponents/FormHeader.jsx'; import Buttons from '../../Components/Forms/Buttons.jsx'; import { Messages } from '../../Components/Notifications/Messages'; import { changeBreadCrumb, getEmpAccess, } from '../../Features/AppPage/CenterPage.js'; import { getApplicationDtlsByAppId, getPreferenceAppNames02, gettableData, postPreferenceAppNames, settingDataSelector, } from '../../Features/PreferenceMaster/PreferenceMaster.js'; import { getSession } from '../../Services/Others.js'; import { useAuth } from '../../AuthContext.jsx'; import { ApplicationPreferences } from '../../Features/BrachLogin/BranchLogin.js'; import './PreferenceList.scss'; const PreferenceList = () => { const { SadminuserAccess } = useAuth(); const dispatch = useDispatch(); const formRef = useRef(); const CompId = getSession('CompId'); const BranchId = getSession('BranchId'); const AppId = getSession('AppId'); const UserId = getSession('UserId'); const UserType = getSession('UserType'); const SalesReport = getSession('SalesReport'); const [open, setOpen] = useState(false); const [applicationValue, setApplicationValue] = useState(null); const [applicationName, setApplicationName] = useState(null); const [messageType, setMessageType] = useState(null); const [messageData, setMessageData] = useState(null); const [activeSection, setActiveSection] = useState('general'); const [empData, setEmpData] = useState(); const [preferenceConfigs, setPreferenceConfigs] = useState([]); const [addnewAccess, setAddNewAccess] = useState(true); const tableValue = useSelector(settingDataSelector); const appPreferences = useSelector(ApplicationPreferences); const bookingTypePreference = appPreferences?.find( (pre) => pre?.PreferredCatName === 'Booking Type' )?.PreferenceCatDetails; const dinePreference = bookingTypePreference?.find( (type) => type?.PreferredSubCatName?.toLowerCase() === 'dine in' && type?.PreferredStatus === 'Y' ); const settingsList = tableValue[0]?.SettingDtlDetails ?? preferenceConfigs; const allSettingsMap = {}; settingsList?.forEach(({ SettingIdName, SettingValue }) => { allSettingsMap[SettingIdName?.toLowerCase()] = SettingValue; }); console.log(allSettingsMap, 'allSettingsMapallSettingsMapallSettingsMap'); const buildInitialValues = () => ({ dashboard: allSettingsMap['dashboard'] === 'Y', shortcutkeys:allSettingsMap['shortcutkeys']==="Y", autoreceivestock: allSettingsMap['autoreceivestock'] === 'Y', decimal: allSettingsMap['decimal'] === 'Y', enabledModules: [ ...(allSettingsMap['dinein'] === 'Y' ? ['dinein'] : []), ...(allSettingsMap['estimation'] === 'Y' ? ['estimation'] : []), ], searchfocus: allSettingsMap['searchfocus'] === 'Y', addproductcard: allSettingsMap['addproductcard'] === 'Y', enabledFeatures: [ ...(allSettingsMap['parking'] === 'Y' ? ['parking'] : []), ...(allSettingsMap['offer'] === 'Y' ? ['offer'] : []), ], stockselection: allSettingsMap['stockselection'] === 'Y', weightasquantity: allSettingsMap['weightasquantity'] === 'Y', expiredselection: allSettingsMap['expiredselection'] === 'Y', 'sales mode': allSettingsMap['sales mode'] === 'Y', billitemsorder: allSettingsMap['billitemsorder'] === 'Y', preorderpricechange: allSettingsMap['preorderpricechange'] === 'Y', billOption: ['whatsapp', 'sms', 'email'].some( (k) => allSettingsMap[k] === 'Y' ) ? 'pdf' : 'print', pdfSendOptions: ['whatsapp', 'email', 'sms'].filter( (k) => allSettingsMap[k] === 'Y' ), tokenonly: allSettingsMap['tokenonly'] === 'Y', allproductindividualtoken: allSettingsMap['allproductindividualtoken'] === 'Y', printOptions: ['printuom', 'printlogo', 'printgreetings'].filter( (k) => allSettingsMap[k] === 'Y' ), mobilea4: allSettingsMap['mobilea4'] === 'Y', estimateprintheader: allSettingsMap['estimateprintheader'] === 'Y', scanlayout: allSettingsMap['scanlayout'] === 'Y', verifyproduct: allSettingsMap['verifyproduct'] === 'Y', customisedbillnumber: allSettingsMap['customisedbillnumber'] === 'Y', upiqr: allSettingsMap['upiqr'] === 'Y', lowstockreport: allSettingsMap['lowstockreport'] === 'Y', weeklysalesreport: allSettingsMap['weeklysalesreport'] === 'Y', notificationblink: allSettingsMap['notificationblink'] === 'Y', onlineindivdualslots: allSettingsMap['onlineindivdualslots'] === 'Y', editbill: allSettingsMap['editbill'] === 'Y', notification: [ ...(allSettingsMap['onbilltime'] === 'Y' ? ['onbilltime'] : []), ...(allSettingsMap['1daybefore'] === 'Y' ? ['1daybefore'] : []), ...(allSettingsMap['1hourbefore'] === 'Y' ? ['1hourbefore'] : []), ], BookingOptionsandmethod: [ ...(allSettingsMap['allow individual booking'] === 'Y' ? ['allow individual booking'] : []), ...(allSettingsMap['single booking only'] === 'Y' ? ['single booking only'] : []), ], }); const [initialValues, setInitialValues] = useState(); console.log(initialValues, 'initialValues'); useEffect(() => { dispatch( changeBreadCrumb({ items: [ { name: 'Home', link: `${import.meta.env.BASE_URL}app-page/home` }, ], }) ); fetchTableData(); const centerPageSub = document.querySelector('.centerPageSub'); if (centerPageSub) { centerPageSub.style.overflow = 'hidden'; } return () => { if (centerPageSub) { centerPageSub.style.overflow = ''; } }; }, []); useEffect(() => { if (settingsList) { const built = buildInitialValues(); setInitialValues(built); formRef.current?.setFieldsValue(built); } }, [settingsList]); useEffect(() => { if (UserType === 'Employee') fetchEmpAccess(); }, [UserType]); useEffect(() => { let hasAccess = ['Admin', 'Super Admin'].includes(UserType) || (UserType === 'Employee' && empData?.AddAccess === 'Y') || (UserType === 'Super Admin User' && SadminuserAccess?.find((e) => e.MenuName === 'Preference') ?.AddAccess === 'Y'); setAddNewAccess(!hasAccess); }, [empData, SadminuserAccess, UserType]); useEffect(() => { if (open) applicationDropdownData(); }, [open]); useEffect(() => { fetchTableData(); if (tableValue?.length === 0) { getPreferenceConfigs(); } }, []); useEffect(() => { const container = document.querySelector('.preferences-list'); if (!container) return; const handleScroll = () => { const sections = [ 'general', 'notifications', 'sales-page', 'print-preferences', ]; const containerTop = container.scrollTop; for (const sectionId of sections) { const element = document.getElementById(sectionId); if (element) { const elementTop = element.offsetTop - container.offsetTop; const elementBottom = elementTop + element.offsetHeight; if ( elementTop <= containerTop + 100 && elementBottom > containerTop ) { setActiveSection(sectionId); break; } } } }; container.addEventListener('scroll', handleScroll); return () => container.removeEventListener('scroll', handleScroll); }, []); const getPreferenceConfigs = async () => { const res = await dispatch(getPreferenceAppNames02())?.unwrap(); if (res?.data?.statusCode === 1) { setPreferenceConfigs( res?.data?.data?.map((i) => ({ SettingIdName: i.ConfigName, SettingId: i.ConfigId, Comments: 'Enter Comment', SettingValue: 'N', })) ); } }; const fetchEmpAccess = async () => { const data = { CompId, BranchId, AppId, EmpId: UserId }; const response = await dispatch(getEmpAccess(data)).unwrap(); const access = response?.data?.data?.[0]?.EmpAccessDetails?.find( (i) => i.ConfigName === 'Preference' ); setEmpData(access); }; const applicationDropdownData = async () => { const res = await dispatch(getApplicationDtlsByAppId({ AppId })).unwrap(); if (res?.data?.statusCode === 1) { setApplicationName(res?.data?.data?.[0]?.AppName); await fetchTableData(); } }; const fetchTableData = async () => { await dispatch(gettableData({ CompId, BranchId, AppId })).unwrap(); handleApplicationChange(AppId); }; const handleApplicationChange = async (value) => { setApplicationValue(value); formRef.current?.setFieldsValue({ AppId: value }); }; const onComplete = useCallback(() => { setMessageData(null); setMessageType(null); }, []); const handleMenuClick = (e, targetId) => { e.preventDefault(); setActiveSection(targetId); const container = document.querySelector('.preferences-list'); const target = document.getElementById(targetId); if (container && target) { const containerTop = container.offsetTop; const targetTop = target.offsetTop; const scrollPosition = targetTop - containerTop; container.scrollTo({ top: scrollPosition, behavior: 'smooth', }); } }; const handleSubmit = async () => { const values = formRef.current.getFieldsValue(); const settings = settingsList || []; console.log(values, 'values'); const updatedSettings = settings.map((setting) => { const key = setting.SettingIdName.toLowerCase(); const boolValues = { dashboard: values.dashboard, shortcutkeys:values.shortcutkeys, autoreceivestock: values.autoreceivestock, decimal: values.decimal, searchfocus: values.searchfocus, addproductcard: values.addproductcard, stockselection: values.stockselection, weightasquantity: values.weightasquantity, expiredselection: values.expiredselection, 'sales mode': values?.['sales mode'], billitemsorder: values.billitemsorder, preorderpricechange: values.preorderpricechange, tokenonly: values.tokenonly, allproductindividualtoken: values.allproductindividualtoken, mobilea4: values.mobilea4, estimateprintheader: values.estimateprintheader, scanlayout: values.scanlayout, verifyproduct: values.verifyproduct, customisedbillnumber: values.customisedbillnumber, upiqr: values.upiqr, lowstockreport: values.lowstockreport, weeklysalesreport: values.weeklysalesreport, notificationblink: values.notificationblink, editbill: values.editbill, }; const arrayValues = { dinein: values.enabledModules, estimation: values.enabledModules, parking: values.enabledFeatures, offer: values.enabledFeatures, whatsapp: values.pdfSendOptions, sms: values.pdfSendOptions, email: values.pdfSendOptions, printuom: values.printOptions, printlogo: values.printOptions, printgreetings: values.printOptions, onbilltime: values.notification, '1daybefore': values.notification, '1hourbefore': values.notification, 'allow individual booking': values['BookingOptionsandmethod'], 'single booking only': values['BookingOptionsandmethod'], }; const isChecked = key in boolValues ? boolValues[key] : (arrayValues[key]?.includes(key) ?? false); return { ConfigName: setting.SettingIdName, SettingId: setting.SettingId, Comments: setting.Comments || 'Enter Comment', SettingValue: isChecked ? 'Y' : 'N', }; }); const payload = { CompId, BranchId, CreatedBy: UserId, AppId, SettingDtlDetails: updatedSettings, }; const response = await dispatch(postPreferenceAppNames(payload)).unwrap(); if (response?.data?.statusCode === 1) { setMessageType('success'); setMessageData(response?.data?.response); await fetchTableData(); } else { setMessageType('error'); setMessageData(response?.data?.response); } }; const renderCheckbox = (name, label) => allSettingsMap[name] !== undefined && ( Yes ); return ( <>
handleMenuClick(e, 'general')} style={{ backgroundColor: activeSection === 'general' ? '#e6f3ff' : '#f4f4f4', color: activeSection === 'general' ? '#1890ff' : '#23378a', borderRight: activeSection === 'general' ? '2px solid #1890ff' : 'none', transform: activeSection === 'general' ? 'scale(1.07)' : 'scale(1)', }} > General
handleMenuClick(e, 'notifications')} style={{ backgroundColor: activeSection === 'notifications' ? '#e6f3ff' : '#f4f4f4', color: activeSection === 'notifications' ? '#1890ff' : '#23378a', borderRight: activeSection === 'notifications' ? '2px solid #1890ff' : 'none', transform: activeSection === 'notifications' ? 'scale(1.07)' : 'scale(1)', }} > Notifications
handleMenuClick(e, 'sales-page')} style={{ backgroundColor: activeSection === 'sales-page' ? '#e6f3ff' : '#f4f4f4', color: activeSection === 'sales-page' ? '#1890ff' : '#23378a', borderRight: activeSection === 'sales-page' ? '2px solid #1890ff' : 'none', transform: activeSection === 'sales-page' ? 'scale(1.07)' : 'scale(1)', }} > Sales Page
handleMenuClick(e, 'print-preferences')} style={{ backgroundColor: activeSection === 'print-preferences' ? '#e6f3ff' : '#f4f4f4', color: activeSection === 'print-preferences' ? '#1890ff' : '#23378a', borderRight: activeSection === 'print-preferences' ? '2px solid #1890ff' : 'none', transform: activeSection === 'print-preferences' ? 'scale(1.07)' : 'scale(1)', }} > Print Preferences
General
{renderCheckbox( 'dashboard', 'Stay on the Dashboard when you open the app?' )} {renderCheckbox( 'shortcutkeys', 'Would you like to enable the shortcut keys?' )} {renderCheckbox( 'autoreceivestock', 'Items received from godown. Add to stock Automatically ?' )} {renderCheckbox( 'decimal', 'Display values with 2 decimal places throughout the app?' )} {((allSettingsMap['dinein'] !== undefined && dinePreference) || allSettingsMap['estimation'] !== undefined) && ( {allSettingsMap['dinein'] !== undefined && dinePreference && ( Dine-In )} {allSettingsMap['estimation'] !== undefined && ( Estimation )} )} {(allSettingsMap['onbilltime'] !== undefined || allSettingsMap['1daybefore'] !== undefined || allSettingsMap['1hourbefore'] !== undefined) && ( {allSettingsMap['onbilltime'] !== undefined && ( On Bill Time )} {allSettingsMap['1daybefore'] !== undefined && ( 1 Day Before )} {allSettingsMap['1hourbefore'] !== undefined && ( 1 Hour Before )} )} {(allSettingsMap['onlineindividualslots'] !== undefined || allSettingsMap['allow individual booking'] !== undefined || allSettingsMap['single booking only'] !== undefined) && ( {allSettingsMap['allow individual booking'] !== undefined && ( Allow online individual booking )} {allSettingsMap['single booking only'] !== undefined && ( Allow single booking Only )} )}
Notifications{' '}
{renderCheckbox( 'lowstockreport', 'Do you want to receive a low-stock report every day?' )} {renderCheckbox( 'weeklysalesreport', 'Do you want to receive a Weekly Sales Report?' )} {renderCheckbox( 'notificationblink', 'Do you want to enable the low-stock blink notification?' )}
Sales Page
{renderCheckbox('searchfocus', 'Always focus the search bar?')} {renderCheckbox( 'addproductcard', 'Would you like to remove Add Product card from sales page ?' )} {(allSettingsMap['offer'] !== undefined || allSettingsMap['parking'] !== undefined) && ( {allSettingsMap['parking'] !== undefined && ( Parking )} {allSettingsMap['offer'] !== undefined && ( Offer )} )} {renderCheckbox('stockselection', 'Enable stock selection?')} {renderCheckbox('weightasquantity', 'Use Weight as Quantity?')} {renderCheckbox('expiredselection', 'Show product expiry date?')} {renderCheckbox('sales mode', 'Retail or Wholesale mode?')} {renderCheckbox( 'billitemsorder', 'Show selected items first in Order Summary?' )} {renderCheckbox( 'preorderpricechange', 'Prefer price update for pre-bookings?' )} {renderCheckbox( 'verifyproduct', 'Do you Want Verify the Product?' )} {renderCheckbox( 'customisedbillnumber', 'Do you Want Customised Bill Number?' )} {renderCheckbox('upiqr', 'Do you want to show the UPI QR code?')} {/* {renderCheckbox('editbill', 'Do you want to edit the previously created bill?')} */}
{['whatsapp', 'sms', 'email'].some( (k) => allSettingsMap[k] !== undefined ) && ( <> Print Bill Send PDF prev.billOption !== curr.billOption } > {({ getFieldValue }) => getFieldValue('billOption') === 'pdf' && ( {allSettingsMap['whatsapp'] !== undefined && ( WhatsApp )} {allSettingsMap['email'] !== undefined && ( Email )} {allSettingsMap['sms'] !== undefined && ( SMS )} ) } )} {renderCheckbox( 'tokenonly', 'Print only token for token-maintained products?' )} {allSettingsMap['allproductindividualtoken'] !== undefined && ( prev.tokenonly !== curr.tokenonly } > {({ getFieldValue }) => { const tokenOnlyTrue = getFieldValue('tokenonly'); return ( tokenOnlyTrue && renderCheckbox( 'allproductindividualtoken', 'Print individual token for each product?' ) ); }} )} {['printuom', 'printlogo', 'printgreetings'].some( (k) => allSettingsMap[k] !== undefined ) && ( {allSettingsMap['printuom'] !== undefined && ( Unit of Measure (UOM) )} {allSettingsMap['printlogo'] !== undefined && ( Branch Logo )} {allSettingsMap['printgreetings'] !== undefined && ( Greetings )} )} {renderCheckbox( 'mobilea4', 'Prefer printing in A4/A5 on mobile?' )} {renderCheckbox( 'estimateprintheader', 'Do you want to include a header when printing Estimate bills?' )} {renderCheckbox('scanlayout', 'Do you Want Scan Layout Page?')}
} />
); }; export default PreferenceList;