Skip to content

Commit 784befc

Browse files
committed
Implement config saving, style login window
1 parent 703a531 commit 784befc

File tree

6 files changed

+691
-173
lines changed

6 files changed

+691
-173
lines changed

index.css

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
1-
body {
2-
margin: 0 auto;
3-
box-sizing: border-box;
4-
font-family: 'Helvetica Neue', Helvetica, sans-serif;
5-
font-size: 14pt;
6-
}
7-
8-
#personal-auth-form {
9-
width: 400px;
10-
margin: 0 auto;
11-
text-align: center;
1+
html, body {
2+
-webkit-app-region: drag;
123
}
134

14-
input {
15-
border: 2px solid rgb(85, 20, 145);
16-
border-radius: 4px;
17-
border-collapse: collapse;
18-
margin: 0;
19-
padding: 0;
20-
height: 30px;
21-
}
22-
23-
input[type="text"] {
24-
width: 100%;
25-
background-color: #fff;
5+
body {
6+
background-color: rgb(62, 48, 111);
7+
padding-top: 40px;
268
}
279

28-
input[type="submit"] {
29-
width: 20%;
30-
background-color: rgb(85, 20, 145);
31-
color: #fff;
10+
.container {
11+
-webkit-app-region: no-drag;
3212
}

index.html

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
56
<title>Starling Bank Personal Auth</title>
6-
<link rel="stylesheet" href="./index.css" media="screen" charset="utf-8">
7+
<link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
8+
<link rel="stylesheet" href="./index.css">
79
</head>
810
<body>
9-
<form id="personal-auth-form">
10-
<input type="text" id="access-token-input" placeholder="Personal Access Token...">
11-
<input type="submit">
12-
</form>
11+
<div class="app-region"></div>
12+
<div class="container">
13+
<div id="error"></div>
14+
<div class="panel panel-default">
15+
<div class="panel-heading">
16+
<h1 class="panel-title">Personal Auth</h1>
17+
</div>
18+
<div class="panel-body">
19+
<p>You can get your personal auth token from the <a href="https://developer.starlingbank.com">Starling Bank developer portal</a>.</p>
20+
<p>You'll be asked to create an account if you don't already have one, and tie it to your Starling account.</p>
21+
<form id="personal-auth-form">
22+
<div class="form-group">
23+
<input type="text" id="access-token-input" placeholder="Token" class="form-control">
24+
</div>
25+
<input type="submit" class="form-control">
26+
</form>
27+
</div>
28+
</div>
29+
</div>
1330
<script>require('./index.js')</script>
1431
</body>
1532
</html>

index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
const $ = window.jQuery = window.$ = require('jquery')
2+
require('bootstrap')
3+
14
const {ipcRenderer, shell} = require('electron')
25

36
const personalAuthForm = document.querySelector('#personal-auth-form')
47
const accessTokenInput = document.querySelector('#access-token-input')
58

6-
personalAuthForm.addEventListener('submit', (e) => {
7-
e.preventDefault()
8-
ipcRenderer.send('personal-auth', accessTokenInput.value)
9+
$('#personal-auth-form').on('submit', (event) => {
10+
event.preventDefault()
11+
ipcRenderer.send('personal-auth-token', $('#access-token-input').val() || ' ')
12+
})
13+
14+
$(document).on('click', 'a[href^="http"]', (event) => {
15+
event.preventDefault()
16+
shell.openExternal(event.target.href)
17+
})
18+
19+
ipcRenderer.on('personal-auth-error', (event, error) => {
20+
$('#error').html(`
21+
<div class="alert alert-warning alert-dismissible" role="alert">
22+
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
23+
<strong>Error</strong>: ${error}
24+
</div>`)
925
})

main.js

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,57 @@
11
const {app, BrowserWindow, ipcMain, Tray, Menu} = require('electron')
22
const path = require('path')
33
const Starling = require('starling-developer-sdk')
4+
const storage = require('electron-json-storage')
45

56
const assetsDirectory = path.join(__dirname, 'assets')
67

78
let starling = undefined
89
let tray = undefined
910
let trayMenu = undefined
1011
let authWindow = undefined
11-
let displayInStatusbar = false
1212

13-
// Don't show the app in the doc
13+
let config = {
14+
starling: {}
15+
}
16+
17+
// don't show the app in the dock
1418
app.dock.hide()
1519

1620
app.on('ready', () => {
21+
createSystemMenu()
1722
createTray()
23+
loadConfig()
1824
})
1925

20-
// Quit the app when the window is closed
21-
app.on('window-all-closed', () => {
22-
app.quit()
23-
})
26+
const loadConfig = () => {
27+
storage.get('starling', function(error, data) {
28+
config.starling = {
29+
accessToken: data.accessToken,
30+
displayInStatusbar: data.displayInStatusbar || true
31+
}
32+
33+
if (config.starling.accessToken) {
34+
starling = new Starling({accessToken: config.starling.accessToken})
35+
starling.getBalance().catch(() => {
36+
starling = null
37+
loadPreAuthTray()
38+
}).then(() => {
39+
loadTray()
40+
})
41+
} else {
42+
loadPreAuthTray()
43+
}
44+
})
45+
}
2446

2547
const createTray = () => {
2648
tray = new Tray(path.join(assetsDirectory, 'sbTemplate.png'))
49+
}
50+
51+
const loadPreAuthTray = () => {
52+
tray.setTitle('')
2753
trayMenu = Menu.buildFromTemplate([
28-
{label: 'Enter Personal Auth Token...', click: doPersonalAuth},
54+
{label: 'Enter personal auth token...', click: doPersonalAuth},
2955
{type: 'separator'},
3056
{label: 'Quit', role: 'quit'}
3157
])
@@ -39,7 +65,6 @@ const loadTray = () => {
3965

4066
updateTray()
4167
setInterval(updateTray, 30000)
42-
4368
}
4469

4570
const updateTray = () => {
@@ -48,44 +73,93 @@ const updateTray = () => {
4873
}
4974

5075
starling.getBalance().then(({data}) => {
51-
if (displayInStatusbar) {
76+
if (config.starling.displayInStatusbar) {
5277
tray.setTitle(${data.effectiveBalance}`)
5378
} else {
5479
tray.setTitle('')
5580
}
5681

5782
trayMenu = Menu.buildFromTemplate([
58-
{type: 'checkbox', label: 'Display in statusbar', click: setDisplayInStatusbar, checked: displayInStatusbar},
83+
{type: 'checkbox', label: 'Display in statusbar', click: setDisplayInStatusbar, checked: config.starling.displayInStatusbar},
5984
{type: 'separator'},
6085
{label: `Balance`, enabled: false},
6186
{label: ` - £${data.effectiveBalance}`, enabled: false},
6287
{type: 'separator'},
88+
{label: 'Log out', click: logOut},
6389
{label: 'Quit', role: 'quit'}
6490
])
6591
tray.setContextMenu(trayMenu)
66-
});
92+
})
6793
}
6894

6995
const setDisplayInStatusbar = (item) => {
70-
displayInStatusbar = item.checked
96+
config.starling.displayInStatusbar = item.checked
7197
updateTray()
98+
storage.set('starling', config.starling, (error) => {
99+
//
100+
})
72101
}
73102

74103
const createAuthWindow = () => {
75-
if (authWindow) {
104+
if (authWindow && !authWindow.isDestroyed()) {
76105
authWindow.close()
77106
}
78107

79-
return new BrowserWindow({})
108+
return new BrowserWindow({
109+
width: 320,
110+
height: 420,
111+
titleBarStyle: 'hidden-inset',
112+
resizable: false,
113+
alwaysOnTop: true
114+
})
80115
}
81116

82117
const doPersonalAuth = () => {
83118
authWindow = createAuthWindow()
84119
authWindow.loadURL(`file://${path.join(__dirname, 'index.html')}`)
85120
}
86121

87-
ipcMain.on('personal-auth', (event, token) => {
122+
ipcMain.on('personal-auth-token', (event, token) => {
88123
starling = new Starling({accessToken: token})
89-
authWindow.hide()
90-
loadTray()
124+
starling.getBalance().then(({data}) => {
125+
config.starling.accessToken = token
126+
storage.set('starling', config.starling, function(error) {
127+
authWindow.hide()
128+
loadTray()
129+
})
130+
}).catch((error) => {
131+
event.sender.send('personal-auth-error', 'Invalid auth token')
132+
})
91133
})
134+
135+
const logOut = () => {
136+
config.starling.accessToken = null
137+
storage.set('starling', config.starling, function(error) {
138+
loadPreAuthTray()
139+
})
140+
}
141+
142+
// required to enable certain system functions such as clipboard
143+
const createSystemMenu = () => {
144+
var template = [{
145+
label: "StarlingBar",
146+
submenu: [
147+
{ label: "About Application", selector: "orderFrontStandardAboutPanel:" },
148+
{ type: "separator" },
149+
{ label: "Quit", accelerator: "Command+Q", click: function() { app.quit() }}
150+
]
151+
}, {
152+
label: "Edit",
153+
submenu: [
154+
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
155+
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
156+
{ type: "separator" },
157+
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
158+
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
159+
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
160+
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
161+
]
162+
}]
163+
164+
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
165+
}

0 commit comments

Comments
 (0)