Skip to content

Commit d2ebb9c

Browse files
committed
fix: fix timeline updating before new range is selected (#449 from activitywatch)
1 parent 1448792 commit d2ebb9c

3 files changed

Lines changed: 65 additions & 58 deletions

File tree

src/components/InputTimeInterval.vue

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,91 @@
11
<template lang="pug">
22
div
33
div
4-
table
5-
tr
6-
th.pr-2
7-
label(for="mode") Interval mode:
8-
td
9-
select(id="mode" v-model="mode")
10-
option(value='last_duration') Last duration
11-
option(value='range') Date range
12-
tr(v-if="mode == 'last_duration'")
13-
th.pr-2
14-
label(for="duration") Show last:
15-
td
16-
select(id="duration" :value="duration", @change="valueChanged")
17-
option(:value="15*60") 15min
18-
option(:value="30*60") 30min
19-
option(:value="60*60") 1h
20-
option(:value="2*60*60") 2h
21-
option(:value="4*60*60") 4h
22-
option(:value="6*60*60") 6h
23-
option(:value="12*60*60") 12h
24-
option(:value="24*60*60") 24h
25-
tr(v-if="mode == 'range'")
26-
th.pr-2 Range:
27-
td
28-
input(type="date", v-model="start", @input="valueChanged")
29-
input(type="date", v-model="end", @input="valueChanged")
4+
div(class="settings-bar")
5+
div
6+
label(for="mode") Interval mode:
7+
select(id="mode" v-model="mode")
8+
option(value='last_duration') Last duration
9+
option(value='range') Date range
10+
div(v-if="mode == 'last_duration'")
11+
label(for="duration") Show last:
12+
select(id="duration" :value="duration" @change="changeDuration")
13+
option(:value="15*60") 15min
14+
option(:value="30*60") 30min
15+
option(:value="60*60") 1h
16+
option(:value="2*60*60") 2h
17+
option(:value="4*60*60") 4h
18+
option(:value="6*60*60") 6h
19+
option(:value="12*60*60") 12h
20+
option(:value="24*60*60") 24h
21+
div(v-if="mode == 'range'")
22+
| Range:
23+
input(type="date", v-model="start")
24+
input(type="date", v-model="end")
25+
div
26+
button.btn.btn-outline-dark(
27+
type="button",
28+
:disabled="invalidDaterange || emptyDaterange",
29+
@click="showTimeline"
30+
) Show Timeline
31+
div(:style="{ color: 'red', visibility: invalidDaterange ? 'visible' : 'hidden' }")
32+
| The selected date range is invalid.
3033

3134
</template>
3235

33-
<style scoped lang="scss"></style>
36+
<style scoped lang="scss">
37+
.settings-bar {
38+
display: flex;
39+
justify-content: space-between;
40+
}
41+
</style>
3442

3543
<script>
3644
import moment from 'moment';
3745
3846
export default {
3947
name: 'input-timeinterval',
4048
props: {
41-
duration: {
49+
defaultDuration: {
4250
type: Number,
4351
default: 60 * 60,
4452
},
4553
},
4654
data: () => {
4755
return {
48-
now: moment(),
4956
mode: 'last_duration',
57+
duration: null,
5058
start: null,
5159
end: null,
5260
};
61+
},
62+
mounted: function() {
63+
this.duration = this.defaultDuration;
5364
},
5465
computed: {
55-
value: {
66+
daterange: {
5667
get() {
5768
if (this.mode == 'range' && this.start && this.end) {
5869
return [moment(this.start), moment(this.end)];
5970
} else {
60-
return [moment(this.now).subtract(this.duration, 'seconds'), moment(this.now)];
61-
}
62-
},
63-
set(newValue) {
64-
if (!isNaN(newValue)) {
65-
// Set new now and duration
66-
this.now = moment();
67-
this.duration = newValue;
68-
} else {
69-
// Not required for mode='range', start and end set directly through v-model
71+
return [moment().subtract(this.duration, 'seconds'), moment()];
7072
}
7173
},
7274
},
75+
emptyDaterange() {
76+
return this.mode == 'range' && !(this.start && this.end);
77+
},
78+
invalidDaterange() {
79+
return this.mode == 'range' && moment(this.start) >= moment(this.end);
80+
},
7381
},
7482
methods: {
75-
valueChanged(e) {
76-
this.value = e.target.value;
77-
this.$emit('input', this.value);
83+
showTimeline() {
84+
this.$emit('update-timeline', this.daterange);
85+
},
86+
changeDuration(e) {
87+
// This is only for last_duration, range mode changes daterange automatically
88+
this.duration = e.target.value;
7889
},
7990
},
8091
};

src/views/Bucket.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ div
2424
th Eventcount:
2525
td {{ eventcount }}
2626

27-
input-timeinterval(v-model="daterange")
27+
input-timeinterval(v-model="daterange", @update-timeline="updateTimeline")
2828

2929
vis-timeline(:buckets="[bucket_with_events]", :showRowLabels="false")
3030

@@ -52,11 +52,6 @@ export default {
5252
return this.$store.getters['buckets/getBucket'](this.id) || {};
5353
},
5454
},
55-
watch: {
56-
daterange: async function() {
57-
await this.getEvents(this.id);
58-
},
59-
},
6055
mounted: async function() {
6156
await this.$store.dispatch('buckets/ensureBuckets');
6257
await this.getEvents(this.id);
@@ -84,6 +79,10 @@ export default {
8479
console.error(':(');
8580
}
8681
},
82+
updateTimeline: async function(daterange) {
83+
this.daterange = daterange;
84+
await this.getEvents(this.id);
85+
}
8786
},
8887
};
8988
</script>

src/views/Timeline.vue

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
div
33
h2 Timeline
44

5-
input-timeinterval(v-model="daterange", v-bind:duration="timeintervalDefaultDuration")
5+
input-timeinterval(v-model="daterange", :defaultDuration="timeintervalDefaultDuration", @update-timeline="getBuckets")
66

77
div(v-show="buckets !== null")
88
div
@@ -25,7 +25,7 @@ export default {
2525
data: () => {
2626
return {
2727
buckets: null,
28-
daterange: [moment().subtract(1, 'hour'), moment()],
28+
daterange: null,
2929
timeintervalDefaultDuration: localStorage.durationDefault,
3030
};
3131
},
@@ -34,16 +34,13 @@ export default {
3434
return _.sumBy(this.buckets, 'events.length');
3535
},
3636
},
37-
watch: {
38-
daterange() {
39-
this.getBuckets();
40-
},
41-
},
4237
mounted: function() {
43-
this.getBuckets();
38+
this.daterange = [moment().subtract(this.timeintervalDefaultDuration, "seconds"), moment()],
39+
this.getBuckets(this.daterange);
4440
},
4541
methods: {
46-
getBuckets: async function() {
42+
getBuckets: async function(daterange) {
43+
this.daterange = daterange;
4744
this.buckets = await this.$store.dispatch('buckets/getBucketsWithEvents', {
4845
start: this.daterange[0].format(),
4946
end: this.daterange[1].format(),

0 commit comments

Comments
 (0)