12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import { Map } from "immutable";
- const LOGOUT = "SESSION::LOGOUT";
- const LOGIN = "SESSION::LOGIN";
- const BANNED = "SESSION::BANNED";
- const UNBANNED = "SESSION::UNBANNED";
- function logout() {
- return {
- type: LOGOUT,
- }
- }
- function login(userId, username, role) {
- return {
- type: LOGIN,
- userId,
- username,
- role,
- }
- }
- function banned(reason) {
- return {
- type: BANNED,
- reason,
- }
- }
- function unbanned() {
- return {
- type: UNBANNED,
- }
- }
- const initialState = Map({
- loggedIn: false,
- userId: "",
- username: "",
- role: "default",
- banned: {
- status: false,
- reason: "",
- },
- });
- function reducer(state = initialState, action) {
- switch (action.type) {
- case LOGOUT:
- return state.merge({
- loggedIn: initialState.get("loggedIn"),
- userId: initialState.get("userId"),
- username: initialState.get("username"),
- role: initialState.get("role"),
- });
- case LOGIN:
- const { userId, username, role } = action;
- return state.merge({
- userId,
- username,
- role,
- });
- case BANNED:
- const { reason } = action;
- return state.merge({
- banned: true,
- reason,
- });
- case UNBANNED:
- return state.merge({
- banned: false,
- reason: initialState.get("reason"),
- });
- }
- return state;
- }
- const actionCreators = {
- logout,
- login,
- banned,
- unbanned,
- };
- const actionTypes = {
- LOGOUT,
- LOGIN,
- BANNED,
- UNBANNED,
- };
- export {
- actionCreators,
- actionTypes,
- };
- export default reducer;
|