diff --git a/app/actions/ActionTypes.js b/app/actions/ActionTypes.js index 3115c10e1f..1c90949285 100644 --- a/app/actions/ActionTypes.js +++ b/app/actions/ActionTypes.js @@ -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 = { diff --git a/app/actions/GroupActions.js b/app/actions/GroupActions.js index 016e47918e..b78cbffc61 100644 --- a/app/actions/GroupActions.js +++ b/app/actions/GroupActions.js @@ -84,6 +84,19 @@ export function fetchAllWithType(type: string): Thunk { }); } +export function fetchRandomInterestGroups(): Thunk { + 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( diff --git a/app/reducers/frontpage.js b/app/reducers/frontpage.js index 2c47982be8..68b3e0f398 100644 --- a/app/reducers/frontpage.js +++ b/app/reducers/frontpage.js @@ -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', diff --git a/app/reducers/groups.js b/app/reducers/groups.js index 5329c596b5..7ae0896755 100644 --- a/app/reducers/groups.js +++ b/app/reducers/groups.js @@ -49,6 +49,10 @@ export default createEntityReducer({ } break; + case Group.FETCH_RANDOM_INTERESTS.SUCCESS: + newState.randomInterestById = action.payload.result; + break; + default: break; } @@ -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]); + } +); diff --git a/app/routes/overview/IndexRoute.js b/app/routes/overview/IndexRoute.js index a16d49f847..175594de22 100644 --- a/app/routes/overview/IndexRoute.js +++ b/app/routes/overview/IndexRoute.js @@ -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', }), @@ -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 }