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/frontend/src/reducers/index.ts

272 lines
7.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 { ILevel } from "../models/level";
2018-09-18 16:59:15 +00:00
import { IUser } from "../models/user";
2018-09-20 17:57:07 +00:00
import { IVocab, IReviewCard } from "../models/vocab";
import { IReviewMetadata } from "../models/review";
2018-09-18 16:59:15 +00:00
interface IState {
drawer: boolean;
scorePopoverOpen: boolean;
2018-09-18 16:59:15 +00:00
drawerButton: boolean;
authenticated: boolean;
didLogin: boolean;
2018-09-18 16:59:15 +00:00
// TODO: Rework this
user: IUser | {},
// All available levels
levels: ILevel[];
2018-09-18 16:59:15 +00:00
login: {
loading: boolean;
snackMsg: string;
snackOpen: boolean;
};
2018-09-18 18:20:26 +00:00
level: {
currentVocab: IVocab;
lookedAt: number[];
vocab: IVocab[];
loading: boolean;
};
2018-09-19 16:00:38 +00:00
levelList: {
loading: boolean;
};
dashboard: {
loadingNL: boolean;
loadingTT: boolean;
2018-09-24 11:36:42 +00:00
loadingLR: boolean;
};
review: {
current: IReviewCard;
2018-09-24 14:16:33 +00:00
dialogOpen: boolean;
loading: boolean;
vocab: IVocab[];
metadata: IReviewMetadata;
popoverOpen: boolean;
popoverText: string;
popoverColor: string;
popoverTextColor: string;
};
2018-09-18 16:59:15 +00:00
topTen: ILearner[];
nextLevel: ILevel;
lastReview: any;
2018-09-18 16:59:15 +00:00
};
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,
scorePopoverOpen: false,
didLogin: false,
2018-09-18 16:06:08 +00:00
// Is the user authenticated?
// TODO: Set this to false
authenticated: false,
2018-09-18 16:59:15 +00:00
2018-09-20 17:54:03 +00:00
user: {
score: 0,
},
2018-09-18 16:59:15 +00:00
login: {
loading: false,
snackMsg: "",
snackOpen: false,
},
levels: [],
2018-09-18 18:20:26 +00:00
level: {
currentVocab: {} as IVocab,
lookedAt: [0],
vocab: [],
loading: true,
},
2018-09-19 16:00:38 +00:00
levelList: {
loading: true,
},
dashboard: {
loadingNL: true,
loadingTT: true,
2018-09-24 11:36:42 +00:00
loadingLR: true,
},
review: {
current: {} as IReviewCard,
2018-09-24 14:16:33 +00:00
dialogOpen: false,
loading: true,
vocab: [],
metadata: {} as IReviewMetadata,
popoverOpen: false,
popoverText: "",
popoverColor: "",
popoverTextColor: "",
},
nextLevel: {} as ILevel,
lastReview: {
correct: 0,
wrong: 0,
},
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_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,
}),
});
case Actions.SET_LEVELS:
return Object.assign({}, state, {
levels: action.levels,
});
case Actions.REVIEW_SET_POPOVER:
return Object.assign({}, state, {
review: Object.assign({}, state.review, {
popoverText: action.text,
popoverOpen: action.state,
popoverColor: action.color,
popoverTextColor: action.textColor,
}),
});
case Actions.SET_REVIEW:
return Object.assign({}, state, {
review: Object.assign({}, state.review, {
current: action.current,
metadata: action.meta,
}),
});
case Actions.SET_LAST_REVIEW:
return Object.assign({}, state, {
lastReview: action.metadata,
});
case Actions.REVIEW_SET_LOADING:
return Object.assign({}, state, {
review: Object.assign({}, state.review, {
loading: action.state,
}),
});
2018-09-19 16:00:38 +00:00
case Actions.LEVELLIST_SET_LOADING:
return Object.assign({}, state, {
levelList: Object.assign({}, state.levelList, {
loading: action.state,
}),
});
case Actions.SET_SCORE_POPOVER:
return Object.assign({}, state, {
scorePopoverOpen: action.state,
});
case Actions.SET_NEXT_LEVEL:
return Object.assign({}, state, {
nextLevel: action.level,
});
case Actions.DASHBOARD_SET_NL_LOADING:
return Object.assign({}, state, {
dashboard: Object.assign({}, state.dashboard, {
loadingNL: action.state,
}),
});
case Actions.SET_TOP_TEN:
return Object.assign({}, state, {
topTen: action.topTen,
});
case Actions.DASHBOARD_SET_TT_LOADING:
return Object.assign({}, state, {
dashboard: Object.assign({}, state.dashboard, {
loadingTT: action.state,
}),
});
case Actions.SET_DID_LOGIN:
return Object.assign({}, state, {
didLogin: state,
});
2018-09-24 11:36:42 +00:00
case Actions.DASHBOARD_SET_LR_LOADING:
return Object.assign({}, state, {
dashboard: Object.assign({}, state.dashboard, {
loadingLR: action.state,
}),
});
2018-09-24 14:16:33 +00:00
case Actions.REVIEW_SET_DIALOG:
return Object.assign({}, state, {
review: Object.assign({}, state.review, {
dialogOpen: action.state,
}),
});
2018-09-18 16:06:08 +00:00
default:
2018-09-19 16:00:38 +00:00
// Ignore the initialization call to the reducer. By that we can
// catch all actions that are not implemented
if (action.type && !action.type.startsWith("@@redux/INIT")) {
console.log("Reducer not implemented:", action.type);
}
2018-09-18 16:06:08 +00:00
return state;
}
};