From b965ea189079b7a5a423e02251fa8323b9a793f9 Mon Sep 17 00:00:00 2001 From: aatbip Date: Fri, 22 Mar 2024 13:28:52 +0545 Subject: [PATCH] removed start to try fixing autofill issue in prod --- src/app/components/EditorInterface.tsx | 4 +- src/hooks/useAppData.tsx | 132 +++++++++++-------------- 2 files changed, 58 insertions(+), 78 deletions(-) diff --git a/src/app/components/EditorInterface.tsx b/src/app/components/EditorInterface.tsx index ab6cf70..090a9a8 100644 --- a/src/app/components/EditorInterface.tsx +++ b/src/app/components/EditorInterface.tsx @@ -175,7 +175,7 @@ const EditorInterface = ({ settings, token }: IEditorInterface) => { ) const c = template(appData) setTimeout(() => { - editor?.chain().focus('start').setContent(c).run() + editor?.chain().focus().setContent(c).run() }) } else { setTimeout(() => { @@ -185,7 +185,7 @@ const EditorInterface = ({ settings, token }: IEditorInterface) => { .setContent(appState?.appState.originalTemplate as string) .run() }) - editor?.chain().focus('start').run() + editor?.chain().focus().run() } }, [ appState?.appState.selectedClient, diff --git a/src/hooks/useAppData.tsx b/src/hooks/useAppData.tsx index 413f4ac..4100db3 100644 --- a/src/hooks/useAppData.tsx +++ b/src/hooks/useAppData.tsx @@ -1,11 +1,4 @@ -import { - createContext, - PropsWithChildren, - useContext, - useEffect, - useMemo, - useState, -} from 'react' +import { createContext, PropsWithChildren, useContext, useMemo } from 'react' import { useAppState } from '@/hooks/useAppState' import { IClient, INotification } from '@/types/interfaces' import Handlebars from 'handlebars' @@ -28,90 +21,77 @@ export const useAppData = (template: string) => { export const AppDataProvider = ({ children }: PropsWithChildren) => { const appState = useAppState() - const [data, setData] = useState<{ - client: IClient | undefined - invoice: { count: number | undefined } - task: { count: number | undefined } - form: { count: number | undefined } - contract: { count: number | undefined } - }>({ - client: undefined, - invoice: { count: undefined }, - task: { count: undefined }, - form: { count: undefined }, - contract: { count: undefined }, - }) - useEffect(() => { - ;(() => { - const _client = appState?.appState.clientList.find( - (el) => el.id === (appState?.appState.selectedClient as IClient)?.id, - ) - //add comma separator for custom fields - const customFields: any = _client?.customFields - // Iterate through each key in customFields - for (const key in customFields) { - // Check if the value is an array and if the key exists in allCustomFields - if ( - Array.isArray(customFields[key]) && - appState?.appState.customFields.some((field) => field.key === key) - ) { - // Map the values to their corresponding labels - customFields[key] = customFields[key].map((value: string[]) => { - const option: any = (appState?.appState?.customFields as any) - .find((field: any) => field.key === key) - .options.find((opt: any) => opt.key === value) - return option ? ' ' + option.label : ' ' + value - }) - } + const data = useMemo(() => { + if (!appState) { + return null + } + const _client = appState.appState.clientList.find( + (el) => el.id === (appState.appState.selectedClient as IClient)?.id, + ) + //add comma separator for custom fields + const customFields: any = _client?.customFields + + // Iterate through each key in customFields + for (const key in customFields) { + // Check if the value is an array and if the key exists in allCustomFields + if ( + Array.isArray(customFields[key]) && + appState?.appState.customFields.some((field) => field.key === key) + ) { + // Map the values to their corresponding labels + customFields[key] = customFields[key].map((value: string[]) => { + const option: any = (appState?.appState?.customFields as any) + .find((field: any) => field.key === key) + .options.find((opt: any) => opt.key === value) + return option ? ' ' + option.label : ' ' + value + }) } + } - let count = 0 - appState?.appState.settings?.notifications?.map((el) => { - if (el.show) { - if (appState?.appState.notifications) { - count += - appState?.appState.notifications[el.key as keyof INotification] - } + let count = 0 + appState?.appState.settings?.notifications?.map((el) => { + if (el.show) { + if (appState?.appState.notifications) { + count += + appState?.appState.notifications[el.key as keyof INotification] } - }) + } + }) - const task = { count } + const task = { count } - const invoice = { - count: appState?.appState.notifications?.billing, - } + const invoice = { + count: appState?.appState.notifications?.billing, + } - const form = { - count: appState?.appState.notifications?.forms, - } + const form = { + count: appState?.appState.notifications?.forms, + } - const contract = { - count: appState?.appState.notifications?.contracts, - } + const contract = { + count: appState?.appState.notifications?.contracts, + } - const client = { - ..._client, - ...customFields, - company: appState?.appState.selectedClientCompanyName, - } + const client = { + ..._client, + ...customFields, + company: appState?.appState.selectedClientCompanyName, + } - setData({ - client, - invoice, - task, - form, - contract, - }) - })() + return { + client, + invoice, + task, + form, + contract, + } }, [ appState?.appState.selectedClient, appState?.appState.selectedClientCompanyName, appState?.appState.notifications, ]) - console.log('data', data) - return ( {children} )