Skip to content

Commit 364176d

Browse files
committed
Refactor isClassNameOk into ClassNameComponent
1 parent c541ebf commit 364176d

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

src/reactComponents/ClassNameComponent.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as I18Next from 'react-i18next';
2525
import * as React from 'react';
2626
import * as commonStorage from '../storage/common_storage';
2727
import * as storageProject from '../storage/project';
28+
import * as storageNames from '../storage/names';
2829

2930
/** Props for the ClassNameComponent. */
3031
interface ClassNameComponentProps {
@@ -66,8 +67,15 @@ export default function ClassNameComponent(props: ClassNameComponentProps): Reac
6667
return;
6768
}
6869

69-
const {ok, error} = storageProject.isClassNameOk(props.project, newClassName, t);
70-
if (ok) {
70+
let error = '';
71+
72+
if (!storageNames.isValidClassName(newClassName)) {
73+
error = t('INVALID_CLASS_NAME', { name: newClassName });
74+
} else if (storageProject.findModuleByClassName(props.project!, newClassName) != null) {
75+
error = t('CLASS_NAME_ALREADY_EXISTS', { name: newClassName });
76+
}
77+
78+
if (!error) {
7179
clearError();
7280
props.onAddNewItem();
7381
} else {

src/storage/project.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -318,31 +318,6 @@ export async function copyModuleInProject(
318318
return newModulePath;
319319
}
320320

321-
/**
322-
* Checks if the proposed class name is valid and does not conflict with existing names in the project.
323-
* @param project The project to check against.
324-
* @param proposedClassName The proposed class name to validate.
325-
* @param t Translation function for internationalized error messages.
326-
* @returns An object containing a boolean `ok` indicating if the name is valid, and an `error` message if it is not.
327-
*/
328-
export function isClassNameOk(project: Project, proposedClassName: string, t: (key: string, params?: any) => string) {
329-
let ok = true;
330-
let error = '';
331-
332-
if (!storageNames.isValidClassName(proposedClassName)) {
333-
ok = false;
334-
error = t('INVALID_CLASS_NAME', { name: proposedClassName });
335-
} else if (findModuleByClassName(project, proposedClassName) != null) {
336-
ok = false;
337-
error = t('CLASS_NAME_ALREADY_EXISTS', { name: proposedClassName });
338-
}
339-
340-
return {
341-
ok: ok,
342-
error: error
343-
}
344-
}
345-
346321
/**
347322
* Returns the module in the given project with the given class name.
348323
*/

0 commit comments

Comments
 (0)