RestStudentContextTray.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import StudentContextTray from "jsx/context_cards/StudentContextTray";
  4. import StudentCardStore from "jsx/context_cards/StudentCardStore";
  5. export default class RestStudentContextTray extends React.Component {
  6. static propTypes = {
  7. studentId: PropTypes.string.isRequired,
  8. courseId: PropTypes.string.isRequired
  9. };
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. loading: true
  14. };
  15. this.store = new StudentCardStore(
  16. this.props.studentId,
  17. this.props.courseId
  18. );
  19. this.store.load();
  20. this.store.onChange = () =>
  21. this.setState(this.mungeState(this.store.getState()));
  22. }
  23. componentWillUnmount() {
  24. this.store.onChange = null;
  25. }
  26. mungeState(state) {
  27. const {
  28. loading,
  29. course,
  30. user,
  31. submissions,
  32. permissions,
  33. analytics
  34. } = state;
  35. const sectionsById = course.sections.reduce((obj, section) => {
  36. obj[section.id] = section;
  37. return obj;
  38. }, {});
  39. return {
  40. loading: loading,
  41. course: {
  42. _id: course.id,
  43. submissionsConnection: {
  44. edges: submissions.map(submission => ({
  45. submission: {
  46. user: { _id: submission.user_id },
  47. ...submission
  48. }
  49. }))
  50. },
  51. permissions: permissions,
  52. ...course
  53. },
  54. user: {
  55. ...user,
  56. _id: user.id,
  57. analytics: analytics
  58. ? {
  59. page_views: {
  60. total: analytics.page_views,
  61. max: analytics.max_page_views,
  62. level: analytics.page_views_level
  63. },
  64. participations: {
  65. total: analytics.participations,
  66. max: analytics.max_participations,
  67. level: analytics.participations_level
  68. },
  69. tardiness_breakdown: analytics.tardiness_breakdown
  70. }
  71. : null,
  72. enrollments: user.enrollments.map(e => ({
  73. last_activity_at: e.last_activity_at,
  74. section: sectionsById[e.course_section_id],
  75. grades: e.grades
  76. }))
  77. }
  78. };
  79. }
  80. render() {
  81. return <StudentContextTray {...this.props} data={this.state} />;
  82. }
  83. }