Tasks: presave active task, able to edit on button action

This commit is contained in:
Filip Rojek 2023-12-31 18:10:00 +01:00
parent dabd5ea0f0
commit cb529e47d9
3 changed files with 63 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import { Request, Response } from 'express' import { Request, Response } from 'express'
import Task from '../models/Task' import Task from '../models/Task'
import { Err, Succ } from '../services/globalService'
export async function get(req: Request, res: Response) { export async function get(req: Request, res: Response) {
try { try {
@ -20,3 +21,25 @@ export async function add(req: Request, res: Response) {
res.status(500).send(error) 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))
}
}

View File

@ -8,6 +8,7 @@ export const router = Router()
router.get('/task/get', requireAuth, taskController.get) router.get('/task/get', requireAuth, taskController.get)
router.post('/task/add', requireAuth, taskController.add) router.post('/task/add', requireAuth, taskController.add)
router.post('/task/edit', requireAuth, taskController.edit)
router.post('/auth/signup', userController.signup) router.post('/auth/signup', userController.signup)
router.post('/auth/signin', userController.signin) router.post('/auth/signin', userController.signin)

View File

@ -30,15 +30,16 @@ export default {
timer: undefined, timer: undefined,
timerState: 'Start', timerState: 'Start',
task: { task: {
startTime: 0, timeStart: 0,
stopTime: 0, timeEnd: 0,
title: '' title: ''
} },
restore: false
} }
}, },
computed: { computed: {
formattedElapsedTime() { formattedElapsedTime() {
const date = new Date(this.timeNow - this.task.startTime) const date = new Date(this.timeNow - this.task.timeStart)
const utc = date.toUTCString() const utc = date.toUTCString()
return utc.substr(utc.indexOf(':') - 2, 8) return utc.substr(utc.indexOf(':') - 2, 8)
} }
@ -47,33 +48,60 @@ export default {
async startStopTimer() { async startStopTimer() {
if (this.timerState == 'Start') { if (this.timerState == 'Start') {
this.timerState = 'Stop' 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() 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)
if (!this.task._id) {
AppStore.data.newTask = {
title: this.task.title,
timeStart: this.task.timeStart
}
await AppStore.sendAdd(AppStore.data.newTask, '/task/add')
}
} else { } else {
this.task.stopTime = Date.now() this.task.timeEnd = Date.now()
this.timerState = 'Start' this.timerState = 'Start'
clearInterval(this.timer) clearInterval(this.timer)
this.timer = undefined this.timer = undefined
this.timeNow = 0 this.timeNow = 0
AppStore.data.newTask = { AppStore.data.newTask = {
title: this.task.title, title: this.task.title,
timeStart: this.task.startTime, timeStart: this.task.timeStart,
timeEnd: this.task.stopTime 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 = { this.task = {
startTime: 0, timeStart: 0,
stopTime: 0, timeEnd: 0,
title: '' title: ''
} }
this.restore = false
this.$emit('get-tasks') 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()
}
} }
} }
</script> </script>