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