Skip to content

Commit

Permalink
backend for volunteer history, added aggregation stats to frontend (#145
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Waschmid authored May 12, 2019
1 parent b83e29c commit e044b01
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 19 deletions.
43 changes: 42 additions & 1 deletion packages/api/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,46 @@ deleteEventTemplate = function(req, res) {
})
}

getVolunteerHistory = async function(req, res) {
try {
let response_data = []
client = this.dbClient
collection = client.db('events-form').collection('events')
let cursor = collection.aggregate([
{$match: {'categories.submissions.volunteer_email': req.body.email}},
{$unwind: '$categories'},
{$match: {'categories.submissions.volunteer_email': req.body.email}},
{$unwind: '$categories.submissions'},
{$match: {'categories.submissions.volunteer_email': req.body.email}}
])

while(await cursor.hasNext()) {
let doc = await cursor.next()
response_data.push({
date: doc.date,
name: doc.categories.submissions.volunteer_name,
email: doc.categories.submissions.volunteer_email,
type: doc.categories.name,
desc: doc.categories.submissions.description,
servings: doc.categories.submissions.servings,
vegan: doc.categories.submissions.vegan,
vegetarian: doc.categories.submissions.vegetarian,
gluten_free: doc.categories.submissions.gluten_free
})
}

res.send({
sub_info: response_data,
status: 'SUCCESS'
})
} catch (err) {
console.log('Error retrieving volunteer history', err)
res.send({
status: 'FAILURE'
})
}
}

module.exports.addPhotos = addPhotos
module.exports.removePhotos = removePhotos
module.exports.removeImagesFromFrontPage = removeImagesFromFrontPage
Expand All @@ -726,4 +766,5 @@ module.exports.editEventTemplate = editEventTemplate
module.exports.getEventTemplate = getEventTemplate
module.exports.deleteEventTemplate = deleteEventTemplate
module.exports.editedStory = editedStory
module.exports.getStoryCount = getStoryCount
module.exports.getStoryCount = getStoryCount
module.exports.getVolunteerHistory = getVolunteerHistory
3 changes: 2 additions & 1 deletion packages/api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ app.get('/api/getEventTemplate', ensureAuthenticated, adminHandlers.getEventTemp
app.post('/api/editEventTemplate', ensureAuthenticated, adminHandlers.editEventTemplate.bind({dbClient: client}))
app.post('/api/deleteEventTemplate', ensureAuthenticated, adminHandlers.deleteEventTemplate.bind({dbClient: client}))
app.post('/api/editedStory', ensureAuthenticated, adminHandlers.editedStory.bind({dbClient: client}))
app.get('/api/storiesCount', ensureAuthenticated, adminHandlers.getStoryCount.bind({dbClient: client}))
app.get('/api/storiesCount', ensureAuthenticated, adminHandlers.getStoryCount.bind({dbClient: client}))
app.post('/api/volunteerHistory', ensureAuthenticated, adminHandlers.getVolunteerHistory.bind({dbClient: client}))
2 changes: 1 addition & 1 deletion packages/ui/src/Components/Dashboard/VolunteerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class VolunteerList extends Component {
as={Link}
to="/VolunteerSubmissions"
onClick={() => {
this.props.updateActiveEmail(item.name)
this.props.updateActiveEmail(item.email)
}}
className="volunteer"
>
Expand Down
124 changes: 110 additions & 14 deletions packages/ui/src/Components/Dashboard/VolunteerSubmissions.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
import React, { Component } from 'react'
import { Header, Icon, Table } from 'semantic-ui-react'
import Axios from 'axios'
import moment from 'moment'

class VolunteerSubmissions extends Component {
constructor(props) {
super(props)
this.state = {
submissions: [
{
date: '06-10-19',
name: 'Pablo Escobar',
email: '[email protected]',
type: 'Main',
desc: 'Pizza',
servings: 100,
vegan: false,
vegetarian: false,
gluten_free: true
}
]
submissions: []
}
}

componentDidMount() {
Axios.post('/api/volunteerHistory', {email: this.props.email})
.then(res => {
let tempSubs = res.data.sub_info

tempSubs.forEach(e => {
e.sortDate = moment(e.date, 'MM-DD-YY')
})

tempSubs.sort((older, newer) => {
if(moment(older.sortDate).isBefore(moment(newer.sortDate))) {
return 1
} else {
return -1
}
})

this.setState({
submissions: tempSubs
})
}).catch(err => {
console.log('Error retrieving volunteer history', err)
})
}

renderIcon = value => {
if (value) {
return <Icon color="green" name="checkmark" size="small" />
Expand All @@ -30,9 +45,89 @@ class VolunteerSubmissions extends Component {
}

render() {
let stats = {
name: null,
events: 0,
categories: [],
avg_servings: 0,
vegan_pct: 0.0,
vegetarian_pct: 0.0,
gf_pct: 0.0,
counts: {}
}

this.state.submissions.forEach(e => {
if(!stats.name) {
stats.name = e.name
}
stats.events++
stats.avg_servings += e.servings
if(e.vegan) {
stats.vegan_pct++
}
if(e.vegetarian) {
stats.vegetarian_pct++
}
if(e.gluten_free) {
stats.gf_pct++
}
if(!stats.categories.includes(e.type)) {
stats.categories.push(e.type)
stats.counts[e.type] = 1
} else {
stats.counts[e.type]++
}
})

stats.avg_servings = stats.avg_servings / this.state.submissions.length
stats.vegan_pct = 100 * (stats.vegan_pct / this.state.submissions.length)
stats.vegetarian_pct = 100* (stats.vegetarian_pct / this.state.submissions.length)
stats.gf_pct = 100 * (stats.gf_pct / this.state.submissions.length)

stats.categories.sort((a, b) => {
return stats.counts[b] - stats.counts[a]
})


return (
<div>
<Header as="h3">Pablo Escobar</Header>
<Header as="h2">{stats.name}</Header>
<div>
<Header as="h3">Stats</Header>
<Table celled textAlign={'center'} selectable>
<Table.Header>
<Table.Row>
<Table.HeaderCell>Total Events</Table.HeaderCell>
<Table.HeaderCell>Top 3 Categories</Table.HeaderCell>
<Table.HeaderCell>Avgerage Servings</Table.HeaderCell>
<Table.HeaderCell>% Vegan</Table.HeaderCell>
<Table.HeaderCell>% Vegetarian</Table.HeaderCell>
<Table.HeaderCell>% Gluten-Free</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{stats.events > 0 &&
<Table.Row>
<Table.Cell>{stats.events}</Table.Cell>
<Table.Cell>
<ol>
{stats.categories[0] && <li>{stats.categories[0]}</li>}
{stats.categories[1] && <li>{stats.categories[1]}</li>}
{stats.categories[2] && <li>{stats.categories[2]}</li>}
</ol>
</Table.Cell>
<Table.Cell>{stats.avg_servings}</Table.Cell>
<Table.Cell>{stats.vegan_pct}</Table.Cell>
<Table.Cell>{stats.vegetarian_pct}</Table.Cell>
<Table.Cell>{stats.gf_pct}</Table.Cell>
</Table.Row>
}
</Table.Body>
</Table>
</div>
<hr></hr>
<div>
<Header as="h3">Signup History</Header>
<Table celled textAlign={'center'} selectable>
<Table.Header>
<Table.Row>
Expand Down Expand Up @@ -68,6 +163,7 @@ class VolunteerSubmissions extends Component {
))}
</Table.Body>
</Table>
</div>
</div>
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/Components/Stylesheets/EventTemplate.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.input-wrapper span{
visibility: hidden;
/* visibility: hidden; */
color: #008000;
}

.input-wrapper.has-error span{
visibility: visible;
/* visibility: visible; */
color: #FF0000;
}

Expand Down

0 comments on commit e044b01

Please sign in to comment.