Skip to content

Commit

Permalink
fix: Select project role by current user
Browse files Browse the repository at this point in the history
  • Loading branch information
David Code Howard committed Nov 8, 2023
1 parent a128e2f commit a3d4b52
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
30 changes: 25 additions & 5 deletions src/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,22 @@ const keyBy = <T, Index extends keyof T>(
);
};

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'),
Expand All @@ -109,7 +120,6 @@ function initState(projects: Project[], users: User[], sites: Site[] = []) {
sites: keyBy(sites, 'id'),
},
},
{ account: { ...accountInitialState } },
);
}

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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',
Expand Down
25 changes: 20 additions & 5 deletions src/selectors.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -35,9 +38,12 @@ const selectProjectsWithUserRole = createSelector(
),
);

export const selectProjectUserRoles = (state: SharedState, userId?: string) => {
const createUserRoleMap = (
projects: Record<string, Project>,
userId?: string,
) => {
return Object.fromEntries(
mapValues(state.project.projects, project => {
mapValues(projects, project => {
if (userId === undefined) {
return {};
}
Expand All @@ -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;
Expand Down

0 comments on commit a3d4b52

Please sign in to comment.