From: Alex-Laptop Date: Sat, 23 Mar 2019 08:23:53 +0000 (-0700) Subject: added sort functionality; currently bugged X-Git-Tag: v1.0.0~83 X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=165f5dd564a10e54666b664285e3b9d131e4c46c;p=workouts added sort functionality; currently bugged --- diff --git a/src/components/manage/manage.component.js b/src/components/manage/manage.component.js index 4c1f279..3281bc0 100644 --- a/src/components/manage/manage.component.js +++ b/src/components/manage/manage.component.js @@ -9,10 +9,14 @@ export default class Manage extends React.Component { constructor(props) { super(props); this.addNew = this.addNew.bind(this); + this.handleSort = this.handleSort.bind(this); } addNew() { this.props.newWorkout(); } + handleSort(key,event) { + this.props.sort(key,event.shiftKey); + } render() { const { workouts, @@ -29,7 +33,7 @@ export default class Manage extends React.Component { "description", "" ].map((x) => { - return createElement("th",{key:"head-"+x},x); + return createElement("th",{key:"head-"+x,onClick:this.handleSort.bind(this,x)},x); }); const rows = Object.keys(workouts).map((i) => { return createElement(manageRow,{ diff --git a/src/components/manage/manage.js b/src/components/manage/manage.js index a29d3d5..0741a1b 100644 --- a/src/components/manage/manage.js +++ b/src/components/manage/manage.js @@ -3,7 +3,7 @@ import {connect} from 'react-redux'; import Manage from './manage.component.js'; import {constants} from '../../constants.js'; -const {ADD_WORKOUT,CHANGE_ATTRIBUTE,CHANGE_WORKOUT_DESCRIPTION,CHANGE_WORKOUT_NAME,NEW_WORKOUT} = constants; +const {ADD_WORKOUT,CHANGE_ATTRIBUTE,CHANGE_WORKOUT_DESCRIPTION,CHANGE_WORKOUT_NAME,NEW_WORKOUT,SORT_VIEW} = constants; const mapStateToProps = (state) => { const {workouts} = state; @@ -44,6 +44,13 @@ const mapDispatchToProps = (dispatch) => { type:NEW_WORKOUT }); }, + sort:(key,shift) => { + dispatch({ + type:SORT_VIEW, + key, + shift + }); + }, toggleAttribute:(name,attr) => { dispatch({ type:CHANGE_ATTRIBUTE, diff --git a/src/reducers/view.js b/src/reducers/view.js index f7b3821..0c16fd2 100644 --- a/src/reducers/view.js +++ b/src/reducers/view.js @@ -11,8 +11,8 @@ const defaultState = { view:"manage", daysAgo:defaultDaysAgo, data:[], - sortKey:"last_done", - sortOrder:"desc" + sortKey:["last_done"], + sortOrder:["desc"] }; const generateDaysAgo = (workouts) => { @@ -39,19 +39,20 @@ const generateDaysAgo = (workouts) => { export default function view(state = defaultState,action) { const {CHANGE_VIEW,SORT_VIEW} = constants; - if((action.workouts == void(0))||(typeof action.workouts != "object" )) { - return state; - } - if(!Object.keys(action.workouts).every((x) => action.workouts[x] instanceof workout)) { - return state; - } switch(action.type) { case CHANGE_VIEW: + if((action.workouts == void(0))||(typeof action.workouts != "object" )) { + return state; + } + if(!Object.keys(action.workouts).every((x) => action.workouts[x] instanceof workout)) { + return state; + } if((action.view === void(0))||(action.view == state.view)) { return state; } switch(action.view) { case "manage": + console.log('manage'); const newStateManageView = { ...state, view:"manage" @@ -66,8 +67,10 @@ export default function view(state = defaultState,action) { }); } newStateManageView.daysAgo = generateDaysAgo(action.workouts); + console.log(JSON.stringify(newStateManageView.data)); return newStateManageView; case "recent": + console.log('recent'); const newStateRecentView = { ...state, view:"recent" @@ -82,25 +85,50 @@ export default function view(state = defaultState,action) { } } newStateRecentView.daysAgo = generateDaysAgo(action.workouts); + console.log(JSON.stringify(newStateRecentView.data)); return newStateRecentView; default: return state; } case SORT_VIEW: - /*if(action.key === void(0)) { - return state; - } - if(action.order === void(0)) { + if(action.key === void(0)) { return state; } - let newSortKey = [...state.sortKey]; - let newSortOrder = [...state.sortOrder]; - const sortIndex = state.sortKey.indexOf(action.key); - if(sortIndex>-1) { - newSortOrder[sortIndex] = (newSortOrder[sortIndex]=="asc")?"desc":"asc"; + const newStateAfterSort = {...state}; + let newSortKey = [...newStateAfterSort.sortKey]; + let newSortOrder = [...newStateAfterSort.sortOrder]; + if(action.shift === true) { + const index = newSortKey.indexOf(action.key); + if(index>-1) { + newSortOrder[index] = (newSortOrder[index]=="asc")?"desc":"asc"; + } else { + newSortKey.push(action.key); + newSortOrder.push("asc"); + } } else { - newSortkey*/ - return state; + if((newSortKey[0] !== void(0))&&(newSortKey[0]==action.key)) { + newSortOrder = (newSortOrder[0]=="asc")?["desc"]:["asc"]; + } else { + newSortOrder = ["asc"]; + } + newSortKey = [action.key]; + } + newStateAfterSort.sortKey = newSortKey; + newStateAfterSort.sortOrder = newSortOrder; + console.log(state.data); + console.log(newStateAfterSort.data); + newStateAfterSort.data.sort((a,b) => { + for(let i=0;ib[newSortKey[i]]) { + return (newSortOrder[i]=="asc")?1:-1; + } + } + return 0; + }); + return newStateAfterSort; default: return state; }