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: Add new selectors site filter #155

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions src/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import {
selectProjectMembershipsWithUsers,
selectProjectsWithTransferrableSites,
selectSitesAndUserRoles,
} from 'terraso-client-shared/selectors';
import { Site } from 'terraso-client-shared/site/siteSlice';
import { SerializableSet } from 'terraso-client-shared/store/utils';
Expand Down Expand Up @@ -184,3 +185,27 @@ test('can access all projects with role', () => {
unaffiliatedSites: [{ siteId: site3.id, siteName: site3.name }],
});
});

test('select user sites with project role', () => {
const user = generateUser();
const project1 = generateProject([generateMembership(user.id, 'manager')]);
const project2 = generateProject([
generateMembership(user.id, 'contributor'),
]);
const site1 = generateSite(project1);
const site2 = generateSite(project2);
const site3 = generateSite();
const site4 = generateSite(project2);

const store = createStore(
initState([project1, project2], [user], [site1, site2, site3, site4]),
);

const roles = selectSitesAndUserRoles(store.getState(), user.id);
expect(roles).toStrictEqual({
[site1.id]: 'manager',
[site2.id]: 'contributor',
[site3.id]: undefined,
[site4.id]: 'contributor',
});
});
31 changes: 31 additions & 0 deletions src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ const selectProjectsWithUserRole = createSelector(
),
);

const selectProjectUserRoles = (state: SharedState, userId?: string) => {
return Object.fromEntries(
mapValues(state.project.projects, project => {
if (userId === undefined) {
return {};
}
const membership = Object.values(project.memberships).find(
({ userId: membUserId }) => membUserId === userId,
);
if (membership) {
return [project.id, membership.userRole];
}
}).filter((item): item is [string, UserRole] => item !== undefined),
);
};

export const selectSitesAndUserRoles = createSelector(
[selectProjectUserRoles, selectSites],
(userRoleMap, sites) => {
return Object.fromEntries(
mapValues(sites, site => {
let role = undefined;
if (site.projectId !== undefined) {
role = userRoleMap[site.projectId];
}
return [site.id, role];
}),
);
},
);

export const selectProjectsWithTransferrableSites = createSelector(
[selectProjectsWithUserRole, selectSites],
(projects, sites) => {
Expand Down