post.js 1001 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 ? json.attachments.map(hydrateAttachment) : null,
  10. poster: json.poster ? hydrateUser(json.poster) : null,
  11. isSelected: false,
  12. isBusy: false,
  13. isDeleted: false
  14. });
  15. }
  16. export function hydrateAttachment(json) {
  17. return Object.assign({}, json, {
  18. uploaded_on: moment(json.uploaded_on)
  19. });
  20. }
  21. export function patch(post, patch) {
  22. return {
  23. type: PATCH_POST,
  24. post,
  25. patch
  26. };
  27. }
  28. export default function post(state={}, action=null) {
  29. switch (action.type) {
  30. case PATCH_POST:
  31. if (state.id == action.post.id) {
  32. return Object.assign({}, state, action.patch);
  33. }
  34. return state;
  35. default:
  36. return state;
  37. }
  38. }