diff --git a/src/selectors.test.ts b/src/selectors.test.ts index 7d8c8325..08393fe9 100644 --- a/src/selectors.test.ts +++ b/src/selectors.test.ts @@ -96,11 +96,22 @@ const keyBy = ( ); }; -function initState(projects: Project[], users: User[], sites: Site[] = []) { +function initState( + projects: Project[], + users: User[], + sites: Site[] = [], + currentUserID?: string, +) { return merge( + { account: { ...accountInitialState } }, { account: { users: keyBy(users, 'id'), + currentUser: { + data: { + id: currentUserID ?? users[0]?.id, + }, + }, }, project: { projects: keyBy(projects, 'id'), @@ -109,7 +120,6 @@ function initState(projects: Project[], users: User[], sites: Site[] = []) { sites: keyBy(sites, 'id'), }, }, - { account: { ...accountInitialState } }, ); } @@ -166,7 +176,12 @@ test('can access all projects with role', () => { const site3 = generateSite(); const store = createStore( - initState([project1, project2, project3], [user], [site1, site2, site3]), + initState( + [project1, project2, project3], + [user], + [site1, site2, site3], + user.id, + ), ); const pairs = selectProjectsWithTransferrableSites( store.getState(), @@ -201,10 +216,15 @@ test('select user sites with project role', () => { const site4 = generateSite(project2); const store = createStore( - initState([project1, project2], [user], [site1, site2, site3, site4]), + initState( + [project1, project2], + [user], + [site1, site2, site3, site4], + user.id, + ), ); - const roles = selectSitesAndUserRoles(store.getState(), user.id); + const roles = selectSitesAndUserRoles(store.getState()); expect(roles).toStrictEqual({ [site1.id]: 'manager', [site2.id]: 'contributor', diff --git a/src/selectors.ts b/src/selectors.ts index 5eaba77f..a0c18869 100644 --- a/src/selectors.ts +++ b/src/selectors.ts @@ -1,7 +1,10 @@ import { createSelector } from '@reduxjs/toolkit'; import { User } from 'terraso-client-shared/account/accountSlice'; import { UserRole } from 'terraso-client-shared/graphqlSchema/graphql'; -import { ProjectMembership } from 'terraso-client-shared/project/projectSlice'; +import { + Project, + ProjectMembership, +} from 'terraso-client-shared/project/projectSlice'; import { type SharedState } from 'terraso-client-shared/store/store'; import { exists, filterValues, mapValues } from 'terraso-client-shared/utils'; @@ -35,9 +38,12 @@ const selectProjectsWithUserRole = createSelector( ), ); -export const selectProjectUserRoles = (state: SharedState, userId?: string) => { +const createUserRoleMap = ( + projects: Record, + userId?: string, +) => { return Object.fromEntries( - mapValues(state.project.projects, project => { + mapValues(projects, project => { if (userId === undefined) { return {}; } @@ -51,9 +57,18 @@ export const selectProjectUserRoles = (state: SharedState, userId?: string) => { ); }; +const selectCurrentUserID = (state: SharedState) => + state.account.currentUser?.data?.id; + +export const selectProjectUserRolesMap = createSelector( + [selectCurrentUserID, selectProjects], + (currentUserID, projects) => createUserRoleMap(projects, currentUserID), +); + export const selectSitesAndUserRoles = createSelector( - [selectProjectUserRoles, selectSites], - (userRoleMap, sites) => { + [selectCurrentUserID, selectProjects, selectSites], + (userID, projects, sites) => { + const userRoleMap = createUserRoleMap(projects, userID); return Object.fromEntries( mapValues(sites, site => { let role = undefined;