From 87563857b7ecce70b875ebeb8f4ac0fe4a1434f1 Mon Sep 17 00:00:00 2001 From: abzokhattab Date: Sat, 23 Nov 2024 04:09:43 +0100 Subject: [PATCH 1/4] Check for empty names when importing categories using spreadsheet --- src/languages/en.ts | 1 + src/languages/es.ts | 1 + .../workspace/categories/ImportedCategoriesPage.tsx | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/languages/en.ts b/src/languages/en.ts index 3e1ea7f0d7cc..799088424e5a 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -768,6 +768,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.`, 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.'), diff --git a/src/languages/es.ts b/src/languages/es.ts index d28a19fbb1be..77c9b4e62acd 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -763,6 +763,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.'), diff --git a/src/pages/workspace/categories/ImportedCategoriesPage.tsx b/src/pages/workspace/categories/ImportedCategoriesPage.tsx index 138c7ea681df..3da3b82be758 100644 --- a/src/pages/workspace/categories/ImportedCategoriesPage.tsx +++ b/src/pages/workspace/categories/ImportedCategoriesPage.tsx @@ -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); From ccb596e6a68baabdb85fda495a4384ee97ec743d Mon Sep 17 00:00:00 2001 From: abzokhattab Date: Wed, 27 Nov 2024 12:44:52 +0100 Subject: [PATCH 2/4] fixing the imported categories number in the success prompt --- src/libs/actions/Policy/Category.ts | 49 +++++++++++++++++------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/libs/actions/Policy/Category.ts b/src/libs/actions/Policy/Category.ts index 2f663ac204d2..61bd851712fd 100644 --- a/src/libs/actions/Policy/Category.ts +++ b/src/libs/actions/Policy/Category.ts @@ -1,5 +1,6 @@ import lodashCloneDeep from 'lodash/cloneDeep'; import lodashUnion from 'lodash/union'; + import type {NullishDeep, OnyxCollection, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as API from '@libs/API'; @@ -27,8 +28,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'; @@ -522,7 +523,15 @@ function createPolicyCategory(policyID: string, categoryName: string) { } function importPolicyCategories(policyID: string, categories: PolicyCategory[]) { - const onyxData = updateImportSpreadsheetData(categories.length); + const uniqueCategories = categories.reduce>((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, @@ -1351,28 +1360,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, }; From 2b6a9558c906f90006c0f0a29d4d3fa6f6a3998e Mon Sep 17 00:00:00 2001 From: abzokhattab Date: Wed, 27 Nov 2024 13:18:58 +0100 Subject: [PATCH 3/4] fixing lint errors --- src/libs/actions/Policy/Category.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Policy/Category.ts b/src/libs/actions/Policy/Category.ts index 61bd851712fd..186cd4672d03 100644 --- a/src/libs/actions/Policy/Category.ts +++ b/src/libs/actions/Policy/Category.ts @@ -1,6 +1,5 @@ import lodashCloneDeep from 'lodash/cloneDeep'; import lodashUnion from 'lodash/union'; - import type {NullishDeep, OnyxCollection, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as API from '@libs/API'; From 43c3979ec05fc6db02cf07909b47ef99fbcdebe5 Mon Sep 17 00:00:00 2001 From: Abdelrahman Khattab Date: Wed, 27 Nov 2024 15:53:47 +0100 Subject: [PATCH 4/4] minor change in the english translation --- src/languages/en.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/en.ts b/src/languages/en.ts index 799088424e5a..f60ece901b86 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -768,7 +768,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.`, + 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.'),