2
0

session.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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(loggedIn, userId, username, role) {
  12. return {
  13. type: LOGIN,
  14. loggedIn,
  15. userId,
  16. username,
  17. role,
  18. }
  19. }
  20. function banned(reason) {
  21. return {
  22. type: BANNED,
  23. reason,
  24. }
  25. }
  26. function unbanned() {
  27. return {
  28. type: UNBANNED,
  29. }
  30. }
  31. const initialState = Map({
  32. loggedIn: false,
  33. userId: "",
  34. username: "",
  35. role: "default",
  36. authProcessed: false,
  37. banned: {
  38. status: false,
  39. reason: "",
  40. },
  41. });
  42. function reducer(state = initialState, action) {
  43. switch (action.type) {
  44. case LOGOUT:
  45. return state.merge({
  46. loggedIn: initialState.get("loggedIn"),
  47. userId: initialState.get("userId"),
  48. username: initialState.get("username"),
  49. role: initialState.get("role"),
  50. authProcessed: false,
  51. });
  52. case LOGIN:
  53. const { loggedIn, userId, username, role } = action;
  54. if (loggedIn) {
  55. return state.merge({
  56. loggedIn: true,
  57. userId,
  58. username,
  59. role,
  60. authProcessed: true,
  61. });
  62. } else {
  63. return state.merge({
  64. authProcessed: true,
  65. });
  66. }
  67. case BANNED:
  68. const { reason } = action;
  69. return state.merge({
  70. banned: true,
  71. reason,
  72. });
  73. case UNBANNED:
  74. return state.merge({
  75. banned: false,
  76. reason: initialState.get("reason"),
  77. });
  78. }
  79. return state;
  80. }
  81. const actionCreators = {
  82. logout,
  83. login,
  84. banned,
  85. unbanned,
  86. };
  87. const actionTypes = {
  88. LOGOUT,
  89. LOGIN,
  90. BANNED,
  91. UNBANNED,
  92. };
  93. export {
  94. actionCreators,
  95. actionTypes,
  96. };
  97. export default reducer;