Skip to content

Commit

Permalink
Save
Browse files Browse the repository at this point in the history
  • Loading branch information
mikozet committed Aug 28, 2023
1 parent 6ea0b13 commit 4141c03
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 32 deletions.
12 changes: 10 additions & 2 deletions src/components/ActivityStreamWithTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import TabNavigationAction from '~/components/TabNavigationAction';
import { useIsOrganization } from '~/hooks/username';
import core from '~/services/core';
import translate from '~/services/locale';
import { loadMoreActivities, updateLastSeen } from '~/store/activity/actions';
import {
loadMoreActivities,
loadMoreActivitiesNews,
updateLastSeen,
} from '~/store/activity/actions';
import { CATEGORIES } from '~/store/activity/reducers';
import {
IconConnections,
Expand Down Expand Up @@ -159,7 +163,11 @@ const ActivityStreamWithTabs = ({ basePath = ACTIVITIES_PATH }) => {
const isLoading = activity?.isLoadingMore || activity?.lastUpdated === 0;

const handleLoadMore = () => {
dispatch(loadMoreActivities(selectedCategory));
if (selectedCategory === QUERY_FILTER_MAP.news) {
dispatch(loadMoreActivitiesNews());
} else {
dispatch(loadMoreActivities(selectedCategory));
}
};

const exportStatementBtnHandler = () => {
Expand Down
88 changes: 79 additions & 9 deletions src/store/activity/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { formatCirclesValue } from '~/utils/format';

const { ActivityTypes, ActivityFilterTypes } = core.activity;

export const PAGE_SIZE = 10;
export const PAGE_SIZE = 2;

export function initializeActivities() {
const lastSeenAt = getLastSeen();
Expand Down Expand Up @@ -109,7 +109,13 @@ export function checkFinishedActivities({
return;
}

dispatch(loadMoreActivitiesNews({ withLoader: false }));
dispatch(
loadMoreActivitiesNews({
fromOffsetZero: true,
withLoader: false,
liveRefresh: true,
}),
);

// Check for mutual activities with other user address
if (
Expand Down Expand Up @@ -349,9 +355,6 @@ export function loadMoreActivitiesNews(options = {}) {
fromOffsetZero = false,
withLoader = true,
liveRefresh = false,
// afterDate,
limit = PAGE_SIZE,
isActive,
} = options;
return async (dispatch, getState) => {
const { activity } = getState();
Expand All @@ -363,7 +366,7 @@ export function loadMoreActivitiesNews(options = {}) {
});
}

const offset = fromOffsetZero ? 0 : activity.news.offset;
const offset = fromOffsetZero ? 0 : activity.mutualActivities.offset;

try {
// TODO - we could use const { data } = await core.news.getLatestNews... but it's undefined here
Expand All @@ -373,8 +376,8 @@ export function loadMoreActivitiesNews(options = {}) {
data: {
// afterDate,
// isActive,
// limit,
// offset,
limit: PAGE_SIZE,
offset,
},
});
// const { data } = await core.news.getLatestNews({
Expand All @@ -391,7 +394,6 @@ export function loadMoreActivitiesNews(options = {}) {
meta: {
activities: data,
offset: liveRefresh ? currentOffset : currentOffset + PAGE_SIZE,
// lastTimestamp,
},
});
} catch (error) {
Expand All @@ -412,6 +414,74 @@ export function loadMoreActivitiesNews(options = {}) {
}
};
}
// export function loadMoreActivitiesNews(options = {}) {
// const {
// fromOffsetZero = false,
// withLoader = true,
// liveRefresh = false,
// // afterDate,
// limit = PAGE_SIZE,
// isActive,
// } = options;
// return async (dispatch, getState) => {
// const { activity } = getState();
// const currentOffset = activity.news.offset;

// if (withLoader) {
// dispatch({
// type: ActionTypes.ACTIVITIES_NEWS_LOAD_MORE,
// });
// }

// const offset = fromOffsetZero ? 0 : activity.news.offset;

// try {
// // TODO - we could use const { data } = await core.news.getLatestNews... but it's undefined here
// const { data } = await core.utils.requestAPI({
// path: ['news'],
// method: 'GET',
// data: {
// // afterDate,
// // isActive,
// // limit,
// // offset,
// },
// });
// // const { data } = await core.news.getLatestNews({
// // // afterDate,
// // // isActive,
// // // limit,
// // // offset,
// // });

// console.log('data', data);

// dispatch({
// type: ActionTypes.ACTIVITIES_NEWS_LOAD_MORE_SUCCESS,
// meta: {
// activities: data,
// offset: liveRefresh ? currentOffset : currentOffset + PAGE_SIZE,
// // lastTimestamp,
// },
// });
// } catch (error) {
// logError(error);
// dispatch(
// notify({
// text: (
// <Typography classes={{ root: 'body4_white' }} variant="body4">
// {translateErrorForUser(error)}
// </Typography>
// ),
// type: NotificationsTypes.ERROR,
// }),
// );
// dispatch({
// type: ActionTypes.ACTIVITIES_NEWS_LOAD_MORE_ERROR,
// });
// }
// };
// }

export function resetActivities({ isClearingStorage = true } = {}) {
if (isClearingStorage) {
Expand Down
27 changes: 6 additions & 21 deletions src/store/activity/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const initialStateItem = {

const initialStateCategory = {
...initialStateItem,
// isLoading: false,
};

const initialStateActivity = {
Expand All @@ -46,13 +45,7 @@ const initialStateActivity = {
};

const initialStateActivityMutual = {
activities: [],
isError: false,
isLoadingMore: true,
isMoreAvailable: true,
lastTimestamp: 0,
lastUpdatedAt: null,
offset: 0,
...initialStateItem,
mutualAddress: null,
};

Expand Down Expand Up @@ -189,7 +182,7 @@ const activityReducer = (state = initialState, action) => {
},
});
}
case ActionTypes.ACTIVITIES_LOAD_MORE:
case ActionTypes.ACTIVITIES_LOAD_MORE: {
return update(state, {
categories: {
[action.meta.category]: {
Expand All @@ -198,6 +191,7 @@ const activityReducer = (state = initialState, action) => {
},
},
});
}
case ActionTypes.ACTIVITIES_LOAD_MORE_SUCCESS: {
// Add new activities
const newActivities = mergeActivities(
Expand Down Expand Up @@ -271,30 +265,21 @@ const activityReducer = (state = initialState, action) => {
},
});
case ActionTypes.ACTIVITIES_NEWS_LOAD_MORE_SUCCESS: {
// Nothing more to add ..
if (action.meta.activities.lengthx === 0) {
return update(state, {
news: {
isLoadingMore: { $set: false },
isMoreAvailable: { $set: false },
},
});
}

// Add new activities
const newActivities = mergeNews(
state.news.activities,
action.meta.activities,
);

// Update offset and add new objects
return update(state, {
news: {
activities: { $set: newActivities },
isLoadingMore: { $set: false },
offset: { $set: action.meta.offset },
...(action.meta.activities.length < PAGE_SIZE
? { isMoreAvailable: { $set: false } }
: {}),
lastUpdatedAt: { $set: DateTime.local().toISO() },
lastTimestamp: { $set: DateTime.local().toISO() },
},
});
}
Expand Down

0 comments on commit 4141c03

Please sign in to comment.