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