Skip to content

Commit

Permalink
Merge pull request #48629 from ishpaul777/fix/gl-code-override-in-cat…
Browse files Browse the repository at this point in the history
…egory-imoort

[CP Staging] [FIX] Categories - GL code data disappears after importing same file that does not have GL code

(cherry picked from commit e0b72a1)

(CP triggered by mountiny)
  • Loading branch information
mountiny authored and OSBotify committed Sep 5, 2024
1 parent c9d3bc7 commit 3606664
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/pages/workspace/categories/ImportedCategoriesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3606664

Please sign in to comment.