Skip to content

Commit

Permalink
added organization remember me
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-couchbase committed Nov 28, 2023
1 parent 7dcb805 commit 13c3b51
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 29 deletions.
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,18 @@
}
]
},
"configuration": {
"title": "Couchbase VSCode",
"properties": {
"iq.organization": {
"type": "string",
"markdownDescription": "Tne Current organization which is being used for all iq tasks. You can change default organization here by changing this field"
"configuration": [
{
"id": "couchbase-iq",
"title": "IQ",
"properties": {
"couchbase.iq.organization": {
"type": "string",
"default": "",
"markdownDescription": "The Current organization which is being used for all iq tasks. You can change default organization here by changing this field. Note: Any change will only appear after you login again"
}
}
}
}
]
}
}
87 changes: 79 additions & 8 deletions src/commands/iq/couchbaseIqWebviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,46 @@ export class CouchbaseIqWebviewProvider implements vscode.WebviewViewProvider {
switch (message.command) {
case "vscode-couchbase.iq.login": {
const organizations = await iqLoginHandler(message.value);
if (organizations) {
if (!organizations || organizations.length === 0) {
// TODO: send login error and do break
break;
}

let config = vscode.workspace.getConfiguration('couchbase');
const savedOrganization = config.get('iq.organization'); // Get saved organization from vscode couchbase settings
if (savedOrganization !== "") {
let savedOrganizationDetail = undefined;
for (let org of organizations) {
if (org.data.id === savedOrganization) {
savedOrganizationDetail = org;
break;
}
}

if (savedOrganizationDetail === undefined) {
// Remove organization from saved settings.
config.update("iq.organization", "");
this._view?.webview.postMessage({
command: "vscode-couchbase.iq.organizationDetails",
organizations: organizations,
isSavedOrganization: false,
savedOrganization: undefined
});
} else {
this._view?.webview.postMessage({
command: "vscode-couchbase.iq.organizationDetails",
organizations: organizations,
isSavedOrganization: true,
savedOrganization: savedOrganizationDetail
});
}
} else {
this._view?.webview.postMessage({
command: "vscode-couchbase.iq.organizationDetails",
organizations: organizations
organizations: organizations,
isSavedOrganization: false,
savedOrganization: undefined
});
} else {
// TODO: Add some login error
}
break;
}
Expand Down Expand Up @@ -92,16 +125,54 @@ export class CouchbaseIqWebviewProvider implements vscode.WebviewViewProvider {
}
case "vscode-couchbase.iq.singleClickSignIn": {
const organizations = await iqSavedLoginHandler(message.value.username);
if (organizations) {
if (!organizations || organizations.length === 0) {
// TODO: send login error and do break
break;
}

let config = vscode.workspace.getConfiguration('couchbase');
const savedOrganization = config.get('iq.organization'); // Get saved organization from vscode couchbase settings
if (savedOrganization !== "") {
let savedOrganizationDetail = undefined;
for (let org of organizations) {
if (org.data.id === savedOrganization) {
savedOrganizationDetail = org;
break;
}
}

if (savedOrganizationDetail === undefined) {
// Remove organization from saved settings.
config.update("iq.organization", "");
this._view?.webview.postMessage({
command: "vscode-couchbase.iq.organizationDetails",
organizations: organizations,
isSavedOrganization: false,
savedOrganization: undefined
});
} else {
this._view?.webview.postMessage({
command: "vscode-couchbase.iq.organizationDetails",
organizations: organizations,
isSavedOrganization: true,
savedOrganization: savedOrganizationDetail
});
}
} else {
this._view?.webview.postMessage({
command: "vscode-couchbase.iq.organizationDetails",
organizations: organizations
organizations: organizations,
isSavedOrganization: false,
savedOrganization: undefined
});
} else {
// TODO: Add some login error
}
break;
}
case "vscode-couchbase.iq.rememberOrganization": {
let config = vscode.workspace.getConfiguration('couchbase');
config.update('iq.organization', message.value.organizationDetails.data.id, vscode.ConfigurationTarget.Global);
break;
}
}
});
}
Expand Down
1 change: 0 additions & 1 deletion src/commands/iq/iqLoginhandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Constants } from "../../util/constants";
import { Global, Memory } from "../../util/util";
import { iqRestApiService } from "./iqRestApiService";
import * as vscode from 'vscode';
import * as keytar from 'keytar';

interface IFormData {
Expand Down
8 changes: 7 additions & 1 deletion src/reactViews/iq/bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Login } from "pages/login/Login";
import LoadingScreen from "pages/loader/Loader";
import SelectOrganizationPage from "pages/organizationSelect/SelectOrganization";
import LoginSingleClick from "pages/login/LoginSingleClick";
import IqChat from "pages/chatscreen/IqChat";

const container: HTMLElement = document.getElementById("vscodeRootIQ");
const newRoot = createRoot(container);
Expand All @@ -17,7 +18,12 @@ export const App: React.FC = () => {
const message = event.data;
switch (message.command) {
case "vscode-couchbase.iq.organizationDetails": {
setShowPage(<SelectOrganizationPage organizationDetails={message.organizations} setShowPage={setShowPage}/>);
if(message.isSavedOrganization){
// Organization is already saved, bypass the organization select page straight to IQ Chat
setShowPage(<IqChat org={message.savedOrganizationDetail}/>);
} else {
setShowPage(<SelectOrganizationPage organizationDetails={message.organizations} setShowPage={setShowPage}/>);
}
setIsLoading(false);
break;
}
Expand Down
60 changes: 48 additions & 12 deletions src/reactViews/iq/pages/organizationSelect/SelectOrganization.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
import Dropdown from "components/inputs/dropdown/Dropdown";
import IqChat from "pages/chatscreen/IqChat";
import { useState } from "react";

const SelectOrganizationPage = ({ organizationDetails, setShowPage }) => {
const [selectedOrg, setSelectedOrg] = useState(undefined);
const [rememberOrgCheckbox, setRememberOrgCheckbox] = useState(false);
const handleOrgClick = (org) => {
setShowPage(<IqChat org={org} />);
setSelectedOrg(org);
};

const handleRememberOrgCheckbox = (event) => {
setRememberOrgCheckbox(event.target.checked);
};

const handleSubmit = () => {
if (selectedOrg !== undefined) {
if (rememberOrgCheckbox) {
// Here if user has asked to remember the org, then we store it inside settings
tsvscode.postMessage({
command: "vscode-couchbase.iq.rememberOrganization",
value: {
organizationDetails: selectedOrg
}
});
}
// At the end, we shift to next page that is main iq chat
setShowPage(<IqChat org={selectedOrg} />);
}
};
console.log(organizationDetails);
return (
<Dropdown
trigger={<button>Dropdown</button>}
menu={organizationDetails.map((org) => {
return (
<button onClick={() => handleOrgClick(org)}>
{org.data.name}
</button>
);
})}
/>
<div>
<Dropdown
trigger={<button>Dropdown</button>}
menu={organizationDetails.map((org) => {
return (
<button onClick={() => handleOrgClick(org)}>
{org.data.name}
</button>
);
})}
/>
<div>
<label>Remember the organization</label>
<input
type="checkbox"
id="rememberOrg"
onClick={(event) => handleRememberOrgCheckbox(event)}
/>
</div>
<button
className="redButton"
onClick={() => handleSubmit()}
>Submit</button>
</div>
);
};

Expand Down

0 comments on commit 13c3b51

Please sign in to comment.