Skip to content

Commit f43708d

Browse files
committed
Refactor validateInputs
1 parent 05190d4 commit f43708d

File tree

3 files changed

+37
-56
lines changed

3 files changed

+37
-56
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
@@ -265,7 +266,7 @@ export class CodeSnippetEditor extends ReactWidget {
265266
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_LANG_INPUT}`
266267
) as HTMLSelectElement).value;
267268

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

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

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

src/CodeSnippetInputDialog.ts

Lines changed: 5 additions & 29 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,
@@ -220,33 +223,6 @@ export function isValidFileName(name: string): boolean {
220223
return name.length > 0 && !validNameExp.test(name);
221224
}
222225

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

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)