feat: task reminders script setup (#1934)
continuous-integration/drone/push Build is passing Details

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: #1934
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-05-14 14:49:53 +00:00 committed by konrad
parent fe698a6f84
commit 0a89e8dc6b
1 changed files with 75 additions and 74 deletions

View File

@ -6,7 +6,7 @@
:class="{ 'overdue': r < new Date()}" :class="{ 'overdue': r < new Date()}"
class="reminder-input" class="reminder-input"
> >
<datepicker <Datepicker
v-model="reminders[index]" v-model="reminders[index]"
:disabled="disabled" :disabled="disabled"
@close-on-change="() => addReminderDate(index)" @close-on-change="() => addReminderDate(index)"
@ -16,7 +16,7 @@
</a> </a>
</div> </div>
<div class="reminder-input" v-if="!disabled"> <div class="reminder-input" v-if="!disabled">
<datepicker <Datepicker
v-model="newReminder" v-model="newReminder"
@close-on-change="() => addReminderDate()" @close-on-change="() => addReminderDate()"
:choose-date-label="$t('task.addReminder')" :choose-date-label="$t('task.addReminder')"
@ -25,87 +25,88 @@
</div> </div>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import {defineComponent} from 'vue' import {PropType, ref, onMounted, watch} from 'vue'
import datepicker from '@/components/input/datepicker.vue' import Datepicker from '@/components/input/datepicker.vue'
export default defineComponent({ type Reminder = Date | string
name: 'reminders',
data() { const props = defineProps({
return { modelValue: {
newReminder: null, type: Array as PropType<Reminder[]>,
reminders: [], default: () => [],
} validator(prop) {
}, // This allows arrays of Dates and strings
props: { if (!(prop instanceof Array)) {
modelValue: { return false
default: () => [], }
validator: prop => {
// This allows arrays of Dates and strings const isDate = (e: any) => e instanceof Date
if (!(prop instanceof Array)) { const isString = (e: any) => typeof e === 'string'
for (const e of prop) {
if (!isDate(e) && !isString(e)) {
console.log('validation failed', e, e instanceof Date)
return false return false
} }
for (const e of prop) {
const isDate = e instanceof Date
const isString = typeof e === 'string'
if (!isDate && !isString) {
console.log('validation failed', e, e instanceof Date)
return false
}
}
return true
},
},
disabled: {
type: Boolean,
default: false,
},
},
emits: ['update:modelValue', 'change'],
components: {
datepicker,
},
mounted() {
this.reminders = this.modelValue
},
watch: {
modelValue(newVal) {
for (const i in newVal) {
if (typeof newVal[i] === 'string') {
newVal[i] = new Date(newVal[i])
}
}
this.reminders = newVal
},
},
methods: {
updateData() {
this.$emit('update:modelValue', this.reminders)
this.$emit('change')
},
addReminderDate(index = null) {
// New Date
if (index === null) {
if (this.newReminder === null) {
return
}
this.reminders.push(new Date(this.newReminder))
this.newReminder = null
} else if(this.reminders[index] === null) {
return
} }
this.updateData() return true
},
removeReminderByIndex(index) {
this.reminders.splice(index, 1)
this.updateData()
}, },
}, },
disabled: {
type: Boolean,
default: false,
},
}) })
const emit = defineEmits(['update:modelValue', 'change'])
const reminders = ref<Reminder[]>([])
onMounted(() => {
reminders.value = props.modelValue
})
watch(
() => props.modelValue,
(newVal) => {
for (const i in newVal) {
if (typeof newVal[i] === 'string') {
newVal[i] = new Date(newVal[i])
}
}
reminders.value = newVal
},
)
function updateData() {
emit('update:modelValue', reminders.value)
emit('change')
}
const newReminder = ref(null)
function addReminderDate(index : number | null = null) {
// New Date
if (index === null) {
if (newReminder.value === null) {
return
}
reminders.value.push(new Date(newReminder.value))
newReminder.value = null
} else if(reminders.value[index] === null) {
return
}
updateData()
}
function removeReminderByIndex(index: number) {
reminders.value.splice(index, 1)
updateData()
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>