Skip to content

Commit

Permalink
Add input box validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JennieJi committed Apr 19, 2020
1 parent 2df6357 commit d30f781
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 70 deletions.
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@
"command": "gitlabSnippets.viewSnippet",
"title": "View"
},
{
"command": "gitlabSnippets.toggleHost",
"title": "Expand/Collapse"
},
{
"command": "gitlabSnippets.exploreMore",
"title": "More"
Expand All @@ -86,13 +82,30 @@
"menus": {
"commandPalette": [
{
"command": "gitlabSnippets.publish"
"command": "gitlabSnippets.publish",
"when": "editorFocus"
},
{
"command": "gitlabSnippets.addHost"
},
{
"command": "gitlabSnippets.star",
"when": "false"
},
{
"command": "gitlabSnippets.starById"
},
{
"command": "gitlabSnippets.unstar",
"when": "false"
},
{
"command": "gitlabSnippets.download",
"when": "false"
},
{
"command": "gitlabSnippets.viewSnippet",
"when": "false"
}
],
"view/item/context": [
Expand Down
16 changes: 12 additions & 4 deletions src/addHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export default async function addHost(state: Memento, defaultValue?: string) {
ignoreFocusOut: true,
prompt: 'Enter your gitlab host',
value: defaultValue || PROTOCOL,
validateInput(value) {
if (value && value.replace(PROTOCOL, '').trim()) {
return null;
}
return 'Must enter a valid host';
},
});
if (!host) {
return;
Expand All @@ -19,6 +25,11 @@ export default async function addHost(state: Memento, defaultValue?: string) {
const token = await window.showInputBox({
ignoreFocusOut: true,
prompt: 'Enter your gitlab token',
validateInput(value) {
if (!value || !value.trim()) {
return 'Personal token is required!';
}
},
});
if (!token) {
return;
Expand All @@ -28,13 +39,10 @@ export default async function addHost(state: Memento, defaultValue?: string) {
placeHolder: 'Choose host API version',
ignoreFocusOut: true,
});
if (!version) {
return;
}
const registry = {
host,
token,
version: parseInt(version, 10),
version: (version && parseInt(version, 10)) || 4,
};
const api = new SnippetRegistry(registry);
const snippets = await api.getSnippets();
Expand Down
1 change: 0 additions & 1 deletion src/commandName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type Command =
| 'star'
| 'unstar'
| 'viewSnippet'
| 'toggleHost'
| 'exploreMore';

export default function commandName(shorthand: Command) {
Expand Down
106 changes: 53 additions & 53 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,60 +30,60 @@ export function activate(context: ExtensionContext) {
);
views = [staredView, hostsView, myView];

subscriptions.concat(
[
[
[
'addHost',
async () => {
const { registry } = (await addHost(globalState)) || {};
if (registry) {
globalState.update(configKey('lastUseHost'), registry.host);
hostSnippetsProvider.openLastest();
mySnippetsProvider.openLastest();
}
},
],
[
'publish',
async () => {
const res = await publish(globalState);
mySnippetsProvider.reload(res?.registry?.host);
},
],
['reloadMySnippets', (host: Host) => mySnippetsProvider.reload(host)],
[
'reloadExploreSnippets',
(host: Host) => hostSnippetsProvider.reload(host),
],
[
'star',
(snippet: Snippet) => {
starSnippet(globalState, snippet);
staredProvider.reload();
},
],
[
'starById',
async () => {
await starById(globalState);
staredProvider.reload();
},
],
[
'unstar',
(snippet: StaredSnippet) => {
unstarSnippet(globalState, snippet);
staredProvider.reload();
},
],
[
'download',
(snippet: StaredSnippet | Snippet) =>
downloadSnippet(globalState, snippet),
],
['viewSnippet', viewSnippet],
['exploreMore', () => hostSnippetsProvider.loadMore()],
].map(([cmd, callback]) =>
'addHost',
async () => {
const { registry } = (await addHost(globalState)) || {};
if (registry) {
globalState.update(configKey('lastUseHost'), registry.host);
hostSnippetsProvider.openLastest();
mySnippetsProvider.openLastest();
}
},
],
[
'publish',
async () => {
const res = await publish(globalState);
mySnippetsProvider.reload(res?.registry?.host);
},
],
['reloadMySnippets', (host: Host) => mySnippetsProvider.reload(host)],
[
'reloadExploreSnippets',
(host: Host) => hostSnippetsProvider.reload(host),
],
[
'star',
(snippet: Snippet) => {
starSnippet(globalState, snippet);
staredProvider.reload();
},
],
[
'starById',
async () => {
await starById(globalState);
staredProvider.reload();
},
],
[
'unstar',
(snippet: StaredSnippet) => {
unstarSnippet(globalState, snippet);
staredProvider.reload();
},
],
[
'download',
(snippet: StaredSnippet | Snippet) =>
downloadSnippet(globalState, snippet),
],
['viewSnippet', viewSnippet],
['exploreMore', () => hostSnippetsProvider.loadMore()],
].forEach(([cmd, callback]) =>
subscriptions.push(
commands.registerCommand(
commandName(cmd as Command),
callback as () => void
Expand Down
9 changes: 7 additions & 2 deletions src/publishSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default async function publish(state: Memento) {
}
const fileName = await window.showInputBox({
ignoreFocusOut: true,
prompt: 'Enter file name',
prompt: 'Enter file name with extention. E.g., "example.js".',
value: activeTextEditor.document.fileName,
});
if (!fileName) {
Expand All @@ -42,14 +42,19 @@ export default async function publish(state: Memento) {
const title = await window.showInputBox({
ignoreFocusOut: true,
prompt: 'Enter a title',
validateInput(value) {
if (!value || !value.trim()) {
return 'Title is required!';
}
},
});
if (!title) {
return;
}
const description =
(await window.showInputBox({
ignoreFocusOut: true,
prompt: 'Enter a brief description',
prompt: '[Optional]Enter a brief description',
})) || null;
const visibility =
(await window.showQuickPick(Object.values(VISIBILITY), {
Expand Down
5 changes: 0 additions & 5 deletions src/views/getHostItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Host } from '../types';
import { TreeItem, TreeItemCollapsibleState, ThemeIcon } from 'vscode';
import commandName from '../commandName';

export default function getHostItem(
{ host }: Host,
Expand All @@ -14,9 +13,5 @@ export default function getHostItem(
? TreeItemCollapsibleState.Expanded
: TreeItemCollapsibleState.Collapsed,
iconPath: ThemeIcon.Folder,
command: {
command: commandName('toggleHost'),
title: 'Expand/collapse host list',
},
};
}

0 comments on commit d30f781

Please sign in to comment.