Skip to content

Commit 6fe6794

Browse files
committed
feat: Add checkbox validation to active or deactive plugin.
1 parent fd3e243 commit 6fe6794

File tree

2 files changed

+59
-30
lines changed

2 files changed

+59
-30
lines changed

browser/lib/wakatime-plugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ function sendWakatimeHeartBeat(
1111
hasFileChanges,
1212
isFileChange
1313
) {
14-
if (new Date().getTime() - lastHeartbeat > 120000 || isFileChange) {
14+
if (
15+
config.get().wakatime.isActive &&
16+
!!config.get().wakatime.key &&
17+
(new Date().getTime() - lastHeartbeat > 120000 || isFileChange)
18+
) {
1519
const notePath = path.join(storagePath, 'notes', noteKey + '.cson')
1620

1721
if (!isWrite && !hasFileChanges && !isFileChange) {

browser/main/modals/PreferencesModal/PluginsTab.js

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,15 @@ class PluginsTab extends React.Component {
3030
})
3131
}
3232
this.handleSettingError = err => {
33-
if (
34-
this.state.config.wakatime.key === '' ||
35-
this.state.config.wakatime.key === null
36-
) {
37-
this.setState({
38-
pluginsAlert: {
39-
type: 'success',
40-
message: i18n.__('Successfully applied!')
41-
}
42-
})
43-
} else {
44-
this.setState({
45-
pluginsAlert: {
46-
type: 'error',
47-
message:
48-
err.message != null ? err.message : i18n.__('An error occurred!')
49-
}
50-
})
51-
}
33+
this.setState({
34+
pluginsAlert: {
35+
type: 'error',
36+
message:
37+
err.message != null ? err.message : i18n.__('An error occurred!')
38+
}
39+
})
5240
}
53-
this.oldWakatimekey = this.state.config.wakatime
41+
this.oldWakatimeConfig = this.state.config.wakatime
5442
ipc.addListener('APP_SETTING_DONE', this.handleSettingDone)
5543
ipc.addListener('APP_SETTING_ERROR', this.handleSettingError)
5644
}
@@ -65,9 +53,10 @@ class PluginsTab extends React.Component {
6553
}
6654

6755
checkWakatimePluginRequirement() {
68-
if (!commandExists('wakatime-cli')) {
56+
const { wakatime } = this.state.config
57+
if (wakatime.isActive && !commandExists('wakatime-cli')) {
6958
this.setState({
70-
wakatimePlugin: {
59+
wakatimePluginAlert: {
7160
type: i18n.__('Warning'),
7261
message: i18n.__('Missing wakatime-cli')
7362
}
@@ -84,14 +73,17 @@ class PluginsTab extends React.Component {
8473
dialog.showMessageBox(remote.getCurrentWindow(), alertConfig)
8574
} else {
8675
this.setState({
87-
wakatimePlugin: null
76+
wakatimePluginAlert: null
8877
})
8978
}
9079
}
9180

9281
handleSaveButtonClick(e) {
9382
const newConfig = {
94-
wakatime: this.state.config.wakatime
83+
wakatime: {
84+
isActive: this.state.config.wakatime.isActive,
85+
key: this.state.config.wakatime.key
86+
}
9587
}
9688

9789
ConfigManager.set(newConfig)
@@ -105,13 +97,33 @@ class PluginsTab extends React.Component {
10597
this.checkPluginsRequirements()
10698
}
10799

100+
handleIsWakatimePluginActiveChange(e) {
101+
const { config } = this.state
102+
config.wakatime.isActive = !config.wakatime.isActive
103+
this.setState({
104+
config
105+
})
106+
if (_.isEqual(this.oldWakatimeConfig.isActive, config.wakatime.isActive)) {
107+
this.props.haveToSave()
108+
} else {
109+
this.props.haveToSave({
110+
tab: 'Plugins',
111+
type: 'warning',
112+
message: i18n.__('Unsaved Changes!')
113+
})
114+
}
115+
}
116+
108117
handleWakatimeKeyChange(e) {
109118
const { config } = this.state
110-
config.wakatime = { key: this.refs.key.value }
119+
config.wakatime = {
120+
isActive: true,
121+
key: this.refs.wakatimeKey.value
122+
}
111123
this.setState({
112124
config
113125
})
114-
if (_.isEqual(this.oldWakatimekey, config.wakatime)) {
126+
if (_.isEqual(this.oldWakatimeConfig.key, config.wakatime.key)) {
115127
this.props.haveToSave()
116128
} else {
117129
this.props.haveToSave({
@@ -137,7 +149,7 @@ class PluginsTab extends React.Component {
137149
<p className={`alert ${pluginsAlert.type}`}>{pluginsAlert.message}</p>
138150
) : null
139151

140-
const wakatimeAlert = this.state.wakatimePlugin
152+
const wakatimeAlert = this.state.wakatimePluginAlert
141153
const wakatimePluginAlertElement =
142154
wakatimeAlert != null ? (
143155
<p className={`alert ${wakatimeAlert.type}`}>{wakatimeAlert.message}</p>
@@ -150,12 +162,25 @@ class PluginsTab extends React.Component {
150162
<div styleName='group'>
151163
<div styleName='group-header'>{i18n.__('Plugins')}</div>
152164
<div styleName='group-section'>
153-
<div styleName='group-section-label'>{i18n.__('Wakatime key')}</div>
165+
<div styleName='group-section-label'>
166+
<div styleName='group-checkBoxSection'>
167+
<label>
168+
<input
169+
onChange={e => this.handleIsWakatimePluginActiveChange(e)}
170+
checked={config.wakatime.isActive}
171+
ref='wakatimeIsActive'
172+
type='checkbox'
173+
/>
174+
&nbsp;
175+
{i18n.__('Wakatime key')}
176+
</label>
177+
</div>
178+
</div>
154179
<div styleName='group-section-control'>
155180
<input
156181
styleName='group-section-control-input'
157182
onChange={e => this.handleWakatimeKeyChange(e)}
158-
ref='key'
183+
ref='wakatimeKey'
159184
value={config.wakatime.key}
160185
type='text'
161186
/>

0 commit comments

Comments
 (0)