Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
homework now done. decided to keep hideChecked variable in the local …
Browse files Browse the repository at this point in the history
…state as per reduxjs/redux#1287
  • Loading branch information
obedm503 committed Sep 3, 2017
1 parent 03d5944 commit 6b33827
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 95 deletions.
30 changes: 12 additions & 18 deletions src/handlers/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,18 @@ export const homework = {
return filter;
},
},
hidden: {
HOMEWORK_TOGGLE_HIDDEN( state: any[], { id }){
let copy = state.slice( 0 );
let i = copy.indexOf( id );
if( i > -1 ){
copy.splice(i, 1);
} else {
copy.push(id)
}
return copy;
homework: {
HOMEWORK_TOGGLE_CHECKED( state: any[], { id }){
return state.map( item => item.lsn_id === id
? { ...item, _checked: !item._checked }
: item
);
},
HOMEWORK_SET( state = [], { response }){
return response.homework.map( item => ({
...item,
_checked: !!state.find( el => item.lsn_id === el.lsn_id && el._checked )
}) ).reverse();
},
},
HOMEWORK_SET( state, { response }){
return {
...state,
// removes unnecessary id's, otherwise the app would accumulate id's for EVER!
hidden: state.hidden.filter( id => !!response.homework.find( item => item.lsn_id === id ) ),
homework: response.homework.slice(0).reverse(),
};
},
};
8 changes: 4 additions & 4 deletions src/pages/homework/homework.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
<ion-item
text-wrap
*ngFor="let item of homework"
[class.dark-gray]="item.lsn_date === store.today"
[@expand]="!( item._hidden && hideChecked )"
[class.dark-gray]="item.lsn_date === today"
[@expand]="!( item._checked && hideChecked )"
>
<ion-label>
{{ item.calc_date }}
Expand All @@ -47,8 +47,8 @@
</ion-label>
<ion-checkbox
color="primary"
[(ngModel)]="item._hidden"
(ngModelChange)="check(item)"
[checked]="item._checked"
(ionChange)="check(item)"
></ion-checkbox>
</ion-item>
</ion-list>
Expand Down
30 changes: 3 additions & 27 deletions src/pages/homework/homework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import { expand } from '../../components/animations';
animations: [ expand ]
})
export class Homework {
homework: Array<{calc_class: string, calc_date: string, lsn_date: string, lsn_hw: string, lsn_id: string}> = [];
classes: any[] = [];
filter: string = 'ALL_CLASSES';
filter: string;
hideChecked: boolean = true;
loading: Loading = this.loadingCtrl.create();

Expand All @@ -47,6 +46,7 @@ export class Homework {
homework: getHomework(state),
filter: getHomeworkFilter(state),
classes: getHomeworkClasses(state),
today: state.today,
}) )( this );

store.dispatch({ type: 'LOAD', key: 'homework' });
Expand All @@ -56,29 +56,6 @@ export class Homework {
log.warn(err);
}
}
// async get( refresh = false ){
// try {
// let hw = await this.store.get(
// 'HOMEWORK',
// ({ newData, oldData = { homework: [] } }) => ({
// ...newData,
// homework: newData.homework.map( item => {
// if( oldData.homework.find( el => item.lsn_id === el.lsn_id && el.checked ) ){
// item.checked = true;
// }
// return item;
// } )
// }),
// refresh,
// );
// // this.homework serves as a backup
// // this.filteredHw is presented in view
// this.filteredHw = this.homework = hw.homework.slice(0).reverse();
// } catch(err){
// this.log.warn(err);
// }
// }

popover(e){
const buttons = this.classes
.map( className => ({
Expand Down Expand Up @@ -110,9 +87,8 @@ export class Homework {
}).present();
}
check( item ){
store.dispatch({ type: 'HOMEWORK_TOGGLE_HIDDEN', id: item.lsn_id });
store.dispatch({ type: 'HOMEWORK_TOGGLE_CHECKED', id: item.lsn_id });
}

refresh( refresher: Refresher ){
store.dispatch({ type: 'LOAD', key: 'homework', refresh: true });
refresher.complete();
Expand Down
13 changes: 7 additions & 6 deletions src/providers/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ enum levels {
debug = 4
};

const level = levels.debug;
// const level = levels.none;

@Injectable()
export class Log {
// public level: number = levels.none;
public level: number = levels.debug;
debug(...rest){
if( this.level < levels.debug){ return; }
if( level < levels.debug){ return; }
console.debug(`DEBUG [StudentAccess]`, ...rest);
}
info(...rest){
if( this.level < levels.info){ return; }
if( level < levels.info){ return; }
console.info(`%cINFO [StudentAccess]`, 'color: blue', ...rest);
}
warn(...rest){
if( this.level < levels.warn){ return; }
if( level < levels.warn){ return; }
console.warn(`WARN [StudentAccess]`, ...rest);
}
error(...rest){
if( this.level < levels.error){ return; }
if( level < levels.error){ return; }
console.error(`ERROR [StudentAccess]`, ...rest);
}
}
Expand Down
12 changes: 1 addition & 11 deletions src/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,16 @@ export const getFilteredStaff = createSelector(
);

export const getRawHomework = state => state.homework.homework;
export const getHiddenHomework = state => state.homework.hidden;
export const getHomeworkFilter = state => state.homework.filter;

export const getFilteredHomework = createSelector(
export const getHomework = createSelector(
getRawHomework,
getHomeworkFilter,
( state = [], filter = 'ALL_CLASSES' ) => filter === 'ALL_CLASSES'
? state
: state.filter( item => item.calc_class === filter ),
);

export const getHomework = createSelector(
getFilteredHomework,
getHiddenHomework,
( homework = [], hidden = []) => homework.map( item => ({
...item,
_hidden: hidden.includes( item.lsn_id ),
})),
);

export const getHomeworkClasses = createSelector(
getRawHomework,
( homework = [] ) => homework
Expand Down
28 changes: 28 additions & 0 deletions src/store/create-connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import { bindActionCreators } from 'redux';

export const createConnect = (store, unSubName) => (mapState, mapDispatch?) => target => {
if( typeof mapDispatch === 'function' ){
Object.assign( target, mapDispatch( store.dispatch ) );
} else if( typeof mapDispatch === 'object' ){
Object.assign( target, bindActionCreators( mapDispatch, store.dispatch ) );
}

const sub = store.state
.map(mapState)
.distinctUntilChanged()
.subscribe(state => Object.assign(target, state));

if( unSubName ){
const original: () => void = target[unSubName];
target[unSubName] = function(...args){
sub.unsubscribe();
if( original ){
return original.apply(this, args);
}
};
}

return sub;
};
33 changes: 5 additions & 28 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs';
import { createConnect } from './create-connect';
import { createStore } from 'rstate';
import handlers from '../handlers';
import { applyMiddleware, compose, bindActionCreators } from 'redux';

import { applyMiddleware, compose } from 'redux';
import logger from 'redux-logger';

import handlers from '../handlers';
import devToolsEnhancer from './dev-tools';
import { initialState } from './initial';

Expand All @@ -26,27 +26,4 @@ window['store'] = store;

export default store;

const createConnect = (store, unSubName) => (mapState, mapDispatch?) => target => {
if( typeof mapDispatch === 'function' ){
Object.assign( target, mapDispatch( store.dispatch ) );
} else if( typeof mapDispatch === 'object' ){
Object.assign( target, bindActionCreators( mapDispatch, store.dispatch ) );
}
const sub = Observable.prototype.map.call(store.state, mapState)
.distinctUntilChanged()
.subscribe(state => Object.assign(target, state));

if( unSubName ){
const original: () => void = target[unSubName];
target[unSubName] = function(...args){
sub.unsubscribe();
if( original ){
return original.apply(this, args);
}
};
}

return sub;
};

export const connect = createConnect(store, 'ionViewWillUnload');
1 change: 0 additions & 1 deletion src/store/initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export const initialState = {
},
homework: {
homework: [],
hidden: [],
filter: 'ALL_CLASSES',
},
login: {
Expand Down

0 comments on commit 6b33827

Please sign in to comment.