Skip to content

Commit

Permalink
Add textbook selector (#994)
Browse files Browse the repository at this point in the history
* Add textbook selector for sidebar

* update course modal body to use textbook state shape
  • Loading branch information
felixzhuologist authored and noahpresler committed Aug 5, 2017
1 parent 82a2090 commit af47512
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
9 changes: 1 addition & 8 deletions courses/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,7 @@ def get_reactions(self, course):

def get_textbooks(self, course):
sections = course.section_set.filter(semester=self.context['semester'])
all_textbooks = (tb for section in sections for tb in section.get_textbooks())
unique_textbooks = []
seen_textbooks = set()
for tb in all_textbooks:
if tb['isbn'] not in seen_textbooks:
unique_textbooks.append(tb)
seen_textbooks.add(tb['isbn'])
return unique_textbooks
return {section.meeting_section: section.get_textbooks() for section in sections}

def get_regexed_courses(self, course):
"""
Expand Down
4 changes: 4 additions & 0 deletions static/js/redux/reducers/entities_reducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import merge from 'lodash/merge';
import uniq from 'lodash/uniq';
import flatMap from 'lodash/flatMap';

// TODO: garbage collect (e.g. clear when changing semesters)
const entities = (state = {}, action) => {
Expand Down Expand Up @@ -55,6 +56,9 @@ export const getSectionTypeToSections = (denormCourse) => {
return sectionTypeToSections;
};

export const getTextbooksFromCourse = (course) =>
flatMap(Object.keys(course.textbooks), sectionCode => course.textbooks[sectionCode]);

// TIMETABLE SELECTORS
// SLOT SELECTORS
export const getDenormSlot = (state, slot) => ({
Expand Down
7 changes: 5 additions & 2 deletions static/js/redux/ui/modals/course_modal_body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ GNU General Public License for more details.
import PropTypes from 'prop-types';
import React from 'react';
import isEmpty from 'lodash/isEmpty';
import flatMap from 'lodash/flatMap';
import Reaction from '../reaction';
import REACTION_MAP from '../../constants/reactions';
import MasterSlot from '../master_slot';
Expand Down Expand Up @@ -303,12 +304,14 @@ class CourseModalBody extends React.Component {
</div>
</div>);
}
const textbooksDisplay = !textbooks || textbooks.length === 0 ? null :

const textbooksArray = flatMap(Object.keys(textbooks), sectionCode => textbooks[sectionCode]);
const textbooksDisplay = !textbooksArray || textbooksArray.length === 0 ? null :
(<div className="modal-module">
<h3 className="modal-module-header">Textbooks</h3>
<div className="modal-textbook-list">
{
textbooks.map(t => <Textbook key={t.isbn} tb={t} />)
textbooksArray.map(t => <Textbook key={t.isbn} tb={t} />)
}
</div>
</div>);
Expand Down
22 changes: 5 additions & 17 deletions static/js/redux/ui/side_bar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import React from 'react';
import classNames from 'classnames';
import ClickOutHandler from 'react-onclickout';
import uniqBy from 'lodash/uniqBy';
import flatMap from 'lodash/flatMap';
import MasterSlot from './master_slot';
import TimetableNameInputContainer from './containers/timetable_name_input_container';
import CreditTickerContainer from './containers/credit_ticker_container';
import Textbook from './textbook';
import * as SemesterlyPropTypes from '../constants/semesterlyPropTypes';
import { getNextAvailableColour } from '../util';
import { getTextbooksFromCourse } from '../reducers/entities_reducer';

class SideBar extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -246,14 +248,8 @@ SideBar.propTypes = {
export default SideBar;

export const TextbookList = ({ courses }) => {
let tbs = [];
for (let i = 0; i < courses.length; i++) {
if (courses[i].textbooks !== undefined && Object.keys(courses[i].textbooks).length > 0) {
for (let j = 0; j < courses[i].enrolled_sections.length; j++) {
tbs = tbs.concat(courses[i].textbooks[courses[i].enrolled_sections[j]]);
}
}
}
let tbs = flatMap(courses, getTextbooksFromCourse);

const img = (!isNaN(parseInt(courses, 0)) && (courses.length >= 5)) ? null :
<img src="/static/img/emptystates/textbooks.png" alt="No textbooks found." />;
if (tbs.length === 0) {
Expand All @@ -272,15 +268,7 @@ export const TextbookList = ({ courses }) => {
);
};

TextbookList.defaultProps = {
courses: null,
};

TextbookList.propTypes = {
courses: PropTypes.arrayOf(PropTypes.shape({
textbooks: PropTypes.arrayOf(PropTypes.shape({
isbn: PropTypes.string,
})),
})),
courses:PropTypes.arrayOf(SemesterlyPropTypes.denormalizedCourse).isRequired,
};

0 comments on commit af47512

Please sign in to comment.