Fix: Update month input when clicking on preselected ranges

This commit is contained in:
Bertrand Benjamin 2021-08-04 21:49:07 +02:00
parent 4d77e61b25
commit b266e9de9b
2 changed files with 64 additions and 44 deletions

View File

@ -4,12 +4,12 @@
<h2>Période</h2>
</li>
<li>
<input type="month" v-model="range.start">
<input type="month" v-model="range.end">
<input type="month" @input="updateStart" v-model="start">
<input type="month" @input="updateEnd" v-model="end">
</li>
<li>
<button @click="setRange6months" :active='selected=="month"'>6 mois</button>
<button @click="setRange1year" :active='selected=="year"'>1 an</button>
<button @click="setRangeFromJanuary" :active='selected=="january"'>Depuis le début de l'année</button>
<button @click="setRange1year" :active='selected=="year"'>Sur 1 an</button>
<button @click="setRangeAll" :active='selected=="all"'>Tout</button>
</li>
</ul>
@ -19,7 +19,7 @@
<script>
import { mapGetters, mapActions } from 'vuex'
import { addMonths, format, parseISO } from 'date-fns'
import { setMonth, addMonths, format, parseISO } from 'date-fns'
const today = new Date();
@ -30,25 +30,46 @@ export default {
data () {
return {
selected: "",
start: "",
end: "",
}
},
computed: {
...mapGetters('travail', {
range: "Range",
range: "range",
monthsDate: "MonthsDate",
})
},
watch: {
range: function (newRange, oldRange) {
this.start = this.range.start
this.end = this.range.end
},
},
mounted () {
this.setRangeFromJanuary()
this.start = this.range.start
this.end = this.range.end
},
methods: {
...mapActions('travail', {
setRange: "setRange",
}),
setRange6months: function () {
const start = addMonths(new Date(), -6)
updateStart: function () {
this.selected = "custom"
this.setRange({start: this.start, end: this.end})
},
updateEnd: function () {
this.selected = "custom"
this.setRange({start: this.start, end: this.end})
},
setRangeFromJanuary: function () {
const start = setMonth(today, 0)
const range = {
start: format(start, 'yyyy-MM'),
end: format(today, 'yyyy-MM'),
}
this.selected = "month"
this.selected = "january"
this.setRange(range)
},
setRange1year: function () {
@ -111,5 +132,4 @@ button {
background-color: white;
}
</style>

View File

@ -76,7 +76,7 @@ const travail = {
},
getters: {
TheEmptyMonth(state) { return { ...state.empty } },
Range(state) { return state.range },
range(state) { return state.range },
MonthsDate(state) {
// Get months inside the range
return Object.keys(state.months).filter(date => (date >= state.range.start) && (date <= state.range.end)).sort().reverse()