From e72fd342266680f40721c0a0e0e0f2b08aefb390 Mon Sep 17 00:00:00 2001 From: Askerov Javid Date: Tue, 5 Dec 2017 16:50:32 +0400 Subject: [PATCH] timetracking now works for projects as well --- client/components/projects/Project.js | 5 +++++ client/reducers/projects.js | 10 ++++++++++ controllers/timelogController.js | 14 ++++++++++++-- models/Project.js | 4 ++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/client/components/projects/Project.js b/client/components/projects/Project.js index 0ab9f92..0468aaf 100644 --- a/client/components/projects/Project.js +++ b/client/components/projects/Project.js @@ -9,6 +9,7 @@ import NotLoggedIn from '../NotLoggedIn'; import NotFound from '../NotFound'; import TasksList from '../tasks/TasksList'; import AddForm from './AddForm'; +import { formatTime } from '../../helpers'; class Project extends React.Component { constructor(props) { @@ -100,11 +101,15 @@ class Project extends React.Component { return ; } const addLinkText = this.state.showForm ? 'Hide The Form' : 'New Task'; + const projectTime = this.state.currentProject.timeSpent + ? formatTime(this.state.currentProject.timeSpent) + : ''; return (

{this.state.currentProject.name || '...'}

+

Time spent: {projectTime}

item._id !== action.id); return projectsList; } + case ADD_TIMELOG: { + const projectList = state.map(item => { + if (item._id === action.data.project._id) { + item.timeSpent += action.seconds; // eslint-disable-line no-param-reassign + } + return item; + }); + return projectList; + } default: return state; } diff --git a/controllers/timelogController.js b/controllers/timelogController.js index 83f0bda..6d8b6f5 100644 --- a/controllers/timelogController.js +++ b/controllers/timelogController.js @@ -1,6 +1,7 @@ const mongoose = require('mongoose'); const Timelog = mongoose.model('Timelog'); +const Project = mongoose.model('Project'); const Task = mongoose.model('Task'); exports.addTime = async (req, res) => { @@ -10,8 +11,17 @@ exports.addTime = async (req, res) => { task.timeSpent += req.body.seconds; // eslint-disable-line no-param-reassign task.save(); }); - const [timelog, task] = await Promise.all([timelogPromise, taskPromise]); - res.json({ timelog, task }); + const projectPromise = Project.findById(req.body.project, (err, project) => { + project.timeSpent += req.body.seconds; // eslint-disable-line no-param-reassign + project.save(); + }); + + const [timelog, task, project] = await Promise.all([ + timelogPromise, + taskPromise, + projectPromise, + ]); + res.json({ timelog, task, project }); }; exports.getLogs = async (req, res) => { diff --git a/models/Project.js b/models/Project.js index c8ace12..8e7b699 100644 --- a/models/Project.js +++ b/models/Project.js @@ -13,6 +13,10 @@ const projectSchema = new mongoose.Schema({ ref: 'User', required: 'Project should belong to an author', }, + timeSpent: { + type: Number, + default: 0, + }, }); module.exports = mongoose.model('Project', projectSchema);