Skip to content

Commit

Permalink
Added student list of class
Browse files Browse the repository at this point in the history
  • Loading branch information
thebaodev committed May 3, 2019
1 parent 22ee702 commit 986d387
Show file tree
Hide file tree
Showing 16 changed files with 453 additions and 236 deletions.
529 changes: 307 additions & 222 deletions .idea/workspace.xml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions client/src/app/actions/students.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import axios from 'axios';
import { FETCH_STUDENTS } from '../constants/actionTypes';

export const fetchStudents = classId => {
return async dispatch => {
const res = await axios.get(`/api/classes/${classId}`);
dispatch({ type: FETCH_STUDENTS, payload: res.data });
};
};
1 change: 1 addition & 0 deletions client/src/app/constants/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const GET_ERRORS = 'GET_ERRORS';
export const SET_CURRENT_USER = 'SET_CURRENT_USER';

export const FETCH_CLASSES = 'FETCH_CLASSES';
export const FETCH_STUDENTS = 'FETCH_STUDENTS';
4 changes: 2 additions & 2 deletions client/src/app/helpers/formatTime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getFormatedTime = dateTime => {
const getFormattedTime = dateTime => {
return new Date(dateTime).toDateString();
};
export default getFormatedTime;
export default getFormattedTime;
8 changes: 5 additions & 3 deletions client/src/app/layout/components/Router.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Home from '../../routes/home/Home';
import Login from '../../routes/login/Login';
import Register from '../../routes/register/Register';
import Home from 'app/routes/home/Home';
import Login from 'app/routes/login/Login';
import Register from 'app/routes/register/Register';
import Class from 'app/routes/class/Class';
const Router = () => (
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/login" component={Login} />
<Route exact path="/register" component={Register} />
<Route exact path="/class/:classId" component={Class} />
</Switch>
);
export default Router;
2 changes: 1 addition & 1 deletion client/src/app/reducers/classesReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FETCH_CLASSES } from '../constants/actionTypes';
export default function(state = [], action) {
switch (action.type) {
case FETCH_CLASSES:
return action.payload || [];
return action.payload.reverse() || [];
default:
return state;
}
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { combineReducers } from 'redux';
import errorReducer from './errorReducer';
import authReducer from './authReducer';
import classesReducer from './classesReducer';
import studentsReducer from './studentsReducer';

export default combineReducers({
errors: errorReducer,
auth: authReducer,
classes: classesReducer,
students: studentsReducer,
});
10 changes: 10 additions & 0 deletions client/src/app/reducers/studentsReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FETCH_STUDENTS } from '../constants/actionTypes';

export default function(state = [], action) {
switch (action.type) {
case FETCH_STUDENTS:
return action.payload.reverse() || [];
default:
return state;
}
}
22 changes: 22 additions & 0 deletions client/src/app/routes/class/Class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { Component } from 'react';
import StudentList from './containers/StudentList';
class Class extends Component {
render() {
debugger;
return (
<div className="class-page">
<section className="hero is-primary">
<div className="hero-body">
<div className="container">
<h1 className="title">Primary title</h1>
<h2 className="subtitle">Primary subtitle</h2>
</div>
</div>
</section>
<StudentList classId={this.props.match.params.classId} />
</div>
);
}
}

export default Class;
27 changes: 27 additions & 0 deletions client/src/app/routes/class/components/StudentItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';

const StudentItem = ({ fullName, email }) => {
return (
<div className="column is-half">
<div className="box u-hover-effect ">
<article className="media">
<div className="media-content">
<div className="content">
<p>
<span className="tag is-light is-medium">
<strong className="is-white">{fullName}</strong>{' '}
</span>
<br />

<strong>Email: </strong>
{email}
</p>
</div>
</div>
</article>
</div>
</div>
);
};

export default StudentItem;
36 changes: 36 additions & 0 deletions client/src/app/routes/class/containers/StudentList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Component } from 'react';
import StudentItem from '../components/StudentItem';
import { connect } from 'react-redux';
import { fetchStudents } from 'app/actions/students';

class StudentList extends Component {
constructor(props) {
super(props);
this.state = {};
}

componentDidMount() {
this.props.fetchStudents(this.props.classId);
}
renderList(students) {
return students.map(({ _id, fullName, email }) => {
return <StudentItem key={_id} fullName={fullName} email={email} />;
});
}
render() {
return (
<section className="section">
<div className="container">
<div className="columns is-multiline is-desktop ">{this.renderList(this.props.students)}</div>
</div>
</section>
);
}
}
const mapStateToProps = state => ({
students: state.students,
});
export default connect(
mapStateToProps,
{ fetchStudents },
)(StudentList);
11 changes: 7 additions & 4 deletions client/src/app/routes/home/components/ClassItem.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react';
import getFormatedTime from 'app/helpers/formatTime';
import { withRouter } from 'react-router-dom';

const ClassItem = ({ name, description, course, startedTime, endedTime, updatedAt }) => {
const ClassItem = ({ _id, name, description, course, startedTime, endedTime, updatedAt, history }) => {
const onClassItemClicked = () => {
history.push(`class/${_id}`);
};
return (
<div className="column is-one-quarter">
<div className="box">
<div className="box u-hover-effect " onClick={() => onClassItemClicked()}>
<article className="media">
<div className="media-content">
<div className="content">
Expand All @@ -31,11 +35,10 @@ const ClassItem = ({ name, description, course, startedTime, endedTime, updatedA
</p>
</div>
</div>
<div className="media-right" />
</article>
</div>
</div>
);
};

export default ClassItem;
export default withRouter(ClassItem);
4 changes: 2 additions & 2 deletions client/src/app/routes/home/containers/ClassList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class ClassList extends Component {
this.props.fetchClasses();
}
renderList(classes) {
debugger;
return classes.reverse().map(({ _id, name, description, course, startedTime, endedTime, updatedAt }) => {
return classes.map(({ _id, name, description, course, startedTime, endedTime, updatedAt }) => {
return (
<ClassItem
key={_id}
_id={_id}
name={name}
description={description}
course={course}
Expand Down
7 changes: 7 additions & 0 deletions client/src/styles/abstracts/_utils.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@
.u-margin-top-big {
margin-top: 8rem !important;
}
.u-hover-effect {
cursor: pointer;
transition: all .2s;
&:hover {
transform: scale(1.05);
}
}
6 changes: 6 additions & 0 deletions client/src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -26814,3 +26814,9 @@ body {

.u-margin-top-big {
margin-top: 8rem !important; }

.u-hover-effect {
cursor: pointer;
transition: all .2s; }
.u-hover-effect:hover {
transform: scale(1.05); }
11 changes: 9 additions & 2 deletions routes/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ router.get('/', async (req, res) => {
}
});
router.get('/:classId/', async (req, res) => {
let classId = new mongoose.Types.ObjectId(req.params.classId);
try {
let classId = new mongoose.Types.ObjectId(req.params.classId);
await AccessCode.find({
class: classId,
})
.populate('user')
.exec((err, accessCodes) => {
if (err) return res.status(422).json(err);
res.json(accessCodes);
let users = accessCodes
.map(accessCode => {
return accessCode.user;
})
.filter(user => {
return user !== undefined;
});
res.json(users);
});
} catch (err) {
res.status(422).json(err);
Expand Down

0 comments on commit 986d387

Please sign in to comment.