Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
timetracking now works for projects as well
Browse files Browse the repository at this point in the history
  • Loading branch information
askeroff committed Dec 5, 2017
1 parent 872d3a6 commit e72fd34
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions client/components/projects/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -100,11 +101,15 @@ class Project extends React.Component {
return <NotFound />;
}
const addLinkText = this.state.showForm ? 'Hide The Form' : 'New Task';
const projectTime = this.state.currentProject.timeSpent
? formatTime(this.state.currentProject.timeSpent)
: '';
return (
<Layout>
<h1 className="page-title">
{this.state.currentProject.name || '...'}
</h1>
<h3 className="page-title">Time spent: {projectTime}</h3>
<a
onClick={this.showAddForm}
href="#"
Expand Down
10 changes: 10 additions & 0 deletions client/reducers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
RENAME_PROJECT,
CLEAR_PROJECTS,
DELETE_PROJECT,
ADD_TIMELOG,
} from '../actions/actionTypes';

function projects(state = [], action) {
Expand All @@ -28,6 +29,15 @@ function projects(state = [], action) {
const projectsList = state.filter(item => 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;
}
Expand Down
14 changes: 12 additions & 2 deletions controllers/timelogController.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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) => {
Expand Down
4 changes: 4 additions & 0 deletions models/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit e72fd34

Please sign in to comment.