-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Simplify file uploader component (#13804)
Co-authored-by: Konrad-Simso <konrad.simso@digdir.no>
- Loading branch information
1 parent
1b47df6
commit 4cc0977
Showing
19 changed files
with
375 additions
and
293 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...ment/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/XSDUpload/FileNameError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type FileNameError = 'invalidFileName' | 'fileExists'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...-development/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/XSDUpload/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './XSDUpload'; |
16 changes: 16 additions & 0 deletions
16
...features/dataModelling/SchemaEditorWithToolbar/TopToolbar/XSDUpload/useValidationAlert.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { useCallback } from 'react'; | ||
import type { FileNameError } from './FileNameError'; | ||
|
||
export const useValidationAlert = () => { | ||
const { t } = useTranslation(); | ||
|
||
return useCallback( | ||
(error: FileNameError): void => { | ||
if (error === 'invalidFileName') { | ||
alert(t('app_data_modelling.upload_xsd_invalid_name_error')); | ||
} | ||
}, | ||
[t], | ||
); | ||
}; |
49 changes: 49 additions & 0 deletions
49
...nt/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/XSDUpload/validationUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { ApplicationMetadata } from 'app-shared/types/ApplicationMetadata'; | ||
import { removeExtension } from 'app-shared/utils/filenameUtils'; | ||
import type { FileNameError } from './FileNameError'; | ||
|
||
export const doesFileExistInMetadataWithClassRef = ( | ||
appMetadata: ApplicationMetadata, | ||
fileNameWithoutExtension: string, | ||
): boolean => { | ||
return Boolean( | ||
appMetadata.dataTypes | ||
?.filter((dataType) => dataType.appLogic?.classRef !== undefined) | ||
.find((dataType) => dataType.id === fileNameWithoutExtension), | ||
); | ||
}; | ||
|
||
export const doesFileExistInMetadataWithoutClassRef = ( | ||
appMetadata: ApplicationMetadata, | ||
fileNameWithoutExtension: string, | ||
): boolean => { | ||
return Boolean( | ||
appMetadata.dataTypes | ||
?.filter((dataType) => dataType.appLogic?.classRef === undefined) | ||
.find((dataType) => dataType.id.toLowerCase() === fileNameWithoutExtension.toLowerCase()), | ||
); | ||
}; | ||
|
||
export const findFileNameError = ( | ||
fileName: string, | ||
appMetadata: ApplicationMetadata, | ||
): FileNameError | null => { | ||
const fileNameWithoutExtension = removeExtension(fileName); | ||
if (!isNameFormatValid(fileNameWithoutExtension)) { | ||
return 'invalidFileName'; | ||
} else if (doesFileExistInMetadata(appMetadata, fileNameWithoutExtension)) { | ||
return 'fileExists'; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
const isNameFormatValid = (fileNameWithoutExtension: string): boolean => { | ||
const fileNameRegex: RegExp = /^[a-zA-Z][a-zA-Z0-9_.\-æÆøØåÅ ]*$/; | ||
return Boolean(fileNameWithoutExtension.match(fileNameRegex)); | ||
}; | ||
|
||
const doesFileExistInMetadata = ( | ||
appMetadata: ApplicationMetadata, | ||
fileNameWithoutExtension: string, | ||
): boolean => appMetadata.dataTypes?.some((dataType) => dataType.id === fileNameWithoutExtension); |
49 changes: 0 additions & 49 deletions
49
...elopment/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/useValidateFileName.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.