Feat: locale for date and button for adding next month

This commit is contained in:
Bertrand Benjamin 2021-08-12 15:12:57 +02:00
parent 37d1967343
commit 35666dce00
4 changed files with 59 additions and 18 deletions

View File

@ -1,12 +1,22 @@
<template> <template>
<div class="boxed-green month-presentation" id="new-month"> <div class="toolbar">
<button v-show='!addingMonth' @click='toggleAdding'>
Ajouter {{ formatedDate }}
</button>
</div>
<div class="boxed-green month-presentation" id="new-month" v-show='addingMonth'>
<div class="date"> <div class="date">
<input type="month" v-model="monthDate"> <div class="month">
{{ theMonth }}
</div>
<div class="year">
{{ theYear }}
</div>
<div class="actions"> <div class="actions">
<button class="validate" @click="save"> Valider </button> <button class="validate" @click="save"> Valider </button>
<button class="cancel" @click="cancel"> Annuler </button> <button class="cancel" @click="cancel"> Annuler </button>
</div> </div>
</div> </div>
<div class="datas"> <div class="datas">
<li v-for="cara in monthDesc" :key='cara.name'> <li v-for="cara in monthDesc" :key='cara.name'>
@ -19,13 +29,8 @@
<script> <script>
import { mapGetters, mapActions } from 'vuex' import { mapGetters, mapActions } from 'vuex'
const today = new Date(); import { parseISO, addMonths, format } from 'date-fns'
function formatDate(date) { import frLocal from 'date-fns/locale/fr'
var y = ''+date.getFullYear()
var m = ''+(date.getMonth()+1)
if (m.length < 2) { m = '0'+m}
return [y, m].join('-')
}
export default { export default {
name: 'NewMonth', name: 'NewMonth',
@ -35,20 +40,41 @@ export default {
}, },
data () { data () {
return { return {
monthDate: formatDate(today), monthDate: new Date(),
monthCopy: Object, monthCopy: Object,
addingMonth: false,
} }
}, },
mounted () { mounted () {
this.monthCopy = this.theEmptyMonth this.monthCopy = this.theEmptyMonth
}, },
watch: {
lastMonthDate: function () {
if (this.lastMonthDate) {
this.monthDate = addMonths(parseISO(this.lastMonthDate, "yyyy-MM", new Date()), 1)
} else {
this.monthDate = new Date()
}
},
},
computed: { computed: {
...mapGetters('travail', { ...mapGetters('travail', {
'theEmptyMonth': 'TheEmptyMonth', 'theEmptyMonth': 'TheEmptyMonth',
'lastMonthDate': 'lastMonthDate',
}), }),
...mapGetters('config', { ...mapGetters('config', {
'monthDesc': 'monthDesc', 'monthDesc': 'monthDesc',
}), }),
formatedDate: function () {
return format(this.monthDate, "MMMM YYY", {locale: frLocal} )
},
theMonth: function () {
return format(this.monthDate, "MMM", {locale: frLocal})
},
theYear: function () {
return format(this.monthDate, "YYY", {locale: frLocal})
},
}, },
methods: { methods: {
...mapActions('travail', { ...mapActions('travail', {
@ -57,14 +83,19 @@ export default {
...mapActions('config', { ...mapActions('config', {
'writeData': 'writeData', 'writeData': 'writeData',
}), }),
toggleAdding: function () {
this.addingMonth = !this.addingMonth
},
save: function () { save: function () {
console.log("save") console.log("save")
console.log(this.monthCopy) console.log(this.monthCopy)
this.createMonth({date: this.monthDate, month: this.monthCopy}) this.createMonth({date: format(this.monthDate, "yyyy-MM"), month: this.monthCopy})
this.toggleAdding()
this.writeData() this.writeData()
}, },
cancel: function () { cancel: function () {
this.monthCopy = this.theEmptyMonth this.toggleAdding()
this.monthCopy = this.theEmptyMonth
}, },
}, },
} }
@ -104,4 +135,7 @@ export default {
.value { .value {
font-weight: bold; font-weight: bold;
} }
.toolbar {
text-align: center;
}
</style> </style>

View File

@ -34,6 +34,7 @@
<script> <script>
import { mapActions, mapGetters } from 'vuex' import { mapActions, mapGetters } from 'vuex'
import { parseISO, format } from 'date-fns' import { parseISO, format } from 'date-fns'
import frLocal from 'date-fns/locale/fr'
export default { export default {
name: 'MonthPresentation', name: 'MonthPresentation',
props: { props: {
@ -59,7 +60,7 @@ export default {
return parseISO(this.TheDate, "yyyy-MM", new Date()) return parseISO(this.TheDate, "yyyy-MM", new Date())
}, },
theMonth: function () { theMonth: function () {
return format(this.rawDate, "MMM", ) return format(this.rawDate, "MMM", {locale: frLocal})
}, },
theYear: function () { theYear: function () {
return format(this.rawDate, "YYY", ) return format(this.rawDate, "YYY", )

View File

@ -34,6 +34,10 @@ const travail = {
// Get all the months // Get all the months
return Object.keys(state.months).sort().reverse() return Object.keys(state.months).sort().reverse()
}, },
lastMonthDate(state) {
// Return the date of the last registered month
return Object.keys(state.months).sort().reverse()[0]
},
months: (state, getters) => { months: (state, getters) => {
// Get in range months // Get in range months
const a = Object.keys(state.months) const a = Object.keys(state.months)

View File

@ -3,7 +3,7 @@ body {
} }
button { button {
color: white; color: black;
padding: 4px; padding: 4px;
box-shadow: 1px 1px 2px gray; box-shadow: 1px 1px 2px gray;
text-align: center; text-align: center;
@ -17,6 +17,8 @@ button {
} }
button:hover { button:hover {
background-color: black;
color: white;
transition: all 0.2s ease-out; transition: all 0.2s ease-out;
} }