diff --git a/package.json b/package.json index 70aec81..1716df9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,9 @@ "name": "harvest-vscode", "displayName": "harvest-vscode", "description": "Varvest Integration to VS Code", - "version": "0.0.1", + "version": "0.1.0", + "publisher": "Tzedakah", + "repository": "https://github.com/joshuashoemaker/harvest-vscode", "engines": { "vscode": "^1.49.0" }, @@ -10,14 +12,22 @@ "Other" ], "activationEvents": [ - "onCommand:harvest-vscode.helloWorld" + "*" ], "main": "./out/extension.js", "contributes": { "commands": [ { - "command": "harvest-vscode.helloWorld", - "title": "Hello World" + "command": "harvest-vscode.punchTime", + "title": "Harvest: Punch Time" + }, + { + "command": "harvest-vscode.login", + "title": "Harvest: Login" + }, + { + "command": "harvest-vscode.logout", + "title": "Harvest: Logout" } ] }, diff --git a/src/UseCases/Commands/Logout.ts b/src/UseCases/Commands/Logout.ts new file mode 100644 index 0000000..352f23c --- /dev/null +++ b/src/UseCases/Commands/Logout.ts @@ -0,0 +1,15 @@ +import * as vscode from "vscode"; +import Harvest from "../../Entities/Harvest" +import User from "../../Entities/User" + +function Logout (context: vscode.ExtensionContext): vscode.Disposable { + return vscode.commands.registerCommand('harvest-vscode.logout', async () => { + new Harvest().destructor() + new User().destructor() + + await context.globalState.update('accountId', '') + await context.globalState.update('accessToken', '') + }) +} + +export default Logout \ No newline at end of file diff --git a/src/UseCases/Commands/PunchTime.ts b/src/UseCases/Commands/PunchTime.ts new file mode 100644 index 0000000..a02564f --- /dev/null +++ b/src/UseCases/Commands/PunchTime.ts @@ -0,0 +1,45 @@ +import * as vscode from "vscode"; +import Harvest from "../../Entities/Harvest" +import UserInterface from "../../Entities/Interfaces/UserInterface"; +import User from "../../Entities/User" +import getUser from "../getUser"; + +function PunchTime (context: vscode.ExtensionContext): vscode.Disposable { + return vscode.commands.registerCommand('harvest-vscode.punchTime', async () => { + const harvest = new Harvest() + let user = new User() + + const accountId: string = context.globalState.get('accountId') || '' + const accessToken: string = context.globalState.get('accessToken') || '' + + if (!accountId || !accessToken) { + vscode.window.showErrorMessage('Run "Harvest: Login" Command before trying to puch time') + return + } + + harvest.accountId = accountId + harvest.accessToken = accessToken + + if (!user.id) { + let userProps: UserInterface + try { + userProps = await getUser() + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Could not retrieve user data from Harvest') + return + } + if (!userProps.id) { + vscode.window.showErrorMessage('Could not retrieve user data from Harvest') + return + } + user.destructor() + user = new User(userProps) + vscode.window.showInformationMessage('Successfully authenticated with Harvest') + } + + console.log(user) + }) +} + +export default PunchTime \ No newline at end of file diff --git a/src/UseCases/Commands/SetUserAuthentication.ts b/src/UseCases/Commands/SetUserAuthentication.ts new file mode 100644 index 0000000..d878e4f --- /dev/null +++ b/src/UseCases/Commands/SetUserAuthentication.ts @@ -0,0 +1,42 @@ +import * as vscode from "vscode"; +import Harvest from "../../Entities/Harvest" +import User from "../../Entities/User" + +function SetUserAuthentication (context: vscode.ExtensionContext): vscode.Disposable { + return vscode.commands.registerCommand('harvest-vscode.login', async () => { + new Harvest().destructor() + new User().destructor() + + const accountId = await vscode.window.showInputBox({ + ignoreFocusOut: true, + placeHolder: 'Harvest Acount Id' + }) + + if (!accountId) { + vscode.window.showErrorMessage('No Account Id Proveded') + return + } + + const accessToken = await vscode.window.showInputBox({ + ignoreFocusOut: true, + placeHolder: 'Harvest Access Token', + password: true + }) + + if (!accessToken) { + vscode.window.showErrorMessage('No Access Token Proveded') + return + } + + await context.globalState.update('accountId', accountId) + await context.globalState.update('accessToken', accessToken) + + new Harvest({ + accountId: accountId, + accessToken: accessToken + }) + + }) +} + +export default SetUserAuthentication \ No newline at end of file diff --git a/src/UseCases/getUser.ts b/src/UseCases/getUser.ts index 8210260..1497a49 100644 --- a/src/UseCases/getUser.ts +++ b/src/UseCases/getUser.ts @@ -1,10 +1,8 @@ import axios from 'axios' import Harvest from '../Entities/Harvest' -import ErrorMessage from '../Constants/ErrorMessageInterface' -import ErrorMessages from '../Constants/ErrorMessages' import UserInterface from '../Entities/Interfaces/UserInterface' -const getUser = async (): Promise => { +const getUser = async (): Promise => { const harvest = new Harvest() let userResponse: any try { @@ -14,15 +12,14 @@ const getUser = async (): Promise => { ) } catch (err) { console.log(err) - return ErrorMessages[0] } const user = { - id: userResponse.data.id || '', - firstName: userResponse.data.first_name || '', - lastName: userResponse.data.last_name || '', - email: userResponse.data.email || '', - avatar: userResponse.data.avatar_url || '', + id: userResponse?.data?.id || '', + firstName: userResponse?.data?.first_name || '', + lastName: userResponse?.data?.last_name || '', + email: userResponse?.data?.email || '', + avatar: userResponse?.data?.avatar_url || '', } return user diff --git a/src/extension.ts b/src/extension.ts index 7d62826..9ce613e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,27 +1,22 @@ -// The module 'vscode' contains the VS Code extensibility API -// Import the module and reference it with the alias vscode in your code below -import * as vscode from 'vscode'; +import * as vscode from 'vscode' +import Logout from './UseCases/Commands/Logout' +import PunchTime from './UseCases/Commands/PunchTime' +import SetUserAuthentication from './UseCases/Commands/SetUserAuthentication' -// this method is called when your extension is activated -// your extension is activated the very first time the command is executed export function activate(context: vscode.ExtensionContext) { + const statusbar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100) + statusbar.command = 'harvest-vscode.punchTime' + + statusbar.text = 'Harvest' + statusbar.show() - // Use the console to output diagnostic information (console.log) and errors (console.error) - // This line of code will only be executed once when your extension is activated - console.log('Congratulations, your extension "harvest-vscode" is now active!'); + const commands: vscode.Disposable[] = [ + SetUserAuthentication(context), + Logout(context), + PunchTime(context) + ] - // The command has been defined in the package.json file - // Now provide the implementation of the command with registerCommand - // The commandId parameter must match the command field in package.json - let disposable = vscode.commands.registerCommand('harvest-vscode.helloWorld', () => { - // The code you place here will be executed every time your command is executed - - // Display a message box to the user - vscode.window.showInformationMessage('Hello World from harvest-vscode!'); - }); - - context.subscriptions.push(disposable); + context.subscriptions.push(...commands) } -// this method is called when your extension is deactivated export function deactivate() {} diff --git a/src/media/harvestLogo.png b/src/media/harvestLogo.png new file mode 100644 index 0000000..2bb19ee Binary files /dev/null and b/src/media/harvestLogo.png differ