From 3606664a755a4957b0e3cd9a72f755a2251853c0 Mon Sep 17 00:00:00 2001 From: Vit Horacek <36083550+mountiny@users.noreply.github.com> Date: Thu, 5 Sep 2024 12:56:51 +0200 Subject: [PATCH] Merge pull request #48629 from ishpaul777/fix/gl-code-override-in-category-imoort [CP Staging] [FIX] Categories - GL code data disappears after importing same file that does not have GL code (cherry picked from commit e0b72a101e4536b304e4d49488b3a4c6f8b67d26) (CP triggered by mountiny) --- .../categories/ImportedCategoriesPage.tsx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/pages/workspace/categories/ImportedCategoriesPage.tsx b/src/pages/workspace/categories/ImportedCategoriesPage.tsx index 6e5528afcad6..11e309f00c88 100644 --- a/src/pages/workspace/categories/ImportedCategoriesPage.tsx +++ b/src/pages/workspace/categories/ImportedCategoriesPage.tsx @@ -28,6 +28,7 @@ function ImportedCategoriesPage({route}: ImportedCategoriesPageProps) { const {containsHeader} = spreadsheet ?? {}; const [isValidationEnabled, setIsValidationEnabled] = useState(false); const policyID = route.params.policyID; + const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`); const policy = usePolicy(policyID); const columnNames = generateColumnNames(spreadsheet?.data?.length ?? 0); @@ -88,18 +89,22 @@ function ImportedCategoriesPage({route}: ImportedCategoriesPageProps) { const categoriesNames = spreadsheet?.data[categoriesNamesColumn].map((name) => name); const categoriesEnabled = categoriesEnabledColumn !== -1 ? spreadsheet?.data[categoriesEnabledColumn].map((enabled) => enabled) : []; const categoriesGLCode = categoriesGLCodeColumn !== -1 ? spreadsheet?.data[categoriesGLCodeColumn].map((glCode) => glCode) : []; - const categories = categoriesNames?.slice(containsHeader ? 1 : 0).map((name, index) => ({ - name, - enabled: categoriesEnabledColumn !== -1 ? categoriesEnabled?.[containsHeader ? index + 1 : index] === 'true' : true, - // eslint-disable-next-line @typescript-eslint/naming-convention - 'GL Code': categoriesGLCodeColumn !== -1 ? categoriesGLCode?.[containsHeader ? index + 1 : index] ?? '' : '', - })); + const categories = categoriesNames?.slice(containsHeader ? 1 : 0).map((name, index) => { + const categoryAlreadyExists = policyCategories?.[name]; + const existingGLCodeOrDefault = categoryAlreadyExists?.['GL Code'] ?? ''; + return { + name, + enabled: categoriesEnabledColumn !== -1 ? categoriesEnabled?.[containsHeader ? index + 1 : index] === 'true' : true, + // eslint-disable-next-line @typescript-eslint/naming-convention + 'GL Code': categoriesGLCodeColumn !== -1 ? categoriesGLCode?.[containsHeader ? index + 1 : index] ?? '' : existingGLCodeOrDefault, + }; + }); if (categories) { setIsImportingCategories(true); importPolicyCategories(policyID, categories); } - }, [validate, spreadsheet, containsHeader, policyID]); + }, [validate, spreadsheet, containsHeader, policyID, policyCategories]); const spreadsheetColumns = spreadsheet?.data; if (!spreadsheetColumns) {