added sort functionality; currently bugged
authorAlex-Laptop <[email protected]>
Sat, 23 Mar 2019 08:23:53 +0000 (01:23 -0700)
committerAlex-Laptop <[email protected]>
Sat, 23 Mar 2019 08:23:53 +0000 (01:23 -0700)
src/components/manage/manage.component.js
src/components/manage/manage.js
src/reducers/view.js

index 4c1f27960a8929d3fa567f46837659ab2df61a7d..3281bc0c19ecc7ac7490df50fe04ad2f8077036c 100644 (file)
@@ -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,{
index a29d3d59c455f587e16a350793392b4b3a998589..0741a1b9c4dab75e5b1fa9de6ba393a49753bcb1 100644 (file)
@@ -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,
index f7b382114549e6f5d718562d38588bb46c42f4ed..0c16fd2aac6cab9c2882697aa9a69a6088abbe74 100644 (file)
@@ -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;i<newSortKey.length;i++) {
+                                       console.log(a[newSortKey[i]],b[newSortKey[i]]);
+                                       if(a[newSortKey[i]]<b[newSortKey[i]]) {
+                                               return (newSortOrder[i]=="asc")?-1:1;
+                                       } else if(a[newSortKey[i]]>b[newSortKey[i]]) {
+                                               return (newSortOrder[i]=="asc")?1:-1;
+                                       }
+                               }
+                               return 0;
+                       });
+                       return newStateAfterSort;
                default:
                        return state;
        }