From cb529e47d90aafb228d5c41925508d6fa55f0d75 Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Sun, 31 Dec 2023 18:10:00 +0100 Subject: [PATCH] Tasks: presave active task, able to edit on button action --- api/src/controllers/taskController.ts | 23 +++++++++++ api/src/routes/apiRoutes.ts | 1 + frontend/src/components/TrackerTimer.vue | 50 ++++++++++++++++++------ 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/api/src/controllers/taskController.ts b/api/src/controllers/taskController.ts index c709099..7c37119 100644 --- a/api/src/controllers/taskController.ts +++ b/api/src/controllers/taskController.ts @@ -1,5 +1,6 @@ import { Request, Response } from 'express' import Task from '../models/Task' +import { Err, Succ } from '../services/globalService' export async function get(req: Request, res: Response) { try { @@ -20,3 +21,25 @@ export async function add(req: Request, res: Response) { res.status(500).send(error) } } + +export async function edit(req: Request, res: Response) { + try { + const payload = req.body + payload.author_id = res.locals.user._id + const task: any = await Task.findByPk(payload._id) + + if (!task) { + return res.status(400).json(new Err(400, 'Task not found')) + } + + task.title = payload.title + task.timeEnd = payload.timeEnd + task.timeStart = payload.timeStart + + await task.save() + + return res.json(new Succ(200, 'Task updated succesfully')) + } catch (error) { + return res.status(500).json(new Err(400, 'something went wrong', error)) + } +} diff --git a/api/src/routes/apiRoutes.ts b/api/src/routes/apiRoutes.ts index 0925861..ca5c5dd 100644 --- a/api/src/routes/apiRoutes.ts +++ b/api/src/routes/apiRoutes.ts @@ -8,6 +8,7 @@ export const router = Router() router.get('/task/get', requireAuth, taskController.get) router.post('/task/add', requireAuth, taskController.add) +router.post('/task/edit', requireAuth, taskController.edit) router.post('/auth/signup', userController.signup) router.post('/auth/signin', userController.signin) diff --git a/frontend/src/components/TrackerTimer.vue b/frontend/src/components/TrackerTimer.vue index 90c0708..e27a7c6 100644 --- a/frontend/src/components/TrackerTimer.vue +++ b/frontend/src/components/TrackerTimer.vue @@ -30,15 +30,16 @@ export default { timer: undefined, timerState: 'Start', task: { - startTime: 0, - stopTime: 0, + timeStart: 0, + timeEnd: 0, title: '' - } + }, + restore: false } }, computed: { formattedElapsedTime() { - const date = new Date(this.timeNow - this.task.startTime) + const date = new Date(this.timeNow - this.task.timeStart) const utc = date.toUTCString() return utc.substr(utc.indexOf(':') - 2, 8) } @@ -47,33 +48,60 @@ export default { async startStopTimer() { if (this.timerState == 'Start') { this.timerState = 'Stop' - this.task.startTime = Date.now() + console.log('TADY', this.restore) + if (this.restore == false) { + console.log('TADY') + this.task.timeStart = Date.now() + } this.timeNow = Date.now() console.log('timer started') this.timer = setInterval(() => { this.timeNow = Date.now() }, 1000) + if (!this.task._id) { + AppStore.data.newTask = { + title: this.task.title, + timeStart: this.task.timeStart + } + await AppStore.sendAdd(AppStore.data.newTask, '/task/add') + } } else { - this.task.stopTime = Date.now() + this.task.timeEnd = Date.now() this.timerState = 'Start' clearInterval(this.timer) this.timer = undefined this.timeNow = 0 + AppStore.data.newTask = { title: this.task.title, - timeStart: this.task.startTime, - timeEnd: this.task.stopTime + timeStart: this.task.timeStart, + timeEnd: this.task.timeEnd + } + if (this.task._id) { + AppStore.data.newTask._id = this.task._id + await AppStore.sendAdd(AppStore.data.newTask, '/task/edit') + } else { + await AppStore.sendAdd(AppStore.data.newTask, '/task/add') } - await AppStore.sendAdd(AppStore.data.newTask, '/task/add') this.task = { - startTime: 0, - stopTime: 0, + timeStart: 0, + timeEnd: 0, title: '' } + this.restore = false this.$emit('get-tasks') } } + }, + async mounted() { + await AppStore.fetchData() + const task = AppStore.data.fetchedTasks.filter((task) => task.timeEnd === null) + if (task.length > 0) { + this.task = task[0] + this.restore = true + this.startStopTimer() + } } }