Skip to content

Commit

Permalink
Merge pull request #2696 from webkom/nfp-2022-random-interestgroup-se…
Browse files Browse the repository at this point in the history
…lector

Add randomly selected interestgroups to frontpage
  • Loading branch information
PeterJFB authored Apr 30, 2022
2 parents 8d8bbd1 + 78c44c3 commit 3cb782a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/actions/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export const Group = {
CREATE: (generateStatuses('Group.CREATE'): AAT),
REMOVE: (generateStatuses('Group.REMOVE'): AAT),
MEMBERSHIP_FETCH: (generateStatuses('Group.MEMBERSHIP_FETCH'): AAT),
FETCH_RANDOM_INTERESTS: (generateStatuses(
'Group.FETCH_RANDOM_INTERESTS'
): AAT),
};

export const CompanyInterestForm = {
Expand Down
13 changes: 13 additions & 0 deletions app/actions/GroupActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ export function fetchAllWithType(type: string): Thunk<any> {
});
}

export function fetchRandomInterestGroups(): Thunk<any> {
return callAPI({
types: Group.FETCH_RANDOM_INTERESTS,
endpoint: '/groups/random_interests/',
method: 'GET',
meta: {
errorMessage: 'Henting av tilfeldige interessegrupper feilet',
},
useCache: false,
schema: [groupSchema],
});
}

export function editGroup(group: Object): Thunk<*> {
return (dispatch) =>
dispatch(
Expand Down
4 changes: 3 additions & 1 deletion app/reducers/frontpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { sortBy } from 'lodash';
import { createSelector } from 'reselect';
import { selectArticles } from './articles';
import { selectEvents } from './events';
import { selectRandomInterestGroups } from './groups';

export default fetching(Frontpage.FETCH);

export const selectFrontpage = createSelector(
selectArticles,
selectEvents,
(articles, events) => {
selectRandomInterestGroups,
(articles, events, interestGroups) => {
articles = articles.map((article) => ({
...article,
documentType: 'article',
Expand Down
13 changes: 13 additions & 0 deletions app/reducers/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default createEntityReducer({
}
break;

case Group.FETCH_RANDOM_INTERESTS.SUCCESS:
newState.randomInterestById = action.payload.result;
break;

default:
break;
}
Expand All @@ -72,3 +76,12 @@ export const selectGroupsWithType = createSelector(
(state, props) => props.groupType,
(groups, groupType) => groups.filter((g) => g.type === groupType)
);

export const selectRandomInterestGroups = createSelector(
(state) => state.groups.byId,
(state) => state.groups.randomInterestById,
(groupsById, groupIds) => {
if (!groupsById || !groupIds) return [];
return groupIds.map((id) => groupsById[id]);
}
);
8 changes: 7 additions & 1 deletion app/routes/overview/IndexRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import {
} from 'app/reducers/feeds';
import { selectPinnedPolls } from 'app/reducers/polls';
import { votePoll } from 'app/actions/PollActions';
import { fetchRandomInterestGroups } from 'app/actions/GroupActions';
import { selectRandomInterestGroups } from 'app/reducers/groups';

const mapStateToProps = (state) => ({
frontpage: selectFrontpage(state),
feed: selectFeedById(state, { feedId: 'personal' }),
shouldFetchQuote: isEmpty(selectRandomQuote(state)),
shouldFetchInterestGroups: isEmpty(selectRandomInterestGroups(state)),
feedItems: selectFeedActivitesByFeedId(state, {
feedId: 'personal',
}),
Expand All @@ -38,10 +41,13 @@ export default compose(
// ),
connect(mapStateToProps, mapDispatchToProps),
prepare(
({ shouldFetchQuote, loggedIn }, dispatch) =>
({ shouldFetchQuote, shouldFetchInterestGroups, loggedIn }, dispatch) =>
Promise.all([
loggedIn && shouldFetchQuote && dispatch(fetchRandomQuote()),
dispatch(fetchReadmes(loggedIn ? 4 : 1)),
loggedIn &&
shouldFetchInterestGroups &&
dispatch(fetchRandomInterestGroups()),
]),
[],
{ awaitOnSsr: false }
Expand Down

0 comments on commit 3cb782a

Please sign in to comment.