Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading