Skip to content
This repository was archived by the owner on Jan 14, 2023. It is now read-only.

Commit 1f3db3a

Browse files
authored
refactor: revamp login and settings. migrate to overmind (#48)
* refactor: revamp settings and migrate to overmind * feat: save token on login and reset session and settings on log out via overmind * feat: upsert session on login * refactor: get rid of redux user state, actions, reducer * feat: saving the session after login. re-initialise the api client * feat: wire up logout from overmind actions * refactor: revamp effects. Revamp login, logout * feat: redirect to the main window if already logged in * fix: fix redirect link to app view main page * feat: initializing overlay. Fix redirect route after login * refactor: update process indicator
1 parent a064672 commit 1f3db3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+797
-956
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"ecmaVersion": 11,
2222
"sourceType": "module"
2323
},
24-
"plugins": ["react"],
24+
"plugins": ["react", "@emotion"],
2525
"rules": {
2626
"class-methods-use-this": "off",
2727
"comma-dangle": "off",

common/config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

common/request.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const axios = require('axios');
22

3-
const storage = require('./storage');
4-
53
let instance;
64

75
const TWENTY_SECONDS = 20000;
@@ -43,13 +41,6 @@ const getInstance = () => instance;
4341

4442
const reset = () => { instance = undefined; };
4543

46-
if (storage.user.isDefined()) {
47-
const { api_key, redmineEndpoint } = storage.user.get();
48-
if (api_key && redmineEndpoint) {
49-
initialize(redmineEndpoint, api_key);
50-
}
51-
}
52-
5344
const handleReject = (error) => {
5445
// if this request was not cancelled
5546
if (!axios.isCancel(error)) {

common/storage.js

Lines changed: 0 additions & 26 deletions
This file was deleted.
File renamed without changes.

main/exceptionCatcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const isDev = require('electron-is-dev');
55
const { app, dialog, clipboard } = require('electron');
66
const logger = require('electron-log');
77

8-
const { report } = require('../common/reporter');
8+
const { report } = require('./handlers/errorHandler');
99

1010
const config = {
1111
showDialog: !isDev,
File renamed without changes.

main/index.js

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const electronUtils = require('electron-util');
1212
const isDev = require('electron-is-dev');
1313
const logger = require('electron-log');
1414

15-
const storage = require('../common/storage');
15+
const storage = require('./storage');
1616
const { redmineClient } = require('./redmine');
1717

1818
const Tray = require('./tray');
@@ -344,32 +344,53 @@ const initialize = () => {
344344
notification.show();
345345
});
346346

347-
ipcMain.on('menu', (event, { settings }) => {
347+
ipcMain.on('menu', (event, message) => {
348+
const { settings } = JSON.stringify(message);
348349
generateMenu({ settings });
349350
});
350351

351-
ipcMain.on('storage', (event, message) => {
352-
const { action, data } = JSON.parse(message);
353-
if (action === 'read') {
354-
event.reply('storage', storage.settings.get());
355-
} else if (action === 'save') {
356-
storage.settings.set(data);
357-
} else {
358-
throw new Error('Unable to process the requested action', action);
359-
}
360-
});
352+
storage.initializeSessionEvents(ipcMain);
361353

362354
ipcMain.on('request', async (event, message) => {
363-
const { payload, config, id } = JSON.parse(message);
364-
console.log('Received a request for query', { payload, config });
355+
const { payload, id } = JSON.parse(message);
356+
console.log('Received a request for query', { payload });
357+
365358
if (!redmineClient.isInitialized()) {
366-
redmineClient.initialize(config);
359+
const error = new Error('Unauthorized');
360+
error.status = 401;
361+
event.reply(`response:${id}`, { success: false, error });
362+
return;
367363
}
368364

369365
const response = await redmineClient.send(payload);
370366

371367
event.reply(`response:${id}`, response);
372368
});
369+
370+
ipcMain.on('system-request', async (event, message) => {
371+
const { action, payload, id } = JSON.parse(message);
372+
console.log(`Received system request for ${action}`, { payload });
373+
374+
switch (action.toLowerCase()) {
375+
case 'login': {
376+
const response = await redmineClient.initialize({
377+
endpoint: payload.endpoint,
378+
token: payload.token,
379+
headers: payload.headers
380+
});
381+
event.reply(`system-response:${id}`, response);
382+
break;
383+
}
384+
case 'logout': {
385+
redmineClient.reset();
386+
await storage.resetActiveSession();
387+
event.reply(`system-response:${id}`, { success: true });
388+
break;
389+
}
390+
default:
391+
break;
392+
}
393+
});
373394
};
374395

375396
app.on('certificate-error', (event, webContents, _url, error, certificate, callback) => {
@@ -391,15 +412,8 @@ app.once('ready', () => {
391412
}
392413

393414
// eslint-disable-next-line global-require
394-
const config = require('../common/config');
415+
const config = require('./env');
395416
PORT = config.PORT;
396-
// eslint-disable-next-line global-require
397-
require('../common/request'); // to initialize from storage
398-
399-
if (!storage.settings.isDefined()) {
400-
// sets defaul value that storage.settings.get returns as a fallback
401-
storage.settings.set(storage.settings.get());
402-
}
403417

404418
initialize();
405419
generateMenu();
@@ -410,6 +424,7 @@ app.once('ready', () => {
410424

411425
app.on('window-all-closed', () => {
412426
if (process.platform !== 'darwin') {
427+
storage.disposeSessionEvents(ipcMain);
413428
app.quit();
414429
}
415430
});

main/redmine.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,76 @@
11
const got = require('got');
22
const { transform } = require('./transformers/index');
33

4-
const createRequestClient = (initConfig = {}) => {
5-
let isInitialized = false;
4+
const createRequestClient = () => {
65
let instance;
7-
8-
const initialize = (config) => {
9-
isInitialized = Boolean(config.endpoint && config.token);
10-
instance = got.extend({
11-
prefixUrl: config.endpoint,
12-
headers: { 'X-Redmine-API-Key': config.token },
13-
responseType: 'json'
14-
});
15-
};
6+
let isInitialized;
167

178
const handleUnsuccessfulRequest = (error) => ({
189
success: false,
1910
error
2011
});
2112

13+
const initialize = async (data) => {
14+
if (isInitialized) {
15+
reset();
16+
}
17+
18+
const route = 'users/current.json';
19+
20+
const configuration = data.token
21+
? {
22+
prefixUrl: data.endpoint,
23+
headers: { 'X-Redmine-API-Key': data.token },
24+
responseType: 'json'
25+
}
26+
: {
27+
prefixUrl: data.endpoint,
28+
headers: {
29+
Authorization: data.headers.Authorization
30+
},
31+
responseType: 'json'
32+
};
33+
34+
try {
35+
const response = await got(route, {
36+
method: 'GET',
37+
...configuration
38+
});
39+
40+
console.log(response.statusCode, response.body);
41+
42+
const loginSuccess = response.statusCode === 200;
43+
isInitialized = loginSuccess;
44+
45+
if (loginSuccess) {
46+
const payload = transform(route, response.body);
47+
48+
instance = got.extend({
49+
...configuration,
50+
headers: {
51+
'X-Redmine-API-Key': payload.token
52+
}
53+
});
54+
55+
return {
56+
success: true,
57+
payload
58+
};
59+
}
60+
61+
return handleUnsuccessfulRequest(response.body);
62+
} catch (error) {
63+
return handleUnsuccessfulRequest(error);
64+
}
65+
};
66+
67+
const reset = () => {
68+
isInitialized = false;
69+
instance = undefined;
70+
};
71+
2272
const send = async (data) => {
23-
if (!instance && !data.headers?.Authorization) {
73+
if (!isInitialized) {
2474
throw new Error('Http client is not initialized.');
2575
}
2676

@@ -34,12 +84,8 @@ const createRequestClient = (initConfig = {}) => {
3484
console.log(response.statusCode, response.body);
3585

3686
if (response.statusCode === 200) {
37-
if (!isInitialized && data.route === 'users/current.json' && response.body.user?.api_key) {
38-
initialize({ endpoint: initConfig.endpoint, token: response.body.user?.api_key });
39-
}
40-
4187
return {
42-
data: transform(data.route, response.body),
88+
payload: transform(data.route, response.body),
4389
success: true,
4490
};
4591
}
@@ -50,12 +96,11 @@ const createRequestClient = (initConfig = {}) => {
5096
}
5197
};
5298

53-
initialize(initConfig);
54-
5599
return {
56100
initialize,
57101
isInitialized: () => isInitialized,
58-
send
102+
send,
103+
reset
59104
};
60105
};
61106

0 commit comments

Comments
 (0)