feat: create new tasks

This commit is contained in:
kolaente 2022-07-20 18:53:55 +02:00
parent e711df758c
commit b6c776592b
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 68 additions and 2 deletions

View file

@ -15,17 +15,42 @@
:bars="bar" :bars="bar"
/> />
</g-gantt-chart> </g-gantt-chart>
<form
@submit.prevent="createTask()"
class="add-new-task"
v-if="canWrite"
>
<transition name="width">
<input
@blur="hideCreateNewTask"
@keyup.esc="newTaskFieldActive = false"
class="input"
ref="newTaskTitleField"
type="text"
v-if="newTaskFieldActive"
v-model="newTaskTitle"
/>
</transition>
<x-button @click="showCreateNewTask" :shadow="false" icon="plus">
{{ $t('list.list.newTaskCta') }}
</x-button>
</form>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import {ref} from 'vue' import {computed, nextTick, ref} from 'vue'
import TaskCollectionService from '@/services/taskCollection' import TaskCollectionService from '@/services/taskCollection'
import {format} from 'date-fns' import {format} from 'date-fns'
import {colorIsDark} from '@/helpers/color/colorIsDark' import {colorIsDark} from '@/helpers/color/colorIsDark'
import TaskService from '@/services/task' import TaskService from '@/services/task'
import {useStore} from 'vuex'
import Rights from '../../models/constants/rights.json'
import TaskModel from '@/models/task'
const dateFormat = 'yyyy-LL-dd kk:mm' const dateFormat = 'yyyy-LL-dd kk:mm'
const store = useStore()
const props = defineProps({ const props = defineProps({
listId: { listId: {
type: Number, type: Number,
@ -45,6 +70,8 @@ const props = defineProps({
}, },
}) })
const canWrite = computed(() => store.state.currentList.maxRight > Rights.READ)
const tasks = ref([]) const tasks = ref([])
const ganttBars = ref([]) const ganttBars = ref([])
@ -54,6 +81,7 @@ const defaultEndDate = format(new Date((new Date()).setDate((new Date()).getDate
// We need a "real" ref object for the gantt bars to instantly update the tasks when they are dragged on the chart. // We need a "real" ref object for the gantt bars to instantly update the tasks when they are dragged on the chart.
// A computed won't work directly. // A computed won't work directly.
function mapGanttBars() { function mapGanttBars() {
ganttBars.value = []
tasks.value.forEach(t => ganttBars.value.push([{ tasks.value.forEach(t => ganttBars.value.push([{
startDate: t.startDate ? format(t.startDate, dateFormat) : defaultStartDate, startDate: t.startDate ? format(t.startDate, dateFormat) : defaultStartDate,
endDate: t.endDate ? format(t.endDate, dateFormat) : defaultEndDate, endDate: t.endDate ? format(t.endDate, dateFormat) : defaultEndDate,
@ -113,6 +141,45 @@ async function updateTask(e) {
await taskService.update(task) await taskService.update(task)
// TODO: Loading animation // TODO: Loading animation
} }
const newTaskFieldActive = ref(false)
const newTaskTitleField = ref()
const newTaskTitle = ref('')
function showCreateNewTask() {
if (!newTaskFieldActive.value) {
// Timeout to not send the form if the field isn't even shown
setTimeout(() => {
newTaskFieldActive.value = true
nextTick(() => newTaskTitleField.value.focus())
}, 100)
}
}
function hideCreateNewTask() {
if (newTaskTitle.value === '') {
nextTick(() => (newTaskFieldActive.value = false))
}
}
async function createTask() {
if (!newTaskFieldActive.value) {
return
}
let task = new TaskModel({
title: newTaskTitle.value,
listId: props.listId,
startDate: defaultStartDate,
endDate: defaultEndDate,
})
const taskService = new TaskService()
const r = await taskService.create(task)
tasks.value.set(r.id, r)
mapGanttBars()
newTaskTitle.value = ''
hideCreateNewTask()
}
</script> </script>
<style> <style>

View file

@ -56,7 +56,6 @@
:date-to="dateTo" :date-to="dateTo"
:precision="precision" :precision="precision"
:list-id="props.listId" :list-id="props.listId"
:show-taskswithout-dates="showTaskswithoutDates"
/> />
</card> </card>