migrated get notifications endpoint

This commit is contained in:
Nouman Tahir 2021-07-05 16:03:38 +05:00
parent 48b4bec41f
commit 8b9ef8b4a3
3 changed files with 25 additions and 51 deletions

View File

@ -49,7 +49,7 @@ class NotificationView extends PureComponent {
this.setState({ selectedFilter: filters[index].key, selectedIndex: index });
await changeSelectedFilter(filters[index].key, index);
getActivities(null, filters[index].key, false);
getActivities(filters[index].key, false);
};
_renderList = (data) => {

View File

@ -337,49 +337,24 @@ export const getLeaderboard = async (duration:'day'|'week'|'month') => {
}
}
export const getActivities = (data) =>
new Promise((resolve, reject) => {
let url = null;
switch (data.type) {
case 'activities':
url = `/activities/${data.user}`;
break;
case 'votes':
url = `/rvotes/${data.user}`;
break;
case 'replies':
url = `/replies/${data.user}`;
break;
case 'mentions':
url = `/mentions/${data.user}`;
break;
case 'follows':
url = `/follows/${data.user}`;
break;
case 'reblogs':
url = `/reblogs/${data.user}`;
break;
case 'transfers':
url = `/transfers/${data.user}`;
break;
default:
url = `/activities/${data.user}`;
break;
/**
* fetches notifications from ecency server using filter and since props
* @param data optional filter and since props;
* @returns array of notifications
*/
export const getNotifications = async (data:{
filter?: "rvotes"|"mentions"|"follows"|"replies"|"reblogs"|"transfers",
since?:string
}) => {
try{
const response = await ecencyApi.post(`/private-api/notifications`, data);
return response.data;
}catch(error){
console.warn("Failed to get notifications", error)
bugsnag.notify(error)
throw error;
}
api
.get(url, {
params: {
since: data.since,
},
})
.then((res) => {
resolve(res.data);
})
.catch((error) => {
bugsnag.notify(error);
reject(error);
});
});
}
export const getUnreadNotificationCount = async () => {

View File

@ -6,7 +6,7 @@ import get from 'lodash/get';
import { injectIntl } from 'react-intl';
// Actions and Services
import { getActivities, markActivityAsRead, markNotifications } from '../../../providers/ecency/ecency';
import { getNotifications, markNotifications } from '../../../providers/ecency/ecency';
import { updateUnreadActivityCount } from '../../../redux/actions/accountAction';
// Constants
@ -29,21 +29,20 @@ class NotificationContainer extends Component {
}
componentDidMount() {
const { username, isConnected } = this.props;
const { isConnected } = this.props;
if (username && isConnected) {
this._getAvtivities(username);
if (isConnected) {
this._getAvtivities();
}
}
_getAvtivities = (user, type = null, loadMore = false) => {
_getAvtivities = (type = null, loadMore = false) => {
const { lastNotificationId, notifications, endOfNotification } = this.state;
const since = loadMore ? lastNotificationId : null;
const { username } = this.props;
if (!endOfNotification || !loadMore) {
this.setState({ isNotificationRefreshing: true });
getActivities({ user: user || username, type, since })
getNotifications({ filter:type, since:since })
.then((res) => {
console.log(res);
const lastId = res.length > 0 ? [...res].pop().id : null;
@ -152,7 +151,7 @@ class NotificationContainer extends Component {
(nextProps.username !== username && nextProps.username)
) {
this.setState({ endOfNotification: false }, () =>
this._getAvtivities(nextProps.username, selectedFilter),
this._getAvtivities(selectedFilter),
);
}
}