refactor: MONOREPO
This commit is contained in:
208
frontend/src/reducers/index.ts
Normal file
208
frontend/src/reducers/index.ts
Normal file
@@ -0,0 +1,208 @@
|
||||
import * as Actions from "../actions";
|
||||
|
||||
import { ILearner } from "../models/learner";
|
||||
import { ILevel } from "../models/level";
|
||||
import { IUser } from "../models/user";
|
||||
import { IVocab } from "../models/vocab";
|
||||
import { IReviewCard, IReviewMetadata } from "../models/review";
|
||||
|
||||
interface IState {
|
||||
drawer: boolean;
|
||||
drawerButton: boolean;
|
||||
authenticated: boolean;
|
||||
|
||||
// TODO: Rework this
|
||||
user: IUser | {},
|
||||
|
||||
// All available levels
|
||||
levels: ILevel[];
|
||||
|
||||
login: {
|
||||
loading: boolean;
|
||||
snackMsg: string;
|
||||
snackOpen: boolean;
|
||||
};
|
||||
|
||||
level: {
|
||||
currentVocab: IVocab;
|
||||
lookedAt: number[];
|
||||
vocab: IVocab[];
|
||||
loading: boolean;
|
||||
};
|
||||
|
||||
levelList: {
|
||||
loading: boolean;
|
||||
};
|
||||
|
||||
review: {
|
||||
current: IReviewCard;
|
||||
|
||||
loading: boolean;
|
||||
vocab: IVocab[];
|
||||
metadata: IReviewMetadata;
|
||||
popoverOpen: boolean;
|
||||
popoverText: string;
|
||||
popoverColor: string;
|
||||
popoverTextColor: string;
|
||||
};
|
||||
|
||||
topTen: ILearner[];
|
||||
|
||||
lastReview: any;
|
||||
};
|
||||
|
||||
const initialState: IState = {
|
||||
// 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
|
||||
authenticated: true,
|
||||
|
||||
user: {},
|
||||
|
||||
login: {
|
||||
loading: false,
|
||||
snackMsg: "",
|
||||
snackOpen: false,
|
||||
},
|
||||
|
||||
levels: [],
|
||||
|
||||
level: {
|
||||
currentVocab: {} as IVocab,
|
||||
lookedAt: [0],
|
||||
vocab: [],
|
||||
loading: true,
|
||||
},
|
||||
|
||||
levelList: {
|
||||
loading: true,
|
||||
},
|
||||
|
||||
review: {
|
||||
current: {} as IReviewCard,
|
||||
|
||||
loading: true,
|
||||
vocab: [],
|
||||
metadata: {} as IReviewMetadata,
|
||||
popoverOpen: false,
|
||||
popoverText: "",
|
||||
popoverColor: "",
|
||||
popoverTextColor: "",
|
||||
},
|
||||
|
||||
lastReview: {
|
||||
correct: 0,
|
||||
wrong: 0,
|
||||
},
|
||||
|
||||
// The top ten
|
||||
topTen: [],
|
||||
};
|
||||
|
||||
export function LateinicusApp(state: IState = initialState, action: any) {
|
||||
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,
|
||||
});
|
||||
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,
|
||||
});
|
||||
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,
|
||||
}),
|
||||
});
|
||||
case Actions.LEVELLIST_SET_LOADING:
|
||||
return Object.assign({}, state, {
|
||||
levelList: Object.assign({}, state.levelList, {
|
||||
loading: action.state,
|
||||
}),
|
||||
});
|
||||
default:
|
||||
// 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);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user