Skip to content

Commit

Permalink
fix: ajaxData -> updateData, получение данных без задержки первой отр…
Browse files Browse the repository at this point in the history
…исовки
  • Loading branch information
popstas committed Jun 24, 2019
1 parent 48ded1a commit f69e28a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
5 changes: 4 additions & 1 deletion components/Entry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
width: 2rem;
}
.entry__second {
margin-left: 2rem;
margin-left: 2.3rem;
}
.entry__project {
}
Expand Down Expand Up @@ -65,8 +65,11 @@ export default {
computed: {
duration() {
// normal entry
// console.log('entry: ', this.entry);
if (this.entry.dur > 0) return Math.round(this.entry.dur / 1000 / 60);
if (this.entry.dur === 0) return 0;
// running entry
const seconds = Math.round(this.entry.dur + this.currentTickTime / 1000);
const t = {
Expand Down
78 changes: 46 additions & 32 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,11 @@
let apiUrl = store.state.apiUrl;
try {
asyncData = {
entries: {
current: [await app.$axios.$get(apiUrl + '/toggl/entries/current')],
today: await app.$axios.$get(apiUrl + '/toggl/entries', {params: {type: 'today'}}),
pending: await app.$axios.$get(apiUrl + '/toggl/entries', {params: {type: 'pending'}}),
last: await app.$axios.$get(apiUrl + '/toggl/entries', {params: {type: 'last'}}),
}
};
if(asyncData.entries.current[0].id == 0){
asyncData.entries.current = [];
}
// sort by date desc
asyncData.entries.today.sort((a, b) => b.id - a.id);
asyncData.entries.pending.sort((a, b) => b.id - a.id);
asyncData.entries.last.sort((a, b) => b.id - a.id);
asyncData.tabs = [
{label: 'Сейчас', props: {entries: asyncData.entries.current}},
{label: 'Сегодня', props: {entries: asyncData.entries.today}},
{label: 'Ожидают', props: {entries: asyncData.entries.pending}},
{label: 'Неделя', props: {entries: asyncData.entries.last}},
];
// let analitics = await app.$axios.$get(apiUrl + '/planfix/analitics');
// store.commit('analitics', analitics.Analitics);
asyncData.pages = [
{name: 'Записи', page: EntriesTabsPage},
{name: 'Настройки', page: SettingsPage},
];
store.commit('tabs', asyncData.tabs);
}
catch (e) {
console.log('error while get data');
Expand All @@ -104,6 +76,28 @@
this.$store.commit('tabs', tabs);
},
async updateData() {
this.entries = this.entries || { current: [], today: [], pending: [], last: [] };
this.entries.current = [await this.$axios.$get(this.$store.state.apiUrl + '/toggl/entries/current')];
this.entries.today = await this.$axios.$get(this.$store.state.apiUrl + '/toggl/entries', {params: {type: 'today'}});
this.entries.pending = await this.$axios.$get(this.$store.state.apiUrl + '/toggl/entries', {params: {type: 'pending'}});
this.entries.last = await this.$axios.$get(this.$store.state.apiUrl + '/toggl/entries', {params: {type: 'last'}});
// check for empty current
// console.log('this.entries.current: ', this.entries.current);
if(this.entries.current[0].id == 0){
this.entries.current = [];
}
// sort by date desc
this.entries.today.sort((a, b) => b.id - a.id);
this.entries.pending.sort((a, b) => b.id - a.id);
this.entries.last.sort((a, b) => b.id - a.id);
this.updateTabs();
},
setUpdateIntervals(currentTimeout, otherTimeout) {
clearInterval(this.currentInterval);
clearInterval(this.otherInterval);
Expand All @@ -118,28 +112,48 @@
this.otherInterval = setInterval(async () => {
// console.log('other data');
// console.log(new Date());
this.entries.today = await app.$axios.$get(apiUrl + '/toggl/entries', {params: {type: 'today'}});
this.entries.pending = await app.$axios.$get(apiUrl + '/toggl/entries', {params: {type: 'pending'}});
this.entries.last = await app.$axios.$get(apiUrl + '/toggl/entries', {params: {type: 'last'}});
this.entries.today = await this.$axios.$get(this.$store.state.apiUrl + '/toggl/entries', {params: {type: 'today'}});
this.entries.pending = await this.$axios.$get(this.$store.state.apiUrl + '/toggl/entries', {params: {type: 'pending'}});
this.entries.last = await this.$axios.$get(this.$store.state.apiUrl + '/toggl/entries', {params: {type: 'last'}});
// console.log('this.entries: ', this.entries);
this.updateTabs();
}, otherTimeout);
}
},
created() {
// console.log('created');
this.pages = [
{name: 'Записи', page: EntriesTabsPage},
{name: 'Настройки', page: SettingsPage},
];
const tabs = [
{label: 'Сейчас', props: {entries: []}},
{label: 'Сегодня', props: {entries: []}},
{label: 'Ожидают', props: {entries: []}},
{label: 'Неделя', props: {entries: []}},
];
this.$store.commit('tabs', tabs);
this.updateData();
this.setUpdateIntervals(10000, 600000);
},
onIdle() {
// console.log('idle');
this.setUpdateIntervals(300000, 1800000);
},
onActive() {
async onActive() {
// console.log('active');
this.updateData();
this.setUpdateIntervals(10000, 600000);
},
head() {
if (!this.entries.current) return 'planfix-toggl';
return { title: `${this.entries.current[0].description} - planfix-toggl` };
}
}
Expand Down

0 comments on commit f69e28a

Please sign in to comment.