Skip to content

Commit 7372028

Browse files
committed
Refactor validateInputs
1 parent 3584c47 commit 7372028

File tree

3 files changed

+37
-57
lines changed

3 files changed

+37
-57
lines changed

src/CodeSnippetEditor.tsx

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { CodeSnippetWidget } from './CodeSnippetWidget';
3333
import { SUPPORTED_LANGUAGES } from './CodeSnippetLanguages';
3434
import { CodeSnippetEditorTags } from './CodeSnippetEditorTags';
3535
import { saveOverWriteFile } from './CodeSnippetInputDialog';
36+
import { validateInputs } from './CodeSnippetUtilities';
3637

3738
/**
3839
* CSS style classes
@@ -266,7 +267,7 @@ export class CodeSnippetEditor extends ReactWidget {
266267
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_LANG_INPUT}`
267268
) as HTMLSelectElement).value;
268269

269-
const validity = this.validateInputs(name, description, language);
270+
const validity = validateInputs(name, description, language);
270271
if (validity) {
271272
this.updateSnippet().then((value) => {
272273
if (value) {
@@ -342,37 +343,12 @@ export class CodeSnippetEditor extends ReactWidget {
342343
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_LANG_INPUT}`
343344
) as HTMLSelectElement).value;
344345

345-
const validity = this.validateInputs(name, description, language);
346+
const validity = validateInputs(name, description, language);
346347
if (validity) {
347348
this.updateSnippet();
348349
}
349350
}
350351

351-
private validateInputs(
352-
name: string,
353-
description: string,
354-
language: string
355-
): boolean {
356-
let status = true;
357-
let message = '';
358-
if (name === '') {
359-
message += 'Name must be filled out\n';
360-
status = false;
361-
}
362-
if (language === '') {
363-
message += 'Language must be filled out';
364-
status = false;
365-
}
366-
if (!SUPPORTED_LANGUAGES.includes(language)) {
367-
message += 'Language must be one of the options';
368-
status = false;
369-
}
370-
if (status === false) {
371-
alert(message);
372-
}
373-
return status;
374-
}
375-
376352
async updateSnippet(): Promise<boolean> {
377353
const name = (document.querySelector(
378354
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_NAME_INPUT}`

src/CodeSnippetInputDialog.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ICodeSnippet, CodeSnippetService } from './CodeSnippetService';
1313
import { CodeSnippetWidget } from './CodeSnippetWidget';
1414
import { SUPPORTED_LANGUAGES } from './CodeSnippetLanguages';
1515
import { showMessage } from './ConfirmMessage';
16-
// import { showCodeSnippetForm } from './CodeSnippetForm'; //CodeSnippetForm } from './CodeSnippetForm';
16+
import { validateInputs } from './CodeSnippetUtilities';
1717

1818
import checkSVGstr from '../style/icon/jupyter_checkmark.svg';
1919

@@ -106,7 +106,10 @@ export function showInputDialog(
106106
return null;
107107
}
108108

109-
if (validateForm(result) === false) {
109+
if (
110+
validateInputs(result.value[0], result.value[1], result.value[2]) ===
111+
false
112+
) {
110113
showInputDialog(
111114
codeSnippetWidget,
112115
tags,
@@ -221,34 +224,6 @@ export function isValidFileName(name: string): boolean {
221224
return name.length > 0 && !validNameExp.test(name);
222225
}
223226

224-
/**
225-
* Test whether user typed in all required inputs.
226-
*/
227-
export function validateForm(input: Dialog.IResult<string[]>): boolean {
228-
let status = true;
229-
let message = '';
230-
const name = input.value[0];
231-
const language = input.value[2];
232-
233-
if (name === '') {
234-
message += 'Name must be filled out\n';
235-
status = false;
236-
}
237-
if (language === '') {
238-
message += 'Language must be filled out\n';
239-
status = false;
240-
}
241-
if (!SUPPORTED_LANGUAGES.includes(language)) {
242-
message += 'Language must be one of the options';
243-
status = false;
244-
}
245-
// TODO: change it to a better UI
246-
if (status === false) {
247-
alert(message);
248-
}
249-
return status;
250-
}
251-
252227
/**
253228
* A widget used to get input data.
254229
*/

src/CodeSnippetUtilities.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { SUPPORTED_LANGUAGES } from './CodeSnippetLanguages';
2+
3+
/**
4+
* Test whether user typed in all required inputs.
5+
*/
6+
export function validateInputs(
7+
name: string,
8+
description: string,
9+
language: string
10+
): boolean {
11+
let status = true;
12+
let message = '';
13+
if (name === '') {
14+
message += 'Name must be filled out\n';
15+
status = false;
16+
}
17+
if (language === '') {
18+
message += 'Language must be filled out';
19+
status = false;
20+
}
21+
if (!SUPPORTED_LANGUAGES.includes(language)) {
22+
message += 'Language must be one of the options';
23+
status = false;
24+
}
25+
if (status === false) {
26+
alert(message);
27+
}
28+
return status;
29+
}

0 commit comments

Comments
 (0)