Permalink
Browse files

Merge pull request #2 from chao7150/auto_hashtag

auto hashtag feature
  • Loading branch information...
chao7150 committed Aug 31, 2018
2 parents 9485c4f + 1a78c86 commit a35c2643c10c15ffaf4bc3c78657988eda27f7f5
@@ -49,13 +49,22 @@ export const COMPOSE_UPLOAD_CHANGE_REQUEST = 'COMPOSE_UPLOAD_UPDATE_REQUEST'
export const COMPOSE_UPLOAD_CHANGE_SUCCESS = 'COMPOSE_UPLOAD_UPDATE_SUCCESS';
export const COMPOSE_UPLOAD_CHANGE_FAIL = 'COMPOSE_UPLOAD_UPDATE_FAIL';
export const HASHTAG_CHANGE = 'HASHTAG_CHANGE';
export function changeCompose(text) {
return {
type: COMPOSE_CHANGE,
text: text,
};
};
export function changeHashtag(text) {
return {
type: HASHTAG_CHANGE,
text: text,
};
};
export function replyCompose(status, router) {
return (dispatch, getState) => {
dispatch({
@@ -155,6 +164,54 @@ export function submitCompose() {
};
};
export function submitComposeWithHashtag() {
return function (dispatch, getState) {
const status = getState().getIn(['compose', 'text'], '') + ' ' + getState().getIn(['compose', 'preservedHashtag']);
const media = getState().getIn(['compose', 'media_attachments']);
if ((!status || !status.length) && media.size === 0) {
return;
}
dispatch(submitComposeRequest());
api(getState).post('/api/v1/statuses', {
status,
in_reply_to_id: getState().getIn(['compose', 'in_reply_to'], null),
media_ids: media.map(item => item.get('id')),
sensitive: getState().getIn(['compose', 'sensitive']),
spoiler_text: getState().getIn(['compose', 'spoiler_text'], ''),
visibility: getState().getIn(['compose', 'privacy']),
}, {
headers: {
'Idempotency-Key': getState().getIn(['compose', 'idempotencyKey']),
},
}).then(function (response) {
dispatch(insertIntoTagHistory(response.data.tags));
dispatch(submitComposeSuccess({ ...response.data }));
// To make the app more responsive, immediately get the status into the columns
const insertIfOnline = (timelineId) => {
if (getState().getIn(['timelines', timelineId, 'items', 0]) !== null) {
dispatch(updateTimeline(timelineId, { ...response.data }));
}
};
insertIfOnline('home');
if (response.data.in_reply_to_id === null && response.data.visibility === 'public') {
insertIfOnline('community');
insertIfOnline('public');
} else if (response.data.visibility === 'direct') {
insertIfOnline('direct');
}
}).catch(function (error) {
dispatch(submitComposeFail(error));
});
};
};
export function submitComposeRequest() {
return {
type: COMPOSE_SUBMIT_REQUEST,
@@ -53,6 +53,9 @@ export default class ComposeForm extends ImmutablePureComponent {
onPickEmoji: PropTypes.func.isRequired,
showSearch: PropTypes.bool,
anyMedia: PropTypes.bool,
preservedHashtag: PropTypes.string,
onChangeHashtag: PropTypes.func.isRequired,
onSubmitWithHashtag: PropTypes.func.isRequired,
};
static defaultProps = {
@@ -63,10 +66,35 @@ export default class ComposeForm extends ImmutablePureComponent {
this.props.onChange(e.target.value);
}
handleChangeHashtag = (e) => {
this.props.onChangeHashtag(e.target.value);
}
handleKeyDown = (e) => {
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
this.handleSubmit();
}
if (e.keyCode === 13 && e.shiftKey) {
this.handleSubmitWithHashtag();
}
}
handleSubmitWithHashtag = () => {
if (this.props.text !== this.autosuggestTextarea.textarea.value) {
// Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
// Update the state to match the current text
this.props.onChange(this.autosuggestTextarea.textarea.value);
}
// Submit disabled:
const { is_submitting, is_uploading, anyMedia } = this.props;
const fulltext = [this.props.spoiler_text, countableText(this.props.text)].join('');
if (is_submitting || is_uploading || length(fulltext) > 500 || (fulltext.length !== 0 && fulltext.trim().length === 0 && !anyMedia)) {
return;
}
this.props.onSubmitWithHashtag();
}
handleSubmit = () => {
@@ -202,6 +230,7 @@ export default class ComposeForm extends ImmutablePureComponent {
</div>
<div className='compose-form__buttons-wrapper'>
<div className='compose-form__buttons'>
<UploadButtonContainer />
<PrivacyDropdownContainer />
@@ -210,8 +239,11 @@ export default class ComposeForm extends ImmutablePureComponent {
</div>
<div className='character-counter__wrapper'><CharacterCounter max={500} text={text} /></div>
</div>
<div className='compose-form__publish'>
<div className='compose-form__hashtag-preserver'>
<input placeholder="#hashtag" class='hashtag-preserver' value={this.props.preservedHashtag} onChange={this.handleChangeHashtag}></input>
</div>
<div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabledButton} block /></div>
</div>
</div>
@@ -3,7 +3,9 @@ import ComposeForm from '../components/compose_form';
import { uploadCompose } from '../../../actions/compose';
import {
changeCompose,
changeHashtag,
submitCompose,
submitComposeWithHashtag,
clearComposeSuggestions,
fetchComposeSuggestions,
selectComposeSuggestion,
@@ -33,10 +35,18 @@ const mapDispatchToProps = (dispatch) => ({
dispatch(changeCompose(text));
},
onChangeHashtag (text) {
dispatch(changeHashtag(text));
},
onSubmit () {
dispatch(submitCompose());
},
onSubmitWithHashtag () {
dispatch(submitComposeWithHashtag());
},
onClearSuggestions () {
dispatch(clearComposeSuggestions());
},
@@ -29,6 +29,7 @@ import {
COMPOSE_UPLOAD_CHANGE_SUCCESS,
COMPOSE_UPLOAD_CHANGE_FAIL,
COMPOSE_RESET,
HASHTAG_CHANGE,
} from '../actions/compose';
import { TIMELINE_DELETE } from '../actions/timelines';
import { STORE_HYDRATE } from '../actions/store';
@@ -61,6 +62,7 @@ const initialState = ImmutableMap({
resetFileKey: Math.floor((Math.random() * 0x10000)),
idempotencyKey: null,
tagHistory: ImmutableList(),
preservedHashtag: '',
});
function statusToTextMentions(state, status) {
@@ -217,6 +219,9 @@ export default function compose(state = initialState, action) {
return state
.set('text', action.text)
.set('idempotencyKey', uuid());
case HASHTAG_CHANGE:
return state
.set('preservedHashtag', action.text);
case COMPOSE_COMPOSING_CHANGE:
return state.set('is_composing', action.value);
case COMPOSE_REPLY:
@@ -5396,3 +5396,8 @@ noscript {
}
}
}
.hashtag-preserver{
align-content: center;
width: 150px;
margin: 16px 2px;
}

0 comments on commit a35c264

Please sign in to comment.