user.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {
  2. SET,
  3. UPDATE,
  4. USER_CONNECTED,
  5. USER_DISCONNECTED,
  6. USER_LANG,
  7. USER_NAME,
  8. USER_AUTH
  9. } from '../action-creator.sync.js'
  10. import { generateAvatarFromPublicName } from 'tracim_frontend_lib'
  11. const defaultUser = {
  12. user_id: -1,
  13. logged: null, // null avoid to be redirected to /login while whoami ep has not responded yet
  14. auth: '',
  15. timezone: '',
  16. profile: {
  17. id: 1,
  18. slug: 'user'
  19. },
  20. email: '',
  21. is_active: true,
  22. caldav_url: null,
  23. avatar_url: null,
  24. created: '',
  25. public_name: '',
  26. lang: ''
  27. }
  28. export default function user (state = defaultUser, action) {
  29. switch (action.type) {
  30. case `${SET}/${USER_CONNECTED}`:
  31. return {
  32. ...state,
  33. ...action.user,
  34. lang: action.user.lang ? action.user.lang : 'en',
  35. avatar_url: action.user.avatar_url
  36. ? action.user.avatar_url
  37. : action.user.public_name ? generateAvatarFromPublicName(action.user.public_name) : ''
  38. }
  39. case `${SET}/${USER_DISCONNECTED}`:
  40. return {...defaultUser, logged: false}
  41. case `${SET}/${USER_LANG}`:
  42. return {...state, lang: action.lang}
  43. case `${UPDATE}/${USER_NAME}`:
  44. return {...state, public_name: action.newName}
  45. case `${UPDATE}/${USER_AUTH}`:
  46. return {...state, auth: action.auth}
  47. default:
  48. return state
  49. }
  50. }