2016-09-20 07:25:59 +10:00
|
|
|
import {
|
|
|
|
COMPOSE_CHANGE,
|
|
|
|
COMPOSE_REPLY,
|
|
|
|
COMPOSE_REPLY_CANCEL,
|
|
|
|
COMPOSE_SUBMIT_REQUEST,
|
|
|
|
COMPOSE_SUBMIT_SUCCESS,
|
|
|
|
COMPOSE_SUBMIT_FAIL,
|
|
|
|
COMPOSE_UPLOAD_REQUEST,
|
|
|
|
COMPOSE_UPLOAD_SUCCESS,
|
|
|
|
COMPOSE_UPLOAD_FAIL,
|
|
|
|
COMPOSE_UPLOAD_UNDO,
|
|
|
|
COMPOSE_UPLOAD_PROGRESS
|
|
|
|
} from '../actions/compose';
|
2016-09-06 00:56:43 +10:00
|
|
|
import { TIMELINE_DELETE } from '../actions/timelines';
|
|
|
|
import Immutable from 'immutable';
|
2016-09-01 00:15:12 +10:00
|
|
|
|
|
|
|
const initialState = Immutable.Map({
|
|
|
|
text: '',
|
2016-09-01 06:58:10 +10:00
|
|
|
in_reply_to: null,
|
2016-09-08 02:17:15 +10:00
|
|
|
is_submitting: false,
|
|
|
|
is_uploading: false,
|
|
|
|
progress: 0,
|
|
|
|
media_attachments: Immutable.List([])
|
2016-09-01 00:15:12 +10:00
|
|
|
});
|
|
|
|
|
2016-09-22 08:09:21 +10:00
|
|
|
function statusToTextMentions(status) {
|
|
|
|
return Immutable.OrderedSet([`@${status.getIn(['account', 'acct'])} `]).union(status.get('mentions').map(mention => `@${mention.get('acct')} `)).join('');
|
|
|
|
};
|
|
|
|
|
2016-09-01 00:15:12 +10:00
|
|
|
export default function compose(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_CHANGE:
|
2016-09-01 00:15:12 +10:00
|
|
|
return state.set('text', action.text);
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_REPLY:
|
2016-09-01 00:15:12 +10:00
|
|
|
return state.withMutations(map => {
|
2016-09-06 00:56:43 +10:00
|
|
|
map.set('in_reply_to', action.status.get('id'));
|
2016-09-22 08:09:21 +10:00
|
|
|
map.set('text', statusToTextMentions(action.status));
|
2016-09-01 00:15:12 +10:00
|
|
|
});
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_REPLY_CANCEL:
|
2016-09-01 06:58:10 +10:00
|
|
|
return state.withMutations(map => {
|
2016-09-08 02:17:15 +10:00
|
|
|
map.set('in_reply_to', null);
|
|
|
|
map.set('text', '');
|
2016-09-01 06:58:10 +10:00
|
|
|
});
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_SUBMIT_REQUEST:
|
2016-09-01 06:58:10 +10:00
|
|
|
return state.set('is_submitting', true);
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_SUBMIT_SUCCESS:
|
2016-09-01 06:58:10 +10:00
|
|
|
return state.withMutations(map => {
|
2016-09-08 02:17:15 +10:00
|
|
|
map.set('text', '');
|
|
|
|
map.set('is_submitting', false);
|
|
|
|
map.set('in_reply_to', null);
|
|
|
|
map.update('media_attachments', list => list.clear());
|
2016-09-01 06:58:10 +10:00
|
|
|
});
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_SUBMIT_FAIL:
|
2016-09-01 06:58:10 +10:00
|
|
|
return state.set('is_submitting', false);
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_UPLOAD_REQUEST:
|
2016-09-08 02:17:15 +10:00
|
|
|
return state.set('is_uploading', true);
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_UPLOAD_SUCCESS:
|
2016-09-08 02:17:15 +10:00
|
|
|
return state.withMutations(map => {
|
|
|
|
map.update('media_attachments', list => list.push(Immutable.fromJS(action.media)));
|
|
|
|
map.set('is_uploading', false);
|
|
|
|
});
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_UPLOAD_FAIL:
|
2016-09-08 02:17:15 +10:00
|
|
|
return state.set('is_uploading', false);
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_UPLOAD_UNDO:
|
2016-09-08 02:17:15 +10:00
|
|
|
return state.update('media_attachments', list => list.filterNot(item => item.get('id') === action.media_id));
|
2016-09-20 07:25:59 +10:00
|
|
|
case COMPOSE_UPLOAD_PROGRESS:
|
2016-09-08 02:17:15 +10:00
|
|
|
return state.set('progress', Math.round((action.loaded / action.total) * 100));
|
2016-09-06 00:56:43 +10:00
|
|
|
case TIMELINE_DELETE:
|
|
|
|
if (action.id === state.get('in_reply_to')) {
|
|
|
|
return state.set('in_reply_to', null);
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
2016-09-01 00:15:12 +10:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-09-13 03:20:55 +10:00
|
|
|
};
|