about summary refs log tree commit diff
path: root/app/assets/javascripts/components/containers/status_container.jsx
blob: 2bcb7026c0abbb2fc7909236723be70bf14817f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { connect }       from 'react-redux';
import Status            from '../components/status';
import { makeGetStatus } from '../selectors';
import {
  replyCompose,
  mentionCompose
}                        from '../actions/compose';
import {
  reblog,
  favourite,
  unreblog,
  unfavourite
}                        from '../actions/interactions';
import { deleteStatus }  from '../actions/statuses';
import { openMedia }     from '../actions/modal';
import { createSelector } from 'reselect'

const mapStateToProps = (state, props) => ({
  statusBase: state.getIn(['statuses', props.id]),
  me: state.getIn(['meta', 'me'])
});

const makeMapStateToPropsInner = () => {
  const getStatus = (() => {
    return createSelector(
      [
        (_, base)     => base,
        (state, base) => (base ? state.getIn(['accounts', base.get('account')]) : null),
        (state, base) => (base ? state.getIn(['statuses', base.get('reblog')], null) : null)
      ],

      (base, account, reblog) => (base ? base.set('account', account).set('reblog', reblog) : null)
    );
  })();

  const mapStateToProps = (state, { statusBase }) => ({
    status: getStatus(state, statusBase)
  });

  return mapStateToProps;
};

const makeMapStateToPropsLast = () => {
  const getStatus = (() => {
    return createSelector(
      [
        (_, status)     => status,
        (state, status) => (status ? state.getIn(['accounts', status.getIn(['reblog', 'account'])], null) : null)
      ],

      (status, reblogAccount) => (status && status.get('reblog') ? status.setIn(['reblog', 'account'], reblogAccount) : status)
    );
  })();

  const mapStateToProps = (state, { status }) => ({
    status: getStatus(state, status)
  });

  return mapStateToProps;
};

const mapDispatchToProps = (dispatch) => ({

  onReply (status) {
    dispatch(replyCompose(status));
  },

  onReblog (status) {
    if (status.get('reblogged')) {
      dispatch(unreblog(status));
    } else {
      dispatch(reblog(status));
    }
  },

  onFavourite (status) {
    if (status.get('favourited')) {
      dispatch(unfavourite(status));
    } else {
      dispatch(favourite(status));
    }
  },

  onDelete (status) {
    dispatch(deleteStatus(status.get('id')));
  },

  onMention (account) {
    dispatch(mentionCompose(account));
  },

  onOpenMedia (url) {
    dispatch(openMedia(url));
  }

});

export default connect(mapStateToProps, mapDispatchToProps)(
  connect(makeMapStateToPropsInner)(
    connect(makeMapStateToPropsLast)(Status)
  )
);