@@ -28,6 +28,7 @@ function ImportedCategoriesPage({route}: ImportedCategoriesPageProps) {
28
28
const { containsHeader} = spreadsheet ?? { } ;
29
29
const [ isValidationEnabled , setIsValidationEnabled ] = useState ( false ) ;
30
30
const policyID = route . params . policyID ;
31
+ const [ policyCategories ] = useOnyx ( `${ ONYXKEYS . COLLECTION . POLICY_CATEGORIES } ${ policyID } ` ) ;
31
32
const policy = usePolicy ( policyID ) ;
32
33
const columnNames = generateColumnNames ( spreadsheet ?. data ?. length ?? 0 ) ;
33
34
@@ -88,18 +89,22 @@ function ImportedCategoriesPage({route}: ImportedCategoriesPageProps) {
88
89
const categoriesNames = spreadsheet ?. data [ categoriesNamesColumn ] . map ( ( name ) => name ) ;
89
90
const categoriesEnabled = categoriesEnabledColumn !== - 1 ? spreadsheet ?. data [ categoriesEnabledColumn ] . map ( ( enabled ) => enabled ) : [ ] ;
90
91
const categoriesGLCode = categoriesGLCodeColumn !== - 1 ? spreadsheet ?. data [ categoriesGLCodeColumn ] . map ( ( glCode ) => glCode ) : [ ] ;
91
- const categories = categoriesNames ?. slice ( containsHeader ? 1 : 0 ) . map ( ( name , index ) => ( {
92
- name,
93
- enabled : categoriesEnabledColumn !== - 1 ? categoriesEnabled ?. [ containsHeader ? index + 1 : index ] === 'true' : true ,
94
- // eslint-disable-next-line @typescript-eslint/naming-convention
95
- 'GL Code' : categoriesGLCodeColumn !== - 1 ? categoriesGLCode ?. [ containsHeader ? index + 1 : index ] ?? '' : '' ,
96
- } ) ) ;
92
+ const categories = categoriesNames ?. slice ( containsHeader ? 1 : 0 ) . map ( ( name , index ) => {
93
+ const categoryAlreadyExists = policyCategories ?. [ name ] ;
94
+ const existingGLCodeOrDefault = categoryAlreadyExists ?. [ 'GL Code' ] ?? '' ;
95
+ return {
96
+ name,
97
+ enabled : categoriesEnabledColumn !== - 1 ? categoriesEnabled ?. [ containsHeader ? index + 1 : index ] === 'true' : true ,
98
+ // eslint-disable-next-line @typescript-eslint/naming-convention
99
+ 'GL Code' : categoriesGLCodeColumn !== - 1 ? categoriesGLCode ?. [ containsHeader ? index + 1 : index ] ?? '' : existingGLCodeOrDefault ,
100
+ } ;
101
+ } ) ;
97
102
98
103
if ( categories ) {
99
104
setIsImportingCategories ( true ) ;
100
105
importPolicyCategories ( policyID , categories ) ;
101
106
}
102
- } , [ validate , spreadsheet , containsHeader , policyID ] ) ;
107
+ } , [ validate , spreadsheet , containsHeader , policyID , policyCategories ] ) ;
103
108
104
109
const spreadsheetColumns = spreadsheet ?. data ;
105
110
if ( ! spreadsheetColumns ) {
0 commit comments