Remove remove all unneeded sorting

This commit is contained in:
Rick Reilly
2019-02-04 10:39:49 -05:00
parent 0ef8e773cc
commit d4421d47fc
7 changed files with 10 additions and 114 deletions

View File

@@ -363,9 +363,6 @@ export default class Gradebook extends React.Component {
this.props.grades,
this.props.areGradesFrozen,
)}
tableSortable
defaultSortDirection="asc"
defaultSortedColumn="username"
rowHeaderColumnKey="username"
/>
</div>
@@ -422,6 +419,7 @@ Gradebook.defaultProps = {
},
selectedCohort: null,
selectedTrack: null,
selectedAssignmentType: 'All',
showSpinner: false,
tracks: [],
};
@@ -453,8 +451,6 @@ Gradebook.propTypes = {
headings: PropTypes.arrayOf(PropTypes.shape({
label: PropTypes.string,
key: PropTypes.string,
columnSortable: PropTypes.bool,
onSort: PropTypes.func,
})).isRequired,
history: PropTypes.shape({
push: PropTypes.func,
@@ -468,7 +464,7 @@ Gradebook.propTypes = {
}),
}),
searchForUser: PropTypes.func.isRequired,
selectedAssignmentType: PropTypes.string.isRequired,
selectedAssignmentType: PropTypes.string,
selectedCohort: PropTypes.shape({
name: PropTypes.string,
}),

View File

@@ -7,28 +7,15 @@ import {
GRADE_UPDATE_SUCCESS,
GRADE_UPDATE_FAILURE,
TOGGLE_GRADE_FORMAT,
SORT_GRADES,
FILTER_COLUMNS,
UPDATE_BANNER,
} from '../constants/actionTypes/grades';
import LmsApiService from '../services/LmsApiService';
import store from '../store';
import { headingMapper, gradeSortMap, sortAlphaAsc } from './utils';
import { headingMapper, sortAlphaAsc } from './utils';
import apiClient from '../apiClient';
const defaultAssignmentFilter = 'All';
const sortGrades = (columnName, direction) => {
const sortFn = gradeSortMap(columnName, direction);
const { results } = store.getState().grades;
results.sort(sortFn);
/* have to make a copy of results or React wont know there was
* a change and wont trigger a re-render
*/
return ({ type: SORT_GRADES, results: [...results] });
};
const startedFetchingGrades = () => ({ type: STARTED_FETCHING_GRADES });
const finishedFetchingGrades = () => ({ type: FINISHED_FETCHING_GRADES });
const errorFetchingGrades = () => ({ type: ERROR_FETCHING_GRADES });
@@ -62,7 +49,7 @@ const toggleGradeFormat = formatType => ({ type: TOGGLE_GRADE_FORMAT, formatType
const filterColumns = (filterType, exampleUser) => (
dispatch => dispatch({
type: FILTER_COLUMNS,
headings: headingMapper(filterType)(dispatch, exampleUser),
headings: headingMapper(filterType)(exampleUser),
})
);
@@ -79,7 +66,7 @@ const fetchGrades = (courseId, cohort, track, assignmentType, showSuccess) => (
cohort,
track,
assignmentType,
headingMapper(assignmentType || defaultAssignmentFilter)(dispatch, data.results[0]),
headingMapper(assignmentType || defaultAssignmentFilter)(data.results[0]),
data.previous,
data.next,
courseId,
@@ -111,7 +98,7 @@ const fetchMatchingUserGrades = (
cohort,
track,
assignmentType,
headingMapper(assignmentType || defaultAssignmentFilter)(dispatch, data.results[0]),
headingMapper(assignmentType || defaultAssignmentFilter)(data.results[0]),
data.previous,
data.next,
courseId,
@@ -136,7 +123,7 @@ const fetchPrevNextGrades = (endpoint, courseId, cohort, track, assignmentType)
cohort,
track,
assignmentType,
headingMapper(assignmentType || defaultAssignmentFilter)(dispatch, data.results[0]),
headingMapper(assignmentType || defaultAssignmentFilter)(data.results[0]),
data.previous,
data.next,
courseId,
@@ -184,7 +171,6 @@ export {
gradeUpdateFailure,
updateGrades,
toggleGradeFormat,
sortGrades,
filterColumns,
updateBanner,
};

View File

@@ -98,16 +98,12 @@ describe('actions', () => {
assignmentType: expectedAssignmentType,
headings: [
{
columnSortable: true,
key: 'username',
label: 'Username',
onSort: expect.anything(),
},
{
columnSortable: true,
key: 'total',
label: 'Total',
onSort: expect.anything(),
},
],
prev: responseData.previous,

View File

@@ -1,5 +1,3 @@
import { sortGrades } from './grades';
const sortAlphaAsc = (gradeRowA, gradeRowB) => {
const a = gradeRowA.username.toUpperCase();
const b = gradeRowB.username.toUpperCase();
@@ -12,62 +10,12 @@ const sortAlphaAsc = (gradeRowA, gradeRowB) => {
return 0;
};
const sortAlphaDesc = (gradeRowA, gradeRowB) => {
const a = gradeRowA.username.toUpperCase();
const b = gradeRowB.username.toUpperCase();
if (a < b) {
return 1;
}
if (a > b) {
return -1;
}
return 0;
};
const sortNumerically = (colKey, direction) => {
function getPercents(gradeRowA, gradeRowB) {
if (colKey !== 'total') {
return {
a: gradeRowA.section_breakdown.find(x => x.label === colKey).percent,
b: gradeRowB.section_breakdown.find(x => x.label === colKey).percent,
};
}
return {
a: gradeRowA.percent,
b: gradeRowB.percent,
};
}
function sortNumAsc(gradeRowA, gradeRowB) {
const { a, b } = getPercents(gradeRowA, gradeRowB);
return a - b;
}
function sortNumDesc(gradeRowA, gradeRowB) {
const { a, b } = getPercents(gradeRowA, gradeRowB);
return b - a;
}
return direction === 'desc' ? sortNumDesc : sortNumAsc;
};
function gradeSortMap(columnName, direction) {
if (columnName === 'username' && direction === 'desc') {
return sortAlphaDesc;
} else if (columnName === 'username') {
return sortAlphaAsc;
}
return sortNumerically(columnName, direction);
}
const headingMapper = (filterKey) => {
function all(dispatch, entry) {
function all(entry) {
if (entry) {
const results = [{
label: 'Username',
key: 'username',
columnSortable: true,
onSort: (direction) => { dispatch(sortGrades('username', direction)); },
}];
const assignmentHeadings = entry.section_breakdown
@@ -75,15 +23,11 @@ const headingMapper = (filterKey) => {
.map(s => ({
label: s.label,
key: s.label,
columnSortable: true,
onSort: direction => dispatch(sortGrades(s.label, direction)),
}));
const totals = [{
label: 'Total',
key: 'total',
columnSortable: true,
onSort: direction => dispatch(sortGrades('total', direction)),
}];
return results.concat(assignmentHeadings).concat(totals);
@@ -91,14 +35,12 @@ const headingMapper = (filterKey) => {
return [];
}
function some(dispatch, entry) {
function some(entry) {
if (!entry) return [];
const results = [{
label: 'Username',
key: 'username',
columnSortable: true,
onSort: (direction) => { dispatch(sortGrades('username', direction)); },
}];
const assignmentHeadings = entry.section_breakdown
@@ -106,15 +48,11 @@ const headingMapper = (filterKey) => {
.map(s => ({
label: s.label,
key: s.label,
columnSortable: false,
onSort: (direction) => { this.sortNumerically(s.label, direction); },
}));
const totals = [{
label: 'Total',
key: 'total',
columnSortable: true,
onSort: direction => dispatch(sortGrades('total', direction)),
}];
return results.concat(assignmentHeadings).concat(totals);
@@ -123,5 +61,5 @@ const headingMapper = (filterKey) => {
return filterKey === 'All' ? all : some;
};
export { headingMapper, gradeSortMap, sortAlphaAsc };
export { headingMapper, sortAlphaAsc };

View File

@@ -8,7 +8,6 @@ const GRADE_UPDATE_SUCCESS = 'GRADE_UPDATE_SUCCESS';
const GRADE_UPDATE_FAILURE = 'GRADE_UPDATE_FAILURE';
const TOGGLE_GRADE_FORMAT = 'TOGGLE_GRADE_FORMAT';
const SORT_GRADES = 'SORT_GRADES';
const FILTER_COLUMNS = 'FILTER_COLUMNS';
const UPDATE_BANNER = 'UPDATE_BANNER';
@@ -21,7 +20,6 @@ export {
GRADE_UPDATE_SUCCESS,
GRADE_UPDATE_FAILURE,
TOGGLE_GRADE_FORMAT,
SORT_GRADES,
FILTER_COLUMNS,
UPDATE_BANNER,
};

View File

@@ -5,7 +5,6 @@ import {
TOGGLE_GRADE_FORMAT,
FILTER_COLUMNS,
UPDATE_BANNER,
SORT_GRADES,
} from '../constants/actionTypes/grades';
const initialState = {
@@ -66,11 +65,6 @@ const grades = (state = initialState, action) => {
...state,
showSuccess: action.showSuccess,
};
case SORT_GRADES:
return {
...state,
results: action.results,
};
default:
return state;
}

View File

@@ -6,7 +6,6 @@ import {
TOGGLE_GRADE_FORMAT,
FILTER_COLUMNS,
UPDATE_BANNER,
SORT_GRADES,
} from '../constants/actionTypes/grades';
const initialState = {
@@ -164,17 +163,6 @@ describe('grades reducer', () => {
})).toEqual(expected);
});
it('updates sort grades state success', () => {
const expected = {
...initialState,
results: gradesData,
};
expect(grades(undefined, {
type: SORT_GRADES,
results: gradesData,
})).toEqual(expected);
});
it('updates fetch grades failure state', () => {
const expected = {
...initialState,