123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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(loggedIn, userId, username, role) {
- return {
- type: LOGIN,
- loggedIn,
- userId,
- username,
- role,
- }
- }
- function banned(reason) {
- return {
- type: BANNED,
- reason,
- }
- }
- function unbanned() {
- return {
- type: UNBANNED,
- }
- }
- const initialState = Map({
- loggedIn: false,
- userId: "",
- username: "",
- role: "default",
- authProcessed: false,
- 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"),
- authProcessed: false,
- });
- case LOGIN:
- const { loggedIn, userId, username, role } = action;
- if (loggedIn) {
- return state.merge({
- loggedIn: true,
- userId,
- username,
- role,
- authProcessed: true,
- });
- } else {
- return state.merge({
- authProcessed: true,
- });
- }
- 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;
|