Skip to content

Commit f11efa8

Browse files
vrubezhnydatho7561
authored andcommitted
Add "OK" and "Cancel" buttons to cluster login quick picks input fields
Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
1 parent 98a90ea commit f11efa8

File tree

1 file changed

+67
-54
lines changed

1 file changed

+67
-54
lines changed

src/openshift/cluster.ts

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ export class Cluster extends OpenShiftItem {
150150
buttons: [deleteBtn],
151151
}));
152152
quickPick.items = contextNames;
153-
quickPick.buttons = [QuickInputButtons.Back];
153+
const cancelBtn = new quickBtn(new ThemeIcon('close'), 'Cancel');
154+
quickPick.buttons = [QuickInputButtons.Back, cancelBtn];
154155
if (contextNames.length === 0) {
155156
void window
156157
.showInformationMessage(
@@ -179,9 +180,7 @@ export class Cluster extends OpenShiftItem {
179180
.catch(reject);
180181
});
181182
quickPick.onDidTriggerButton((button) => {
182-
if (button === QuickInputButtons.Back) {
183-
quickPick.hide();
184-
}
183+
if (button === QuickInputButtons.Back || button === cancelBtn) quickPick.hide();
185184
});
186185
quickPick.onDidTriggerItemButton(async (event: QuickPickItemButtonEvent<QuickPickItem>) => {
187186
if (event.button === deleteBtn) {
@@ -204,7 +203,7 @@ export class Cluster extends OpenShiftItem {
204203
});
205204
}
206205

207-
static async getUrl(): Promise<string | null> {
206+
static async getUrl(): Promise<string | null | undefined> {
208207
const clusterURl = await Cluster.getUrlFromClipboard();
209208
return await Cluster.showQuickPick(clusterURl);
210209
}
@@ -221,7 +220,8 @@ export class Cluster extends OpenShiftItem {
221220
buttons: ctx.description ? [] : [deleteBtn],
222221
}));
223222
quickPick.items = [createUrl, ...contextNames];
224-
quickPick.buttons = [QuickInputButtons.Back];
223+
const cancelBtn = new quickBtn(new ThemeIcon('close'), 'Cancel');
224+
quickPick.buttons = [QuickInputButtons.Back, cancelBtn];
225225
let selection: readonly QuickPickItem[] | undefined;
226226
const hideDisposable = quickPick.onDidHide(() => resolve(null));
227227
quickPick.onDidAccept(async () => {
@@ -232,13 +232,9 @@ export class Cluster extends OpenShiftItem {
232232
const prompt = 'Provide new Cluster URL to connect';
233233
const validateInput = (value: string) => NameValidator.validateUrl('Invalid URL provided', value);
234234
const newURL = await this.enterValue(prompt, '', false, validateInput);
235-
if (!newURL) {
236-
resolve(await Cluster.showQuickPick(clusterURl)); // Back
237-
} else if (newURL === null) {
238-
return null; // Cancel
239-
} else {
240-
resolve(newURL);
241-
}
235+
if (newURL === null) return null; // Cancel
236+
else if (!newURL) resolve(await Cluster.showQuickPick(clusterURl)); // Back
237+
else resolve(newURL);
242238
} else {
243239
resolve(choice.label);
244240
}
@@ -247,9 +243,10 @@ export class Cluster extends OpenShiftItem {
247243
selection = selects;
248244
});
249245
quickPick.onDidTriggerButton((button) => {
250-
if (button === QuickInputButtons.Back) {
251-
quickPick.hide();
252-
}
246+
hideDisposable.dispose();
247+
quickPick.hide();
248+
if (button === QuickInputButtons.Back) quickPick.hide();
249+
else if (button === cancelBtn) resolve(null);
253250
});
254251
quickPick.onDidTriggerItemButton(async (event) => {
255252
if (event.button === deleteBtn) {
@@ -325,7 +322,7 @@ export class Cluster extends OpenShiftItem {
325322
* @returns string contaning cluster login method name or null if cancelled or undefined if Back is pressed
326323
*/
327324
private static async getLoginMethod(): Promise<string | null | undefined> {
328-
return new Promise<string | null>((resolve, reject) => {
325+
return new Promise<string | null | undefined>((resolve, reject) => {
329326
const loginActions: QuickPickItem[] = [
330327
{
331328
label: 'Credentials',
@@ -338,7 +335,8 @@ export class Cluster extends OpenShiftItem {
338335
];
339336
const quickPick = window.createQuickPick();
340337
quickPick.items = [...loginActions];
341-
quickPick.buttons = [QuickInputButtons.Back];
338+
const cancelBtn = new quickBtn(new ThemeIcon('close'), 'Cancel');
339+
quickPick.buttons = [QuickInputButtons.Back, cancelBtn];
342340
let selection: readonly QuickPickItem[] | undefined;
343341
const hideDisposable = quickPick.onDidHide(() => resolve(null));
344342
quickPick.onDidAccept(() => {
@@ -351,9 +349,10 @@ export class Cluster extends OpenShiftItem {
351349
selection = selects;
352350
});
353351
quickPick.onDidTriggerButton((button) => {
354-
if (button === QuickInputButtons.Back) {
355-
resolve(undefined);
356-
}
352+
hideDisposable.dispose();
353+
quickPick.hide();
354+
if (button === QuickInputButtons.Back) resolve(undefined);
355+
else if (button === cancelBtn) resolve(null);
357356
});
358357
quickPick.show();
359358
});
@@ -395,8 +394,13 @@ export class Cluster extends OpenShiftItem {
395394
// so it's running
396395
clusterIsUp = true;
397396
} catch (e) {
398-
const clusterURLObj = new URL(clusterURL);
399-
if (clusterURLObj.hostname === 'api.crc.testing') {
397+
let clusterURLObj: any = undefined;
398+
try {
399+
clusterURLObj = new URL(clusterURL);
400+
} catch (_) {
401+
// Ignore
402+
}
403+
if (clusterURLObj && clusterURLObj.hostname === 'api.crc.testing') {
400404
const startCrc = 'Start OpenShift Local';
401405
const promptResponse = await window.showWarningMessage(
402406
'The cluster appears to be a OpenShift Local cluster, but it isn\'t running',
@@ -409,7 +413,7 @@ export class Cluster extends OpenShiftItem {
409413
// it will take the cluster a few minutes to stabilize
410414
return null;
411415
}
412-
} else if (/api\.sandbox-.*openshiftapps\.com/.test(clusterURLObj.hostname)) {
416+
} else if (clusterURLObj && /api\.sandbox-.*openshiftapps\.com/.test(clusterURLObj.hostname)) {
413417
const devSandboxSignup = 'Sign up for OpenShift Dev Sandbox';
414418
const promptResponse = await window.showWarningMessage(
415419
'The cluster appears to be a OpenShift Dev Sandbox cluster, but it isn\'t running',
@@ -435,10 +439,10 @@ export class Cluster extends OpenShiftItem {
435439
}
436440
case Step.selectLoginMethod: {
437441
const result = await Cluster.getLoginMethod();
438-
if (!result) { // Back Button is hit
439-
step = Step.selectCluster;
440-
} if (result === null) { // User cancelled the operation
442+
if (result === null) { // User cancelled the operation
441443
return null;
444+
} else if (!result) { // Back button is hit
445+
step = Step.selectCluster;
442446
} else if(result === 'Credentials') {
443447
step = Step.loginUsingCredentials;
444448
} else if (result === 'Token') {
@@ -451,10 +455,11 @@ export class Cluster extends OpenShiftItem {
451455
const clusterVersions: string = step === Step.loginUsingCredentials
452456
? await Cluster.credentialsLogin(true, clusterURL)
453457
: await Cluster.tokenLogin(clusterURL, true);
454-
if (!clusterVersions) { // Back Button is hit
455-
step = Step.selectLoginMethod;
456-
} else if (clusterVersions === null) { // User cancelled the operation
458+
459+
if (clusterVersions === null) { // User cancelled the operation
457460
return null;
461+
} else if (!clusterVersions) { // Back button is hit
462+
step = Step.selectLoginMethod;
458463
} else {
459464
// login successful
460465
return null;
@@ -511,7 +516,8 @@ export class Cluster extends OpenShiftItem {
511516

512517
const quickPick = window.createQuickPick();
513518
quickPick.items = [addUser, ...users];
514-
quickPick.buttons = [QuickInputButtons.Back];
519+
const cancelBtn = new quickBtn(new ThemeIcon('close'), 'Cancel');
520+
quickPick.buttons = [QuickInputButtons.Back, cancelBtn];
515521
let selection: readonly QuickPickItem[] | undefined;
516522
const hideDisposable = quickPick.onDidHide(() => resolve(null));
517523
quickPick.onDidAccept(() => {
@@ -524,9 +530,10 @@ export class Cluster extends OpenShiftItem {
524530
selection = selects;
525531
});
526532
quickPick.onDidTriggerButton((button) => {
527-
if (button === QuickInputButtons.Back) {
528-
resolve(undefined);
529-
}
533+
hideDisposable.dispose();
534+
quickPick.hide();
535+
if (button === QuickInputButtons.Back) resolve(undefined);
536+
else if (button === cancelBtn) resolve(null);
530537
});
531538
quickPick.show();
532539
});
@@ -545,14 +552,15 @@ export class Cluster extends OpenShiftItem {
545552
input.value = initialValue;
546553
input.prompt = prompt;
547554
input.password = password;
548-
input.buttons = [QuickInputButtons.Back];
549-
const validationMessage: string = validate(input.value);
555+
const enterBtn = new quickBtn(new ThemeIcon('check'), 'Enter');
556+
const cancelBtn = new quickBtn(new ThemeIcon('close'), 'Cancel');
557+
input.buttons = [QuickInputButtons.Back, enterBtn, cancelBtn];
558+
const validationMessage: string = validate(input.value? input.value : '');
550559
input.ignoreFocusOut = true;
551560
if (validationMessage) {
552561
input.validationMessage = validationMessage;
553562
}
554-
555-
input.onDidAccept(async () => {
563+
const acceptInput = async () => {
556564
const value = input.value;
557565
input.enabled = false;
558566
input.busy = true;
@@ -562,21 +570,25 @@ export class Cluster extends OpenShiftItem {
562570
}
563571
input.enabled = true;
564572
input.busy = false;
565-
});
573+
};
574+
input.onDidAccept(acceptInput);
566575
input.onDidChangeValue(async text => {
567576
const current = validate(text);
568577
const validating = current;
569578
const validationMessage = await current;
570579
if (current === validating) {
571-
input.validationMessage = validationMessage;
580+
input.validationMessage = validationMessage;
572581
}
573582
});
574583
input.onDidHide(() => {
575584
input.dispose();
576585
})
577-
input.onDidTriggerButton((event) => {
578-
if (event === QuickInputButtons.Back) {
579-
resolve(undefined);
586+
input.onDidTriggerButton(async (event) => {
587+
if (event === QuickInputButtons.Back) resolve(undefined);
588+
else if (event === enterBtn) await acceptInput();
589+
else if (event === cancelBtn) {
590+
resolve(null);
591+
input.dispose();
580592
}
581593
});
582594
input.show();
@@ -614,8 +626,7 @@ export class Cluster extends OpenShiftItem {
614626
if (!username) {
615627
const addUserLabel = '$(plus) Add new user...';
616628
const choice = await this.getUserName(clusterURL, addUserLabel);
617-
if (!choice || choice === null) return choice; // Back or Cancel
618-
629+
if (!choice) return choice; // Back or Cancel
619630
if (choice === addUserLabel) {
620631
step = Step.enterUserName;
621632
} else {
@@ -629,11 +640,12 @@ export class Cluster extends OpenShiftItem {
629640
const prompt = 'Provide Username for basic authentication to the API server';
630641
const validateInput = (value: string) => NameValidator.emptyName('User name cannot be empty', value);
631642
const newUsername = await this.enterValue(prompt, '', false, validateInput);
632-
if (!newUsername) {
643+
644+
if (newUsername === null) {
645+
return null; // Cancel
646+
} else if (!newUsername) {
633647
username = undefined;
634648
step = Step.getUserName; // Back
635-
} else if (newUsername === null) {
636-
return null; // Cancel
637649
} else {
638650
username = newUsername;
639651
step = Step.enterPassword;
@@ -646,11 +658,12 @@ export class Cluster extends OpenShiftItem {
646658
const prompt = 'Provide Password for basic authentication to the API server';
647659
const validateInput = (value: string) => NameValidator.emptyName('Password cannot be empty', value);
648660
const newPassword = await this.enterValue(prompt, password, true, validateInput);
649-
if (!newPassword) {
661+
662+
if (newPassword === null) {
663+
return null; // Cancel
664+
} else if (!newPassword) {
650665
username = undefined;
651666
step = Step.getUserName; // Back
652-
} else if (newPassword === null) {
653-
return null; // Cancel
654667
} else {
655668
passwd = newPassword;
656669
step = undefined;
@@ -743,10 +756,10 @@ export class Cluster extends OpenShiftItem {
743756
const validateInput = (value: string) => NameValidator.emptyName('Bearer token cannot be empty', value);
744757
const initialValue = token ? token : '';
745758
ocToken = await this.enterValue(prompt, initialValue, true, validateInput);
746-
if (!ocToken) {
747-
return undefined; // Back
748-
} else if (ocToken === null) {
759+
if (ocToken === null) {
749760
return null; // Cancel
761+
} else if (!ocToken) {
762+
return undefined; // Back
750763
}
751764
} else {
752765
ocToken = userToken;

0 commit comments

Comments
 (0)