session.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Map } from "immutable";
  2. const LOGOUT = "SESSION::LOGOUT";
  3. const LOGIN = "SESSION::LOGIN";
  4. const BANNED = "SESSION::BANNED";
  5. const UNBANNED = "SESSION::UNBANNED";
  6. function logout() {
  7. return {
  8. type: LOGOUT,
  9. }
  10. }
  11. function login(userId, username, role) {
  12. return {
  13. type: LOGIN,
  14. userId,
  15. username,
  16. role,
  17. }
  18. }
  19. function banned(reason) {
  20. return {
  21. type: BANNED,
  22. reason,
  23. }
  24. }
  25. function unbanned() {
  26. return {
  27. type: UNBANNED,
  28. }
  29. }
  30. const initialState = Map({
  31. loggedIn: false,
  32. userId: "",
  33. username: "",
  34. role: "default",
  35. banned: {
  36. status: false,
  37. reason: "",
  38. },
  39. });
  40. function reducer(state = initialState, action) {
  41. switch (action.type) {
  42. case LOGOUT:
  43. return state.merge({
  44. loggedIn: initialState.get("loggedIn"),
  45. userId: initialState.get("userId"),
  46. username: initialState.get("username"),
  47. role: initialState.get("role"),
  48. });
  49. case LOGIN:
  50. const { userId, username, role } = action;
  51. return state.merge({
  52. userId,
  53. username,
  54. role,
  55. });
  56. case BANNED:
  57. const { reason } = action;
  58. return state.merge({
  59. banned: true,
  60. reason,
  61. });
  62. case UNBANNED:
  63. return state.merge({
  64. banned: false,
  65. reason: initialState.get("reason"),
  66. });
  67. }
  68. return state;
  69. }
  70. const actionCreators = {
  71. logout,
  72. login,
  73. banned,
  74. unbanned,
  75. };
  76. const actionTypes = {
  77. LOGOUT,
  78. LOGIN,
  79. BANNED,
  80. UNBANNED,
  81. };
  82. export {
  83. actionCreators,
  84. actionTypes,
  85. };
  86. export default reducer;