Mastodon: Add ability to follow hashtags in web UI Closes #1192 See merge request soapbox-pub/soapbox!2200environments/review-develop-3zknud/deployments/3361
commit
91bc40c732
@ -0,0 +1,201 @@
|
|||||||
|
import api, { getLinks } from '../api';
|
||||||
|
|
||||||
|
import type { AxiosError } from 'axios';
|
||||||
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
||||||
|
import type { APIEntity } from 'soapbox/types/entities';
|
||||||
|
|
||||||
|
const HASHTAG_FETCH_REQUEST = 'HASHTAG_FETCH_REQUEST';
|
||||||
|
const HASHTAG_FETCH_SUCCESS = 'HASHTAG_FETCH_SUCCESS';
|
||||||
|
const HASHTAG_FETCH_FAIL = 'HASHTAG_FETCH_FAIL';
|
||||||
|
|
||||||
|
const HASHTAG_FOLLOW_REQUEST = 'HASHTAG_FOLLOW_REQUEST';
|
||||||
|
const HASHTAG_FOLLOW_SUCCESS = 'HASHTAG_FOLLOW_SUCCESS';
|
||||||
|
const HASHTAG_FOLLOW_FAIL = 'HASHTAG_FOLLOW_FAIL';
|
||||||
|
|
||||||
|
const HASHTAG_UNFOLLOW_REQUEST = 'HASHTAG_UNFOLLOW_REQUEST';
|
||||||
|
const HASHTAG_UNFOLLOW_SUCCESS = 'HASHTAG_UNFOLLOW_SUCCESS';
|
||||||
|
const HASHTAG_UNFOLLOW_FAIL = 'HASHTAG_UNFOLLOW_FAIL';
|
||||||
|
|
||||||
|
const FOLLOWED_HASHTAGS_FETCH_REQUEST = 'FOLLOWED_HASHTAGS_FETCH_REQUEST';
|
||||||
|
const FOLLOWED_HASHTAGS_FETCH_SUCCESS = 'FOLLOWED_HASHTAGS_FETCH_SUCCESS';
|
||||||
|
const FOLLOWED_HASHTAGS_FETCH_FAIL = 'FOLLOWED_HASHTAGS_FETCH_FAIL';
|
||||||
|
|
||||||
|
const FOLLOWED_HASHTAGS_EXPAND_REQUEST = 'FOLLOWED_HASHTAGS_EXPAND_REQUEST';
|
||||||
|
const FOLLOWED_HASHTAGS_EXPAND_SUCCESS = 'FOLLOWED_HASHTAGS_EXPAND_SUCCESS';
|
||||||
|
const FOLLOWED_HASHTAGS_EXPAND_FAIL = 'FOLLOWED_HASHTAGS_EXPAND_FAIL';
|
||||||
|
|
||||||
|
const fetchHashtag = (name: string) => (dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
|
dispatch(fetchHashtagRequest());
|
||||||
|
|
||||||
|
api(getState).get(`/api/v1/tags/${name}`).then(({ data }) => {
|
||||||
|
dispatch(fetchHashtagSuccess(name, data));
|
||||||
|
}).catch(err => {
|
||||||
|
dispatch(fetchHashtagFail(err));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchHashtagRequest = () => ({
|
||||||
|
type: HASHTAG_FETCH_REQUEST,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetchHashtagSuccess = (name: string, tag: APIEntity) => ({
|
||||||
|
type: HASHTAG_FETCH_SUCCESS,
|
||||||
|
name,
|
||||||
|
tag,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetchHashtagFail = (error: AxiosError) => ({
|
||||||
|
type: HASHTAG_FETCH_FAIL,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
|
||||||
|
const followHashtag = (name: string) => (dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
|
dispatch(followHashtagRequest(name));
|
||||||
|
|
||||||
|
api(getState).post(`/api/v1/tags/${name}/follow`).then(({ data }) => {
|
||||||
|
dispatch(followHashtagSuccess(name, data));
|
||||||
|
}).catch(err => {
|
||||||
|
dispatch(followHashtagFail(name, err));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const followHashtagRequest = (name: string) => ({
|
||||||
|
type: HASHTAG_FOLLOW_REQUEST,
|
||||||
|
name,
|
||||||
|
});
|
||||||
|
|
||||||
|
const followHashtagSuccess = (name: string, tag: APIEntity) => ({
|
||||||
|
type: HASHTAG_FOLLOW_SUCCESS,
|
||||||
|
name,
|
||||||
|
tag,
|
||||||
|
});
|
||||||
|
|
||||||
|
const followHashtagFail = (name: string, error: AxiosError) => ({
|
||||||
|
type: HASHTAG_FOLLOW_FAIL,
|
||||||
|
name,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
|
||||||
|
const unfollowHashtag = (name: string) => (dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
|
dispatch(unfollowHashtagRequest(name));
|
||||||
|
|
||||||
|
api(getState).post(`/api/v1/tags/${name}/unfollow`).then(({ data }) => {
|
||||||
|
dispatch(unfollowHashtagSuccess(name, data));
|
||||||
|
}).catch(err => {
|
||||||
|
dispatch(unfollowHashtagFail(name, err));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const unfollowHashtagRequest = (name: string) => ({
|
||||||
|
type: HASHTAG_UNFOLLOW_REQUEST,
|
||||||
|
name,
|
||||||
|
});
|
||||||
|
|
||||||
|
const unfollowHashtagSuccess = (name: string, tag: APIEntity) => ({
|
||||||
|
type: HASHTAG_UNFOLLOW_SUCCESS,
|
||||||
|
name,
|
||||||
|
tag,
|
||||||
|
});
|
||||||
|
|
||||||
|
const unfollowHashtagFail = (name: string, error: AxiosError) => ({
|
||||||
|
type: HASHTAG_UNFOLLOW_FAIL,
|
||||||
|
name,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetchFollowedHashtags = () => (dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
|
dispatch(fetchFollowedHashtagsRequest());
|
||||||
|
|
||||||
|
api(getState).get('/api/v1/followed_tags').then(response => {
|
||||||
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
||||||
|
dispatch(fetchFollowedHashtagsSuccess(response.data, next ? next.uri : null));
|
||||||
|
}).catch(err => {
|
||||||
|
dispatch(fetchFollowedHashtagsFail(err));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchFollowedHashtagsRequest = () => ({
|
||||||
|
type: FOLLOWED_HASHTAGS_FETCH_REQUEST,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetchFollowedHashtagsSuccess = (followed_tags: APIEntity[], next: string | null) => ({
|
||||||
|
type: FOLLOWED_HASHTAGS_FETCH_SUCCESS,
|
||||||
|
followed_tags,
|
||||||
|
next,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetchFollowedHashtagsFail = (error: AxiosError) => ({
|
||||||
|
type: FOLLOWED_HASHTAGS_FETCH_FAIL,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
|
||||||
|
const expandFollowedHashtags = () => (dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
|
const url = getState().followed_tags.next;
|
||||||
|
|
||||||
|
if (url === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch(expandFollowedHashtagsRequest());
|
||||||
|
|
||||||
|
api(getState).get(url).then(response => {
|
||||||
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
||||||
|
dispatch(expandFollowedHashtagsSuccess(response.data, next ? next.uri : null));
|
||||||
|
}).catch(error => {
|
||||||
|
dispatch(expandFollowedHashtagsFail(error));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const expandFollowedHashtagsRequest = () => ({
|
||||||
|
type: FOLLOWED_HASHTAGS_EXPAND_REQUEST,
|
||||||
|
});
|
||||||
|
|
||||||
|
const expandFollowedHashtagsSuccess = (followed_tags: APIEntity[], next: string | null) => ({
|
||||||
|
type: FOLLOWED_HASHTAGS_EXPAND_SUCCESS,
|
||||||
|
followed_tags,
|
||||||
|
next,
|
||||||
|
});
|
||||||
|
|
||||||
|
const expandFollowedHashtagsFail = (error: AxiosError) => ({
|
||||||
|
type: FOLLOWED_HASHTAGS_EXPAND_FAIL,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
export {
|
||||||
|
HASHTAG_FETCH_REQUEST,
|
||||||
|
HASHTAG_FETCH_SUCCESS,
|
||||||
|
HASHTAG_FETCH_FAIL,
|
||||||
|
HASHTAG_FOLLOW_REQUEST,
|
||||||
|
HASHTAG_FOLLOW_SUCCESS,
|
||||||
|
HASHTAG_FOLLOW_FAIL,
|
||||||
|
HASHTAG_UNFOLLOW_REQUEST,
|
||||||
|
HASHTAG_UNFOLLOW_SUCCESS,
|
||||||
|
HASHTAG_UNFOLLOW_FAIL,
|
||||||
|
FOLLOWED_HASHTAGS_FETCH_REQUEST,
|
||||||
|
FOLLOWED_HASHTAGS_FETCH_SUCCESS,
|
||||||
|
FOLLOWED_HASHTAGS_FETCH_FAIL,
|
||||||
|
FOLLOWED_HASHTAGS_EXPAND_REQUEST,
|
||||||
|
FOLLOWED_HASHTAGS_EXPAND_SUCCESS,
|
||||||
|
FOLLOWED_HASHTAGS_EXPAND_FAIL,
|
||||||
|
fetchHashtag,
|
||||||
|
fetchHashtagRequest,
|
||||||
|
fetchHashtagSuccess,
|
||||||
|
fetchHashtagFail,
|
||||||
|
followHashtag,
|
||||||
|
followHashtagRequest,
|
||||||
|
followHashtagSuccess,
|
||||||
|
followHashtagFail,
|
||||||
|
unfollowHashtag,
|
||||||
|
unfollowHashtagRequest,
|
||||||
|
unfollowHashtagSuccess,
|
||||||
|
unfollowHashtagFail,
|
||||||
|
fetchFollowedHashtags,
|
||||||
|
fetchFollowedHashtagsRequest,
|
||||||
|
fetchFollowedHashtagsSuccess,
|
||||||
|
fetchFollowedHashtagsFail,
|
||||||
|
expandFollowedHashtags,
|
||||||
|
expandFollowedHashtagsRequest,
|
||||||
|
expandFollowedHashtagsSuccess,
|
||||||
|
expandFollowedHashtagsFail,
|
||||||
|
};
|
@ -0,0 +1,52 @@
|
|||||||
|
import debounce from 'lodash/debounce';
|
||||||
|
import React, { useEffect } from 'react';
|
||||||
|
import { defineMessages, useIntl, FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
import { fetchFollowedHashtags, expandFollowedHashtags } from 'soapbox/actions/tags';
|
||||||
|
import Hashtag from 'soapbox/components/hashtag';
|
||||||
|
import ScrollableList from 'soapbox/components/scrollable-list';
|
||||||
|
import { Column } from 'soapbox/components/ui';
|
||||||
|
import PlaceholderHashtag from 'soapbox/features/placeholder/components/placeholder-hashtag';
|
||||||
|
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
|
||||||
|
|
||||||
|
const messages = defineMessages({
|
||||||
|
heading: { id: 'column.followed_tags', defaultMessage: 'Followed hashtags' },
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleLoadMore = debounce((dispatch) => {
|
||||||
|
dispatch(expandFollowedHashtags());
|
||||||
|
}, 300, { leading: true });
|
||||||
|
|
||||||
|
const FollowedTags = () => {
|
||||||
|
const intl = useIntl();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
dispatch(fetchFollowedHashtags());
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const tags = useAppSelector((state => state.followed_tags.items));
|
||||||
|
const isLoading = useAppSelector((state => state.followed_tags.isLoading));
|
||||||
|
const hasMore = useAppSelector((state => !!state.followed_tags.next));
|
||||||
|
|
||||||
|
const emptyMessage = <FormattedMessage id='empty_column.followed_tags' defaultMessage="You haven't followed any hashtag yet." />;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Column label={intl.formatMessage(messages.heading)}>
|
||||||
|
<ScrollableList
|
||||||
|
scrollKey='followed_tags'
|
||||||
|
emptyMessage={emptyMessage}
|
||||||
|
isLoading={isLoading}
|
||||||
|
hasMore={hasMore}
|
||||||
|
onLoadMore={() => handleLoadMore(dispatch)}
|
||||||
|
placeholderComponent={PlaceholderHashtag}
|
||||||
|
placeholderCount={5}
|
||||||
|
itemClassName='pb-3'
|
||||||
|
>
|
||||||
|
{tags.map(tag => <Hashtag key={tag.name} hashtag={tag} />)}
|
||||||
|
</ScrollableList>
|
||||||
|
</Column>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default FollowedTags;
|
@ -0,0 +1,47 @@
|
|||||||
|
import { List as ImmutableList, Record as ImmutableRecord } from 'immutable';
|
||||||
|
|
||||||
|
import {
|
||||||
|
FOLLOWED_HASHTAGS_FETCH_REQUEST,
|
||||||
|
FOLLOWED_HASHTAGS_FETCH_SUCCESS,
|
||||||
|
FOLLOWED_HASHTAGS_FETCH_FAIL,
|
||||||
|
FOLLOWED_HASHTAGS_EXPAND_REQUEST,
|
||||||
|
FOLLOWED_HASHTAGS_EXPAND_SUCCESS,
|
||||||
|
FOLLOWED_HASHTAGS_EXPAND_FAIL,
|
||||||
|
} from 'soapbox/actions/tags';
|
||||||
|
import { normalizeTag } from 'soapbox/normalizers';
|
||||||
|
|
||||||
|
import type { AnyAction } from 'redux';
|
||||||
|
import type { APIEntity, Tag } from 'soapbox/types/entities';
|
||||||
|
|
||||||
|
const ReducerRecord = ImmutableRecord({
|
||||||
|
items: ImmutableList<Tag>(),
|
||||||
|
isLoading: false,
|
||||||
|
next: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default function followed_tags(state = ReducerRecord(), action: AnyAction) {
|
||||||
|
switch (action.type) {
|
||||||
|
case FOLLOWED_HASHTAGS_FETCH_REQUEST:
|
||||||
|
return state.set('isLoading', true);
|
||||||
|
case FOLLOWED_HASHTAGS_FETCH_SUCCESS:
|
||||||
|
return state.withMutations(map => {
|
||||||
|
map.set('items', ImmutableList(action.followed_tags.map((item: APIEntity) => normalizeTag(item))));
|
||||||
|
map.set('isLoading', false);
|
||||||
|
map.set('next', action.next);
|
||||||
|
});
|
||||||
|
case FOLLOWED_HASHTAGS_FETCH_FAIL:
|
||||||
|
return state.set('isLoading', false);
|
||||||
|
case FOLLOWED_HASHTAGS_EXPAND_REQUEST:
|
||||||
|
return state.set('isLoading', true);
|
||||||
|
case FOLLOWED_HASHTAGS_EXPAND_SUCCESS:
|
||||||
|
return state.withMutations(map => {
|
||||||
|
map.update('items', list => list.concat(action.followed_tags.map((item: APIEntity) => normalizeTag(item))));
|
||||||
|
map.set('isLoading', false);
|
||||||
|
map.set('next', action.next);
|
||||||
|
});
|
||||||
|
case FOLLOWED_HASHTAGS_EXPAND_FAIL:
|
||||||
|
return state.set('isLoading', false);
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
import { Map as ImmutableMap } from 'immutable';
|
||||||
|
|
||||||
|
import {
|
||||||
|
HASHTAG_FETCH_SUCCESS,
|
||||||
|
HASHTAG_FOLLOW_REQUEST,
|
||||||
|
HASHTAG_FOLLOW_FAIL,
|
||||||
|
HASHTAG_UNFOLLOW_REQUEST,
|
||||||
|
HASHTAG_UNFOLLOW_FAIL,
|
||||||
|
} from 'soapbox/actions/tags';
|
||||||
|
import { normalizeTag } from 'soapbox/normalizers';
|
||||||
|
|
||||||
|
import type { AnyAction } from 'redux';
|
||||||
|
import type { Tag } from 'soapbox/types/entities';
|
||||||
|
|
||||||
|
const initialState = ImmutableMap<string, Tag>();
|
||||||
|
|
||||||
|
export default function tags(state = initialState, action: AnyAction) {
|
||||||
|
switch (action.type) {
|
||||||
|
case HASHTAG_FETCH_SUCCESS:
|
||||||
|
return state.set(action.name, normalizeTag(action.tag));
|
||||||
|
case HASHTAG_FOLLOW_REQUEST:
|
||||||
|
case HASHTAG_UNFOLLOW_FAIL:
|
||||||
|
return state.setIn([action.name, 'following'], true);
|
||||||
|
case HASHTAG_FOLLOW_FAIL:
|
||||||
|
case HASHTAG_UNFOLLOW_REQUEST:
|
||||||
|
return state.setIn([action.name, 'following'], false);
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue