Skip to content

Commit

Permalink
Merge pull request Expensify#52992 from abzokhattab/check-for-empty-n…
Browse files Browse the repository at this point in the history
…ames-when-uploading-category-spreedsheet

Check for empty names when importing categories using spreadsheet
  • Loading branch information
mountiny authored Nov 28, 2024
2 parents 089e8f3 + 43c3979 commit d7aeb7f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ const translations = {
column: ({name}: SpreadSheetColumnParams) => `Column ${name}`,
fieldNotMapped: ({fieldName}: SpreadFieldNameParams) => `Oops! A required field ("${fieldName}") hasn't been mapped. Please review and try again.`,
singleFieldMultipleColumns: ({fieldName}: SpreadFieldNameParams) => `Oops! You've mapped a single field ("${fieldName}") to multiple columns. Please review and try again.`,
emptyMappedField: ({fieldName}: SpreadFieldNameParams) => `Oops! The field ("${fieldName}") contains one or more empty values. Please review and try again.`,
importSuccessfullTitle: 'Import successful',
importCategoriesSuccessfullDescription: ({categories}: SpreadCategoriesParams) => (categories > 1 ? `${categories} categories have been added.` : '1 category has been added.'),
importMembersSuccessfullDescription: ({members}: ImportMembersSuccessfullDescriptionParams) => (members > 1 ? `${members} members have been added.` : '1 member has been added.'),
Expand Down
1 change: 1 addition & 0 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ const translations = {
column: ({name}: SpreadSheetColumnParams) => `Columna ${name}`,
fieldNotMapped: ({fieldName}: SpreadFieldNameParams) => `¡Vaya! Un campo obligatorio ("${fieldName}") no ha sido mapeado. Por favor, revisa e inténtalo de nuevo.`,
singleFieldMultipleColumns: ({fieldName}: SpreadFieldNameParams) => `¡Vaya! Has mapeado un solo campo ("${fieldName}") a varias columnas. Por favor, revisa e inténtalo de nuevo.`,
emptyMappedField: ({fieldName}: SpreadFieldNameParams) => `¡Vaya! El campo ("${fieldName}") contiene uno o más valores vacíos. Por favor, revísalo e inténtalo de nuevo.`,
importFailedTitle: 'Fallo en la importación',
importFailedDescription: 'Por favor, asegúrate de que todos los campos estén llenos correctamente e inténtalo de nuevo. Si el problema persiste, por favor contacta a Concierge.',
importCategoriesSuccessfullDescription: ({categories}: SpreadCategoriesParams) => (categories > 1 ? `Se han agregado ${categories} categorías.` : 'Se ha agregado 1 categoría.'),
Expand Down
48 changes: 28 additions & 20 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {translateLocal} from '@libs/Localize';
import Log from '@libs/Log';
import enhanceParameters from '@libs/Network/enhanceParameters';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -522,7 +522,15 @@ function createPolicyCategory(policyID: string, categoryName: string) {
}

function importPolicyCategories(policyID: string, categories: PolicyCategory[]) {
const onyxData = updateImportSpreadsheetData(categories.length);
const uniqueCategories = categories.reduce<Record<string, PolicyCategory>>((acc, category) => {
if (!category.name) {
return acc;
}
acc[category.name] = category;
return acc;
}, {});
const categoriesLength = Object.keys(uniqueCategories).length;
const onyxData = updateImportSpreadsheetData(categoriesLength);

const parameters = {
policyID,
Expand Down Expand Up @@ -1351,28 +1359,28 @@ function getPolicyCategoriesData(policyID: string) {
}

export {
getPolicyCategories,
openPolicyCategoriesPage,
buildOptimisticPolicyCategories,
buildOptimisticPolicyRecentlyUsedCategories,
setWorkspaceCategoryEnabled,
setPolicyCategoryDescriptionRequired,
setWorkspaceCategoryDescriptionHint,
setWorkspaceRequiresCategory,
setPolicyCategoryPayrollCode,
createPolicyCategory,
renamePolicyCategory,
setPolicyCategoryGLCode,
clearCategoryErrors,
enablePolicyCategories,
setPolicyCustomUnitDefaultCategory,
createPolicyCategory,
deleteWorkspaceCategories,
buildOptimisticPolicyCategories,
setPolicyCategoryReceiptsRequired,
downloadCategoriesCSV,
enablePolicyCategories,
getPolicyCategories,
getPolicyCategoriesData,
importPolicyCategories,
openPolicyCategoriesPage,
removePolicyCategoryReceiptsRequired,
setPolicyCategoryMaxAmount,
renamePolicyCategory,
setPolicyCategoryApprover,
setPolicyCategoryDescriptionRequired,
setPolicyCategoryGLCode,
setPolicyCategoryMaxAmount,
setPolicyCategoryPayrollCode,
setPolicyCategoryReceiptsRequired,
setPolicyCategoryTax,
importPolicyCategories,
downloadCategoriesCSV,
getPolicyCategoriesData,
setPolicyCustomUnitDefaultCategory,
setWorkspaceCategoryDescriptionHint,
setWorkspaceCategoryEnabled,
setWorkspaceRequiresCategory,
};
9 changes: 8 additions & 1 deletion src/pages/workspace/categories/ImportedCategoriesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ function ImportedCategoriesPage({route}: ImportedCategoriesPageProps) {
} else {
const duplicate = findDuplicate(columns);
const duplicateColumn = columnRoles.find((role) => role.value === duplicate);

const categoriesNamesColumn = columns.findIndex((column) => column === CONST.CSV_IMPORT_COLUMNS.NAME);
const categoriesNames = categoriesNamesColumn !== -1 ? spreadsheet?.data[categoriesNamesColumn] : [];
const containsEmptyName = categoriesNames?.some((name, index) => (!containsHeader || index > 0) && !name?.toString().trim());

if (duplicateColumn) {
errors.duplicates = translate('spreadsheet.singleFieldMultipleColumns', {fieldName: duplicateColumn.text});
} else if (containsEmptyName) {
errors.emptyNames = translate('spreadsheet.emptyMappedField', {fieldName: translate('common.name')});
} else {
errors = {};
}
}
return errors;
}, [requiredColumns, spreadsheet?.columns, translate, columnRoles]);
}, [spreadsheet?.columns, spreadsheet?.data, requiredColumns, translate, columnRoles, containsHeader]);

const importCategories = useCallback(() => {
setIsValidationEnabled(true);
Expand Down

0 comments on commit d7aeb7f

Please sign in to comment.