Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: select project by role #159

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/selectors.test.ts
Original file line number Diff line number Diff line change
@@ -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'),
@@ -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',
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';

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

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 {};
}
@@ -51,9 +57,18 @@ 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;