Skip to content

Commit

Permalink
#5 Add desktop notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe HAMERLING committed Feb 26, 2019
1 parent 2714d76 commit da01046
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"axios": "^0.18.0",
"notifyjs": "^3.0.0",
"rxjs": "^6.3.3",
"vue": "^2.5.17",
"vue-analytics": "^5.16.1",
Expand Down
16 changes: 16 additions & 0 deletions src/notification.js
@@ -0,0 +1,16 @@
import Notification from "@/services/notification";

export default {
init
};

function init(store) {
Notification.enable()
.then(status => {
store.dispatch("setNotificationEnabled", status);
})
.catch(err => {
console.log(err);
store.dispatch("setNotificationEnabled", false);
});
}
39 changes: 39 additions & 0 deletions src/services/notification.js
@@ -0,0 +1,39 @@
import Notify from "notifyjs";

export default {
notify,
enable,
isSupported: Notify.isSupported
};

function notify(type, title, options = {}) {
isEnabled(type)
.then(() => doNotify())
.catch(err => console.warn(err));

function doNotify() {
new Notify(title, options).show();
}
}

function isEnabled() {
// TODO: Check if yhe type is allowed to notify
return enable();
}

function enable() {
return new Promise((resolve, reject) => {
if (!Notify.needsPermission) {
return resolve(true);
}

if (Notify.isSupported()) {
Notify.requestPermission(
() => resolve(true),
() => reject(new Error("You denied permission to display notifications"))
);
} else {
reject(new Error("Notification are not supported by your browser"));
}
});
}
2 changes: 2 additions & 0 deletions src/store/index.js
Expand Up @@ -14,6 +14,7 @@ import issue from "./modules/issue";
import pipeline from "./modules/pipeline";
import user from "./modules/user";
import settings from "./modules/settings";
import notification from "./modules/notification";

Vue.use(Vuex);

Expand All @@ -26,6 +27,7 @@ export default new Vuex.Store({
team,
user,
settings,
notification,
pipeline
},
state,
Expand Down
25 changes: 25 additions & 0 deletions src/store/modules/notification.js
@@ -0,0 +1,25 @@
const state = {
enabled: false // global notification state
};

const getters = {};

const actions = {
setNotificationEnabled({ commit }, value) {
commit("setEnabled", value);
}
};

const mutations = {
setEnabled(state, value) {
state.enabled = value;
}
};

export default {
namespaced: false,
state,
getters,
actions,
mutations
};

0 comments on commit da01046

Please sign in to comment.