validate prefix and make saveFile async (#915)

Co-authored-by: burnettk <burnettk@users.noreply.github.com>
This commit is contained in:
Kevin Burnett 2024-01-23 14:08:44 -08:00 committed by GitHub
parent 6b0625ea5d
commit f462457f92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 32 additions and 7 deletions

View File

@ -64,6 +64,9 @@ export default function ReactFormBuilder({
const [fetchFailed, setFetchFailed] = useState<boolean>(false); const [fetchFailed, setFetchFailed] = useState<boolean>(false);
const [ready, setReady] = useState<boolean>(false); const [ready, setReady] = useState<boolean>(false);
const [filenameBaseInvalid, setFilenameBaseInvalid] =
useState<boolean>(false);
const [strSchema, setStrSchema] = useState<string>(''); const [strSchema, setStrSchema] = useState<string>('');
const [debouncedStrSchema] = useDebounce(strSchema, 500); const [debouncedStrSchema] = useDebounce(strSchema, 500);
const [strUI, setStrUI] = useState<string>(''); const [strUI, setStrUI] = useState<string>('');
@ -100,7 +103,7 @@ export default function ReactFormBuilder({
} }
const saveFile = useCallback( const saveFile = useCallback(
(file: File, create: boolean = false) => { (file: File, create: boolean = false, callback: Function | null = null) => {
let httpMethod = 'PUT'; let httpMethod = 'PUT';
let url = `/process-models/${processModelId}/files`; let url = `/process-models/${processModelId}/files`;
if (create) { if (create) {
@ -114,7 +117,11 @@ export default function ReactFormBuilder({
HttpService.makeCallToBackend({ HttpService.makeCallToBackend({
path: url, path: url,
successCallback: () => {}, successCallback: () => {
if (callback) {
callback();
}
},
failureCallback: (e: any) => { failureCallback: (e: any) => {
setErrorMessage(`Failed to save file: '${fileName}'. ${e.message}`); setErrorMessage(`Failed to save file: '${fileName}'. ${e.message}`);
}, },
@ -125,12 +132,28 @@ export default function ReactFormBuilder({
[processModelId, fileName] [processModelId, fileName]
); );
const hasValidName = (identifierToCheck: string) => {
return identifierToCheck.match(/^[a-z0-9][0-9a-z-]+[a-z0-9]$/);
};
const createFiles = (base: string) => { const createFiles = (base: string) => {
saveFile(new File(['{}'], base + SCHEMA_EXTENSION), true); if (hasValidName(base)) {
saveFile(new File(['{}'], base + UI_EXTENSION), true); // meaning it switched from invalid to valid
saveFile(new File(['{}'], base + DATA_EXTENSION), true); if (filenameBaseInvalid) {
setFilenameBaseInvalid(false);
}
} else {
setFilenameBaseInvalid(true);
return;
}
saveFile(new File(['{}'], base + SCHEMA_EXTENSION), true, () => {
saveFile(new File(['{}'], base + UI_EXTENSION), true, () => {
saveFile(new File(['{}'], base + DATA_EXTENSION), true, () => {
setBaseFileName(base); setBaseFileName(base);
onFileNameSet(base + SCHEMA_EXTENSION); onFileNameSet(base + SCHEMA_EXTENSION);
});
});
});
}; };
const isReady = () => { const isReady = () => {
@ -373,6 +396,8 @@ export default function ReactFormBuilder({
<TextInput <TextInput
id="file_name" id="file_name"
labelText="Name:" labelText="Name:"
invalidText="Name is required, must be at least three characters, and must be all lowercase characters and hyphens."
invalid={filenameBaseInvalid}
value={newFileName} value={newFileName}
onChange={(event: any) => { onChange={(event: any) => {
setNewFileName(event.srcElement.value); setNewFileName(event.srcElement.value);