Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
billangli committed Aug 19, 2020
1 parent a8b35d9 commit f8b876b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
44 changes: 25 additions & 19 deletions src/components/NewReleaseNotification.vue
Expand Up @@ -36,13 +36,17 @@ export default {
currentVersion: null,
latestVersion: null,
latestVersionDate: null,
// The following constants can be used in other files, such as ReleaseNotificationSettings.vue
SHORT_BACKOFF_PERIOD: SHORT_BACKOFF_PERIOD,
LONG_BACKOFF_PERIOD: LONG_BACKOFF_PERIOD,
};
},
async mounted() {
this.retrieveData();
if (this.data && (!this.data.isEnabled || moment() < moment(this.data.nextCheckTime))) return;
await this.retreiveReleases();
await this.retrieveCurrentVersion();
await this.retrieveLatestVersion();
this.isVisible = this.getHasNewRelease() && this.getReleaseIsReady();
if (this.isVisible && this.data) {
Expand Down Expand Up @@ -71,28 +75,30 @@ export default {
if (localStorage.getItem('newReleaseCheckData')) {
try {
this.data = JSON.parse(localStorage.getItem('newReleaseCheckData'));
} catch (e) {
} catch (err) {
console.error('newReleaseCheckData not found in localStorage: ', err);
localStorage.removeItem('newReleaseCheckData');
}
}
},
retreiveReleases() {
return Promise.all([
this.$aw.getInfo(),
axios.get('https://api.github.com/repos/ActivityWatch/activitywatch/releases/latest'),
])
.then(responses => {
this.currentVersion = this.cleanVersionTag(responses[0].version);
this.latestVersion = this.cleanVersionTag(responses[1].data.tag_name);
this.latestVersionDate = moment(responses[1].data.published_at);
})
.catch(err => {
// Should not display alert if failed, e.g. when user isn't connected to the internet
console.error('Check for new releases is unable to connect: ', err);
this.currentVersion = null;
this.latestVersion = null;
this.latestVersionDate = null;
});
async retrieveCurrentVersion() {
try {
const response = await this.$aw.getInfo();
this.currentVersion = this.cleanVersionTag(response.version);
} catch (err) {
console.error('unable to connect to aw-server: ', err);
}
},
async retrieveLatestVersion() {
try {
const response = await axios.get(
'https://api.github.com/repos/ActivityWatch/activitywatch/releases/latest'
);
this.latestVersion = this.cleanVersionTag(response.data.tag_name);
this.latestVersionDate = moment(response.data.published_at);
} catch (err) {
console.error('unable to connect to GitHub API to check for latest version: ', err);
}
},
cleanVersionTag(tag) {
tag = tag.trim();
Expand Down
6 changes: 3 additions & 3 deletions src/views/settings/ReleaseNotificationSettings.vue
Expand Up @@ -12,10 +12,9 @@ div.row

<script>
import moment from 'moment';
import NewReleaseNotification from '~/components/NewReleaseNotification.vue';
const SHORT_BACKOFF_PERIOD = 24 * 60 * 60;
// The following may be used for testing, roughly 10s
// const SHORT_BACKOFF_PERIOD = 10;
const SHORT_BACKOFF_PERIOD = NewReleaseNotification.SHORT_BACKOFF_PERIOD;
export default {
data() {
Expand All @@ -37,6 +36,7 @@ export default {
try {
this.data = JSON.parse(localStorage.getItem('newReleaseCheckData'));
} catch (e) {
console.error('newReleaseCheckData not found in localStorage');
localStorage.removeItem('newReleaseCheckData');
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/views/settings/Settings.vue
Expand Up @@ -14,9 +14,9 @@ div

hr

ReleaseNotificationSettings(v-if="isNewReleaseCheckEnabled")

hr(v-if="isNewReleaseCheckEnabled")
div(v-if="isNewReleaseCheckEnabled")
ReleaseNotificationSettings
hr

CategorizationSettings

Expand Down

0 comments on commit f8b876b

Please sign in to comment.