Advertisement
Guest User

Refactored code

a guest
Oct 21st, 2019
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2.  
  3. import SubjectCell from './SubjectCell';
  4. import "../style/SubjectSelectionTable.css";
  5.  
  6. import { dayNames, periodNames } from "../objects/Period";
  7.  
  8. export default class SubjectSelectionTable extends Component {
  9.     renderPeriod(period, periodName, subjects) {
  10.         return (
  11.             <div key={period} className="subjectsRow scheduleRow">
  12.                 <div className="periodName">
  13.                     {periodName}
  14.                 </div>
  15.        
  16.                 <div className="subjectsSubRow">
  17.                     {
  18.                         subjects.map(subject => {
  19.                             return <SubjectCell subject={subject} bold={subject.id === this.props.selectedSubject} key={subject.id}/>;
  20.                         })
  21.                     }
  22.                 </div>
  23.             </div>
  24.         );
  25.     }
  26.  
  27.     renderDay(day, dayName, subjects) {
  28.         return (
  29.             <React.Fragment key={day}>
  30.                 <div className="dayName scheduleRow">
  31.                     {dayName}
  32.                 </div>
  33.  
  34.                 {
  35.                     periodNames.map((periodName, period) => {
  36.                         var periodSubjects = subjects.filter(subject => subject.period.period === period);
  37.                         if (periodSubjects.length) {
  38.                             return this.renderPeriod(period, periodName, periodSubjects);
  39.                         }
  40.                         return null;
  41.                     })
  42.                 }
  43.             </React.Fragment>
  44.         );
  45.     }
  46.  
  47.     render() {
  48.         return (<>
  49.             <div className="subjectTable">
  50.                 {
  51.                     dayNames.map((dayName, day) => {
  52.                         var daySubjects = this.props.subjects.filter(subject => subject.period.day === day);
  53.                         if (daySubjects.length) {
  54.                             return this.renderDay(day, dayName, daySubjects);
  55.                         }
  56.                         return null;
  57.                     })
  58.                 }
  59.             </div>
  60.         </>);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement