user.js 751 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { Map } from "immutable";
  2. import {
  3. BAN,
  4. AUTHENTICATE,
  5. } from "actions/app";
  6. const initialState = Map({
  7. loggedIn: false,
  8. role: "default",
  9. username: "",
  10. userId: "",
  11. banned: {
  12. status: false,
  13. reason: "",
  14. },
  15. });
  16. const actionsMap = {
  17. [BAN]: (state, action) => {
  18. return Object.assign({}, state.toObject(), {
  19. banned: {
  20. status: true,
  21. reason: action.reason,
  22. },
  23. });
  24. },
  25. [AUTHENTICATE]: (state, action) => {
  26. return state.merge({
  27. loggedIn: action.data.loggedIn,
  28. role: action.data.role,
  29. username: action.data.username,
  30. userId: action.data.userId,
  31. });
  32. },
  33. };
  34. export default function reducer(state = initialState, action = {}) {
  35. const fn = actionsMap[action.type];
  36. return fn ? fn(state, action) : state;
  37. }