Skip to content

Commit

Permalink
✨ Completed task uses estimated time if tracked blank (close #43)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveryh committed Nov 1, 2020
1 parent 6327f8b commit 641025a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
9 changes: 5 additions & 4 deletions client/src/components/Task.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@
color="green"
dense
round
@click="
task.complete = true
taskUpdate(task)
"
@click="taskComplete(task)"
icon="mdi-check"
></q-btn>
<q-btn
Expand Down Expand Up @@ -224,6 +221,7 @@ import {
A_TASK_DELETE,
A_TASK_TIMER_START,
A_TASK_TIMER_STOP,
A_TASK_COMPLETE,
} from '@/store/actions.type'
import { mapState } from 'vuex'
Expand Down Expand Up @@ -341,6 +339,9 @@ export default {
taskTimerStop(task) {
this.$store.dispatch(A_TASK_TIMER_STOP, task)
},
taskComplete(task) {
this.$store.dispatch(A_TASK_COMPLETE, task)
},
// editor
editorOpen() {
this.editedTask = Object.assign({}, this.task)
Expand Down
1 change: 1 addition & 0 deletions client/src/store/actions.type
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const A_TASK_UPDATE = "actionTaskUpdate";
export const A_TASK_DELETE = "actionTaskDelete";
export const A_TASK_TIMER_START = "actionTaskTimerStart";
export const A_TASK_TIMER_STOP = "actionTaskTimerStop";
export const A_TASK_COMPLETE = "actionTaskComplete";
export const A_TASK_REORDER = "actionTaskReorder";
export const A_STATS_RETRIEVE = "actionStatsRetrieve";
export const A_CATEGORY_RETRIEVE = "actionCategoryRetrieve";
Expand Down
13 changes: 13 additions & 0 deletions client/src/store/home.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
A_TASK_UPDATE,
A_TASK_TIMER_START,
A_TASK_TIMER_STOP,
A_TASK_COMPLETE,
A_TASK_REORDER,
} from './actions.type'
import {
Expand Down Expand Up @@ -98,6 +99,18 @@ const actions = {
})
})
},
[A_TASK_COMPLETE](context, task) {
return new Promise(resolve => {
ApiService.put(`tasks/${task._id}/complete`, task)
.then(({ data }) => {
context.commit(M_TASK_UPDATE, data)
resolve(data)
})
.catch(({ response }) => {
context.commit(M_ERROR_SET, response.data.errors)
})
})
},
[A_TASK_DELETE](context, task) {
return new Promise(resolve => {
ApiService.delete(`tasks/${task._id}`, task)
Expand Down
44 changes: 44 additions & 0 deletions server/routes/api/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,50 @@ router.put('/:task', auth.required, function (req, res, next) {
.catch(next)
})

router.put('/:task/complete', auth.required, function (req, res, next) {
User.findById(req.payload.id)
.then(function (user) {
if (!user) {
return res.sendStatus(401)
}

// authorized if user is author of task
if (req.task.author._id.toString() === req.payload.id.toString()) {
if (req.task.timerActive) {
// stop the timer if it was running
numSeconds = parseInt((Date.now() - req.task.timerStartedAt) / 1000)

req.task.timerActive = false
req.task.timerTrackedTime += numSeconds
req.task.timerStartedAt = null
}

// set the tracked time to estimated
if (
((req.task.timerTrackedTime == 0) |
(req.task.timerTrackedTime == null)) &
(req.task.timerEstimatedTime != 0) &
!isNaN(req.task.timerEstimatedTime)
) {
req.task.timerTrackedTime = req.task.timerEstimatedTime
}

// complete task
req.task.complete = true

req.task
.save()
.then(function (task) {
return res.json(task)
})
.catch(next)
} else {
return res.sendStatus(401)
}
})
.catch(next)
})

router.put('/:task/start', auth.required, function (req, res, next) {
User.findById(req.payload.id)
.then(function (user) {
Expand Down

0 comments on commit 641025a

Please sign in to comment.