mastodon/app/assets/javascripts/components/features/compose/containers/compose_form_container.jsx

71 lines
2.3 KiB
React
Raw Normal View History

import { connect } from 'react-redux';
import ComposeForm from '../components/compose_form';
import { createSelector } from 'reselect';
import {
changeCompose,
submitCompose,
clearComposeSuggestions,
fetchComposeSuggestions,
selectComposeSuggestion,
changeComposeSpoilerText,
} from '../../../actions/compose';
const getMentionedUsernames = createSelector(state => state.getIn(['compose', 'text']), text => text.match(/(?:^|[^\/\w])@([a-z0-9_]+@[a-z0-9\.\-]+)/ig));
const getMentionedDomains = createSelector(getMentionedUsernames, mentionedUsernamesWithDomains => {
return mentionedUsernamesWithDomains !== null ? [...new Set(mentionedUsernamesWithDomains.map(item => item.split('@')[2]))] : [];
});
2017-02-22 15:43:07 +01:00
const mapStateToProps = (state, props) => {
const mentionedUsernames = getMentionedUsernames(state);
const mentionedUsernamesWithDomains = getMentionedDomains(state);
2017-02-22 15:43:07 +01:00
return {
text: state.getIn(['compose', 'text']),
suggestion_token: state.getIn(['compose', 'suggestion_token']),
suggestions: state.getIn(['compose', 'suggestions']),
spoiler: state.getIn(['compose', 'spoiler']),
spoiler_text: state.getIn(['compose', 'spoiler_text']),
unlisted: state.getIn(['compose', 'unlisted'], ),
private: state.getIn(['compose', 'private']),
fileDropDate: state.getIn(['compose', 'fileDropDate']),
focusDate: state.getIn(['compose', 'focusDate']),
preselectDate: state.getIn(['compose', 'preselectDate']),
is_submitting: state.getIn(['compose', 'is_submitting']),
is_uploading: state.getIn(['compose', 'is_uploading']),
me: state.getIn(['compose', 'me']),
needsPrivacyWarning: state.getIn(['compose', 'private']) && mentionedUsernames !== null,
mentionedDomains: mentionedUsernamesWithDomains
};
};
2017-02-22 15:43:07 +01:00
const mapDispatchToProps = (dispatch) => ({
2017-02-22 15:43:07 +01:00
onChange (text) {
dispatch(changeCompose(text));
},
2016-08-31 22:58:10 +02:00
2017-02-22 15:43:07 +01:00
onSubmit () {
dispatch(submitCompose());
},
2017-02-22 15:43:07 +01:00
onClearSuggestions () {
dispatch(clearComposeSuggestions());
},
2017-02-22 15:43:07 +01:00
onFetchSuggestions (token) {
dispatch(fetchComposeSuggestions(token));
},
2017-02-22 15:43:07 +01:00
onSuggestionSelected (position, token, accountId) {
dispatch(selectComposeSuggestion(position, token, accountId));
},
2017-02-22 15:43:07 +01:00
onChangeSpoilerText (checked) {
dispatch(changeComposeSpoilerText(checked));
},
2017-02-22 15:43:07 +01:00
});
2017-02-22 15:43:07 +01:00
export default connect(mapStateToProps, mapDispatchToProps)(ComposeForm);