Tasks: print newly added tasks, fixed some bugs

This commit is contained in:
Filip Rojek 2023-12-31 02:39:01 +01:00
parent 4c9765b4a3
commit 5c769bfa2f
3 changed files with 46 additions and 6 deletions

View File

@ -44,21 +44,34 @@ export default {
} }
}, },
methods: { methods: {
startStopTimer() { async startStopTimer() {
if (this.timerState == 'Start') { if (this.timerState == 'Start') {
this.timerState = 'Stop' this.timerState = 'Stop'
this.task.startTime = Date.now() this.task.startTime = Date.now()
this.timeNow = Date.now()
console.log('timer started') console.log('timer started')
this.timer = setInterval(() => { this.timer = setInterval(() => {
this.timeNow = Date.now() this.timeNow = Date.now()
}, 1000) }, 1000)
} else { } else {
this.timerState = 'Start'
this.task.stopTime = Date.now() this.task.stopTime = Date.now()
this.timerState = 'Start'
clearInterval(this.timer) clearInterval(this.timer)
this.timer = undefined this.timer = undefined
AppStore.data.newTask = this.task this.timeNow = 0
console.log(AppStore.data.newTask) AppStore.data.newTask = {
title: this.task.title,
timeStart: this.task.startTime,
timeEnd: this.task.stopTime
}
await AppStore.sendAdd(AppStore.data.newTask, '/task/add')
this.task = {
startTime: 0,
stopTime: 0,
title: ''
}
this.$emit('get-tasks')
} }
} }
} }

View File

@ -31,5 +31,21 @@ export default {
} catch (error) { } catch (error) {
console.error('Error fetching data:', error) console.error('Error fetching data:', error)
} }
},
async sendAdd(data, url) {
try {
const response = await fetch(this.api_url + url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`)
}
} catch (error) {
console.error('Error sending data:', error)
}
} }
} }

View File

@ -6,11 +6,11 @@ import TaskRecord from '@/components/TaskRecord.vue'
<template> <template>
<main> <main>
<TrackerTimer /> <TrackerTimer @get-tasks="getTasks" />
<div class="task-day-wrapper"> <div class="task-day-wrapper">
<TaskHeader /> <TaskHeader />
<div class="task-wrapper" v-for="task in tasks" :key="task._id"> <div class="task-wrapper" v-for="task in sortedData" :key="task._id">
<TaskRecord :task="task" /> <TaskRecord :task="task" />
</div> </div>
</div> </div>
@ -26,6 +26,17 @@ export default {
tasks: AppStore.data.fetchedTasks tasks: AppStore.data.fetchedTasks
} }
}, },
methods: {
async getTasks() {
await AppStore.fetchData()
this.tasks = AppStore.data.fetchedTasks
}
},
computed: {
sortedData() {
return this.tasks.slice().sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
}
},
async mounted() { async mounted() {
await AppStore.fetchData() await AppStore.fetchData()
this.tasks = AppStore.data.fetchedTasks this.tasks = AppStore.data.fetchedTasks