This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
Lateinicus/src/reducers/index.ts

132 lines
3.6 KiB
TypeScript
Raw Normal View History

2018-09-18 16:06:08 +00:00
import * as Actions from "../actions";
2018-09-18 16:59:15 +00:00
import { ILearner } from "../models/learner";
import { IUser } from "../models/user";
2018-09-18 18:20:26 +00:00
import { IVocab } from "../models/vocab";
2018-09-18 16:59:15 +00:00
interface IState {
drawer: boolean;
drawerButton: boolean;
authenticated: boolean;
// TODO: Rework this
user: IUser | {},
login: {
loading: boolean;
snackMsg: string;
snackOpen: boolean;
};
2018-09-18 18:20:26 +00:00
level: {
currentVocab: IVocab;
lookedAt: number[];
toReview: boolean;
vocab: IVocab[];
loading: boolean;
};
2018-09-18 16:59:15 +00:00
topTen: ILearner[];
};
const initialState: IState = {
2018-09-18 16:06:08 +00:00
// Show the drawer?
drawer: false,
// Should we show the button to open the drawer?
drawerButton: true,
// Is the user authenticated?
// TODO: Set this to false
2018-09-18 18:20:26 +00:00
authenticated: true,
2018-09-18 16:59:15 +00:00
user: {},
login: {
loading: false,
snackMsg: "",
snackOpen: false,
},
2018-09-18 18:20:26 +00:00
level: {
currentVocab: {} as IVocab,
lookedAt: [0],
toReview: false,
vocab: [],
loading: true,
},
2018-09-18 16:59:15 +00:00
// The top ten
topTen: [],
2018-09-18 16:06:08 +00:00
};
2018-09-18 16:59:15 +00:00
export function LateinicusApp(state: IState = initialState, action: any) {
2018-09-18 16:06:08 +00:00
switch (action.type) {
case Actions.SET_DRAWER:
return Object.assign({}, state, {
drawer: action.show,
});
case Actions.SET_DRAWER_BUTTON:
return Object.assign({}, state, {
drawerButton: action.show,
});
2018-09-18 16:59:15 +00:00
case Actions.LOGIN_SET_SNACKBAR:
return Object.assign({}, state, {
login: {
loading: state.login.loading,
snackMsg: action.msg,
snackOpen: action.show,
},
});
case Actions.LOGIN_SET_LOADING:
return Object.assign({}, state, {
login: {
loading: action.show,
snackMsg: state.login.snackMsg,
snackOpen: state.login.snackOpen,
},
});
case Actions.SET_AUTHENTICATED:
return Object.assign({}, state, {
authenticated: action.status,
});
case Actions.SET_USER:
return Object.assign({}, state, {
user: action.user,
});
2018-09-18 18:20:26 +00:00
case Actions.LEVEL_SET_REVIEW:
return Object.assign({}, state, {
level: Object.assign({}, state.level, {
toReview: action.state,
// Make sure that we are in a "loading mode", when the page gets
// called the next time
loading: true,
}),
});
case Actions.LEVEL_SET_LOOKEDAT:
return Object.assign({}, state, {
level: Object.assign({}, state.level, {
lookedAt: action.lookedAt,
}),
});
case Actions.LEVEL_SET_CUR_VOCAB:
return Object.assign({}, state, {
level: Object.assign({}, state.level, {
currentVocab: action.vocab,
}),
});
case Actions.LEVEL_SET_VOCAB:
return Object.assign({}, state, {
level: Object.assign({}, state.level, {
vocab: action.vocab,
}),
});
case Actions.LEVEL_SET_LOADING:
return Object.assign({}, state, {
level: Object.assign({}, state.level, {
loading: action.state,
}),
});
2018-09-18 16:06:08 +00:00
default:
return state;
}
};