Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Add data refresher component
Browse files Browse the repository at this point in the history
  • Loading branch information
Allypost committed May 13, 2021
1 parent 2549bbe commit a2ad6b1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
31 changes: 31 additions & 0 deletions components/DataRefresher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export default {
data: () => ({
timers: {},
listeners: {},
}),
beforeDestroy() {
Expand All @@ -18,6 +19,19 @@
() => this.$store.dispatch("external/live/fetchYoutubeLiveVideoId"),
14000 + 2000 * Math.random(),
);
this.addTimer(
"jobFairLiveCheck",
() => this.$store.dispatch("meta/health/fetchLiveStatus"),
4000 + 2000 * Math.random(),
);
const setOnline = (toStatus) => () => {
this.$store.commit("meta/health/SET_ONLINE", Boolean(toStatus));
};
this.addListener("online", setOnline(true));
this.addListener("offline", setOnline(false));
},
methods: {
Expand All @@ -33,11 +47,28 @@
);
},
addListener(event, fn) {
const oldListener = this.listeners[event];
if (oldListener) {
window.removeEventListener(event, oldListener);
}
this.$set(this.listeners, "event", fn);
window.addEventListener(event, fn);
},
removeTimers() {
for (const { timer } of Object.values(this.timers)) {
clearInterval(timer);
}
},
removeListeners() {
for (const [ type, listener ] of Object.entries(this.listeners)) {
window.removeEventListener(type, listener);
}
},
},
};
</script>
33 changes: 1 addition & 32 deletions components/HealthStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@

<script>
import {
mapActions,
mapGetters,
} from "vuex";
export default {
data: () => ({
timer: null,
isOnline: true,
}),
computed: {
...mapGetters("meta/health", [
"isLive",
"isOnline",
]),
show() {
Expand All @@ -55,32 +50,6 @@
return "Nešto je pošlo po zlu. Molimo budite strpljivi dok otklanjamo poteškoće";
},
},
mounted() {
this.timer = setInterval(
() =>
this.fetchLiveStatus()
,
4000 + 2000 * Math.random(),
);
const setOnline = (toStatus) => () => {
this.isOnline = Boolean(toStatus);
};
window.addEventListener("online", setOnline(true));
window.addEventListener("offline", setOnline(false));
},
beforeDestroy() {
clearInterval(this.timer);
},
methods: {
...mapActions("meta/health", [
"fetchLiveStatus",
]),
},
};
</script>

Expand Down
4 changes: 4 additions & 0 deletions layouts/admin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@
<nuxt :class="$style.pageContainer" />
</v-container>
</v-main>

<data-refresher />
</v-app>
</template>

<script>
import {
mapGetters,
} from "vuex";
import DataRefresher from "../components/DataRefresher";
import NavUserModule from "~/components/NavUserModule";
import {
generateMetadata,
Expand All @@ -122,6 +125,7 @@
export default {
name: "LayoutAdmin",
components: {
DataRefresher,
NavUserModule,
MenuIcon,
},
Expand Down
4 changes: 4 additions & 0 deletions layouts/company.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,13 @@
<nuxt :class="$style.pageContainer" />
</v-container>
</v-main>

<data-refresher />
</v-app>
</template>

<script>
import DataRefresher from "../components/DataRefresher";
import NavUserModule from "~/components/NavUserModule";
import TranslatedText from "~/components/TranslatedText";
import {
Expand All @@ -129,6 +132,7 @@
name: "LayoutCompany",
components: {
DataRefresher,
TranslatedText,
NavUserModule,
MenuIcon,
Expand Down
3 changes: 3 additions & 0 deletions layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
<app-footer />

<health-status />
<data-refresher />
</v-app>
</template>

<script>
import AppNavBar from "@/components/NavBar";
import AppNavDrawer from "@/components/NavDrawer";
import AppFooter from "@/components/Footer";
import DataRefresher from "../components/DataRefresher";
import HealthStatus from "../components/HealthStatus";
import {
generateMetadata,
Expand All @@ -29,6 +31,7 @@
name: "LayoutDefault",
components: {
DataRefresher,
HealthStatus,
AppNavBar,
AppNavDrawer,
Expand Down
9 changes: 9 additions & 0 deletions store/meta/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@ import Vue from "vue";

export const state = () => ({
live: true,
online: true,
});

export const getters = {
isLive(state) {
return state.live;
},

isOnline(state) {
return state.online;
},
};

export const mutations = {
SET_LIVE(state, live) {
Vue.set(state, "live", live);
},

SET_ONLINE(state, online) {
Vue.set(state, "online", online);
},
};

export const actions = {
Expand Down

0 comments on commit a2ad6b1

Please sign in to comment.