post.js 1009 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import moment from "moment"
  2. import { hydrateUser } from "./users"
  3. export const PATCH_POST = "PATCH_POST"
  4. export function hydrate(json) {
  5. return Object.assign({}, json, {
  6. posted_on: moment(json.posted_on),
  7. updated_on: moment(json.updated_on),
  8. hidden_on: moment(json.hidden_on),
  9. attachments: json.attachments
  10. ? json.attachments.map(hydrateAttachment)
  11. : null,
  12. poster: json.poster ? hydrateUser(json.poster) : null,
  13. isSelected: false,
  14. isBusy: false,
  15. isDeleted: false
  16. })
  17. }
  18. export function hydrateAttachment(json) {
  19. return Object.assign({}, json, {
  20. uploaded_on: moment(json.uploaded_on)
  21. })
  22. }
  23. export function patch(post, patch) {
  24. return {
  25. type: PATCH_POST,
  26. post,
  27. patch
  28. }
  29. }
  30. export default function post(state = {}, action = null) {
  31. switch (action.type) {
  32. case PATCH_POST:
  33. if (state.id == action.post.id) {
  34. return Object.assign({}, state, action.patch)
  35. }
  36. return state
  37. default:
  38. return state
  39. }
  40. }