Skip to content

Commit

Permalink
add approve and reject buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Dwyer authored and Michael Dwyer committed Feb 26, 2019
1 parent 7defa99 commit 74679c9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
26 changes: 25 additions & 1 deletion controllers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,31 @@ module.exports = {
show,
searchPage,
searchResults,
addUser
addUser,
submit
}

function submit(req, res){
Project.findById(req.params.id)
.then(function(project){
if (project.approvals[0]){
res.redirect(`/projects/${req.params.id}`)
} else {
project.approvals.push(req.user.id);
if (JSON.stringify(project.approvals.sort()) === JSON.stringify(project.users.sort())) {
project.versions.push({content: req.body.content, approved: true});
project.approvals = [];
project.save(function(err){
res.redirect(`/projects/${req.params.id}`);
})
} else {
project.pendingVersion = {content: req.body.content, approved: false};
project.save(function(err){
res.redirect(`/projects/${req.params.id}`);
})
}
}
})
}

function addUser(req, res){
Expand Down
17 changes: 15 additions & 2 deletions data.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
exports.projects = [
{
name: 'Project A',
versions: [{content: 'aaaa', approved: true}, {content: 'bbbb', approved: false}, {content: 'ccccc', approved: true}],
versions: [{content: 'aaaa', approved: true}, {content: 'bbbb', approved: false}],
pendingVersion: {content: 'ddddd', approved: null}
},
{
name: 'Project B',
versions: [{content: 'xxxxx', approved: true}],
pendingVersion: null
}
];
];

exports.users = [
{
name: 'Mike Dwyer',
email: 'brianmichaeldwyer@gmail.com',
googleId: '105071105568043566196',
},
{
name: 'John Smith',
email: 'js5907658@gmail.com',
googleId: '103208053237588678337',
}
];
1 change: 1 addition & 0 deletions routes/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ router.get('/projects/new', projectsCtrl.new);
router.post('/projects/', projectsCtrl.create);
router.get('/projects/', projectsCtrl.index);
router.get('/projects/:id', projectsCtrl.show);
router.post('/projects/:id', projectsCtrl.submit);
router.get('/projects/:id/search', projectsCtrl.searchPage);
router.post('/projects/:id/search', projectsCtrl.searchResults);
router.post('/projects/:id/invite', projectsCtrl.addUser);
Expand Down
29 changes: 29 additions & 0 deletions seeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require('dotenv').config();
require('./config/database');
const Project = require('./models/project');
const User = require('./models/user');
const data = require('./data');

Project.deleteMany({})
Expand All @@ -15,6 +16,34 @@ Project.deleteMany({})
console.log(project);
});
})
.then(function(){
return User.deleteMany({});
})
.then(function(){
return User.create(data.users);
})
.then(function(users){
console.log(users);
})
.then(function(){
process.exit();
});

// Project.deleteMany({})
// .then(function(){
// User.deleteMany({});
// })
// .then(function(){
// Project.create(data.projects);
// })
// .then(function(){
// User.create(data.users);
// })
// .then(function(){
// process.exit;
// });

// Project.deleteMany({})
// .then(function(){
// process.exit;
// });
6 changes: 5 additions & 1 deletion views/projects/show-edit.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
<% } %>

<h2><%= project.name %></h2>
<textarea class="content-display white" cols="30" rows="10"> <%= project.getCurrentVersion().content %></textarea>
<form action="/projects/<%= project.id %>" method="POST">
<textarea class="content-display white" cols="30" rows="10" name="content"><%= project.getCurrentVersion().content %></textarea>
<button class="btn" type="submit">Submit Edit</button>
</form>


<h4>Current collaborators:</h4>
<ul>
Expand Down
14 changes: 10 additions & 4 deletions views/projects/show-lock.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
<p>Welcome to Tag</p>
<% } %>

<h2><%= project.name %></h2>
<textarea readonly class="content-display white" cols="30" rows="10"> <%= project.getCurrentVersion().content %></textarea>
<h2><%= project.name %> (needs approval)</h2>
<textarea readonly class="content-display white" cols="30" rows="10"><%= project.pendingVersion.content %></textarea>

<!-- below code activates approval button if current user is included in project but not in approvals -->
<% if (JSON.stringify(project.users).includes(JSON.stringify(user.id)) && !JSON.stringify(project.approvals).includes(JSON.stringify(user.id))) { %>
<a href="#" class="btn">Approve Edit</a>
<a href="#" class="btn red">Reject Edit</a>
<% } %>

<h4>Current collaborators:</h4>
<ul>
<% project.users.forEach(function(user){ %>
<% project.users.forEach(function(u){ %>
<li>
<%= user.name %>, <%= user.email %>
<%= u.name %>, <%= u.email %>
</li>
<% }); %>
</ul>
Expand Down

0 comments on commit 74679c9

Please sign in to comment.