Skip to content

Commit 1293699

Browse files
committed
Refactor, add utils.ts
1 parent 0443c1e commit 1293699

4 files changed

Lines changed: 39 additions & 37 deletions

File tree

src/commands/note.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as apiClient from '@hackmd/api';
3-
import {checkLogin} from './../tree/index';
3+
import { checkLogin } from './../utils';
44
const API = new apiClient.default();
55

66
export async function registerNoteCommands(context: vscode.ExtensionContext) {

src/commands/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode'
2-
import { checkLogin, login, refreshHistoryList } from './../tree/index'
2+
import { checkLogin, login, refreshHistoryList } from './../utils'
33
import { Store } from '../store';
44
import * as apiClient from '@hackmd/api';
55
const API = new apiClient.default();

src/tree/index.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import * as vscode from 'vscode';
22
import { Store, store } from '../store';
33
import { reaction } from 'mobx';
44
import { TreeNode, NoteTreeNode } from './nodes';
5-
import * as apiClient from '@hackmd/api';
6-
const API = new apiClient.default();
75

86
export class HackMDTreeViewProvider implements vscode.TreeDataProvider<TreeNode> {
97
private _onDidChangeTreeData = new vscode.EventEmitter<TreeNode>();
@@ -39,36 +37,3 @@ export class HackMDTreeViewProvider implements vscode.TreeDataProvider<TreeNode>
3937
this._onDidChangeTreeData.fire();
4038
}
4139
}
42-
43-
export const refreshHistoryList = async (context) => {
44-
if (await checkLogin()) {
45-
store.history = (await API.getHistory()).history;
46-
} else {
47-
store.history = [{}];
48-
}
49-
};
50-
51-
export const checkLogin = async () => {
52-
return (await API.getMe()).status === 'ok';
53-
};
54-
55-
export const login = async (context: vscode.ExtensionContext) => {
56-
const { email, password } = getLoginCredential(context);
57-
if (!email || !password) {
58-
vscode.window.showInformationMessage('Please enter your email and password to use HackMD extension!')
59-
return;
60-
}
61-
await API.login(email, password);
62-
if (await checkLogin()) {
63-
store.isLogin = true;
64-
vscode.window.showInformationMessage('Successfully login!');
65-
} else {
66-
vscode.window.showInformationMessage('Wrong email or password, please enter again');
67-
}
68-
};
69-
70-
export const getLoginCredential = (context: vscode.ExtensionContext) => {
71-
const email: string = context.globalState.get('email');
72-
const password: string = context.globalState.get('password');
73-
return { email, password };
74-
};

src/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as vscode from 'vscode';
2+
import { store } from './store';
3+
import * as apiClient from '@hackmd/api';
4+
5+
const API = new apiClient.default();
6+
export const refreshHistoryList = async (context) => {
7+
if (await checkLogin()) {
8+
store.history = (await API.getHistory()).history;
9+
} else {
10+
store.history = [{}];
11+
}
12+
};
13+
14+
export const checkLogin = async () => {
15+
return (await API.getMe()).status === 'ok';
16+
};
17+
18+
export const login = async (context: vscode.ExtensionContext) => {
19+
const { email, password } = getLoginCredential(context);
20+
if (!email || !password) {
21+
vscode.window.showInformationMessage('Please enter your email and password to use HackMD extension!')
22+
return;
23+
}
24+
await API.login(email, password);
25+
if (await checkLogin()) {
26+
store.isLogin = true;
27+
vscode.window.showInformationMessage('Successfully login!');
28+
} else {
29+
vscode.window.showInformationMessage('Wrong email or password, please enter again');
30+
}
31+
};
32+
33+
export const getLoginCredential = (context: vscode.ExtensionContext) => {
34+
const email: string = context.globalState.get('email');
35+
const password: string = context.globalState.get('password');
36+
return { email, password };
37+
};

0 commit comments

Comments
 (0)