nupanick

re-refactored code for BlackFeather97

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