session.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. authProcessed: false,
  36. banned: {
  37. status: false,
  38. reason: "",
  39. },
  40. });
  41. function reducer(state = initialState, action) {
  42. switch (action.type) {
  43. case LOGOUT:
  44. return state.merge({
  45. loggedIn: initialState.get("loggedIn"),
  46. userId: initialState.get("userId"),
  47. username: initialState.get("username"),
  48. role: initialState.get("role"),
  49. authProcessed: false,
  50. });
  51. case LOGIN:
  52. const { userId, username, role } = action;
  53. return state.merge({
  54. loggedIn: true,
  55. userId,
  56. username,
  57. role,
  58. authProcessed: true,
  59. });
  60. case BANNED:
  61. const { reason } = action;
  62. return state.merge({
  63. banned: true,
  64. reason,
  65. });
  66. case UNBANNED:
  67. return state.merge({
  68. banned: false,
  69. reason: initialState.get("reason"),
  70. });
  71. }
  72. return state;
  73. }
  74. const actionCreators = {
  75. logout,
  76. login,
  77. banned,
  78. unbanned,
  79. };
  80. const actionTypes = {
  81. LOGOUT,
  82. LOGIN,
  83. BANNED,
  84. UNBANNED,
  85. };
  86. export {
  87. actionCreators,
  88. actionTypes,
  89. };
  90. export default reducer;