mirror of
https://github.com/plankanban/planka.git
synced 2025-12-27 09:14:59 +03:00
@@ -1,3 +1,8 @@
|
||||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
import { createSelector } from 'redux-orm';
|
||||
|
||||
import orm from '../orm';
|
||||
@@ -5,6 +10,134 @@ import { selectPath } from './router';
|
||||
import { selectCurrentUserId } from './users';
|
||||
import { isLocalId } from '../utils/local-id';
|
||||
|
||||
export const makeSelectProjectById = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
({ Project }, id) => {
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
return projectModel.ref;
|
||||
},
|
||||
);
|
||||
|
||||
export const selectProjectById = makeSelectProjectById();
|
||||
|
||||
export const makeSelectBoardIdsByProjectId = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project, User }, id, currentUserId) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
const currentUserModel = User.withId(currentUserId);
|
||||
|
||||
return projectModel
|
||||
.getBoardsModelArrayAvailableForUser(currentUserModel)
|
||||
.map((boardModel) => boardModel.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectBoardIdsByProjectId = makeSelectBoardIdsByProjectId();
|
||||
|
||||
export const makeSelectFirstBoardIdByProjectId = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project, User }, id, currentUserId) => {
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
const currentUserModel = User.withId(currentUserId);
|
||||
const boardsModels = projectModel.getBoardsModelArrayAvailableForUser(currentUserModel);
|
||||
|
||||
return boardsModels[0] && boardsModels[0].id;
|
||||
},
|
||||
);
|
||||
|
||||
export const selectFirstBoardIdByProjectId = makeSelectFirstBoardIdByProjectId();
|
||||
|
||||
export const makeSelectNotificationsTotalByProjectId = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project, User }, id, currentUserId) => {
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
const currentUserModel = User.withId(currentUserId);
|
||||
const boardsModels = projectModel.getBoardsModelArrayAvailableForUser(currentUserModel);
|
||||
|
||||
return boardsModels.reduce(
|
||||
(result, boardModel) => result + boardModel.getUnreadNotificationsQuerySet().count(),
|
||||
0,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectNotificationsTotalByProjectId = makeSelectNotificationsTotalByProjectId();
|
||||
|
||||
export const makeSelectIsProjectWithIdAvailableForCurrentUser = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project, User }, id, currentUserId) => {
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentUserModel = User.withId(currentUserId);
|
||||
return projectModel.isAvailableForUser(currentUserModel);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectIsProjectWithIdAvailableForCurrentUser =
|
||||
makeSelectIsProjectWithIdAvailableForCurrentUser();
|
||||
|
||||
export const makeSelectIsProjectWithIdExternalAccessibleForCurrentUser = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project, User }, id, currentUserId) => {
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentUserModel = User.withId(currentUserId);
|
||||
return projectModel.isExternalAccessibleForUser(currentUserModel);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectIsProjectWithIdExternalAccessibleForCurrentUser =
|
||||
makeSelectIsProjectWithIdExternalAccessibleForCurrentUser();
|
||||
|
||||
export const selectCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
@@ -26,8 +159,7 @@ export const selectCurrentProject = createSelector(
|
||||
export const selectManagersForCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project }, id, currentUserId) => {
|
||||
({ Project }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
@@ -39,24 +171,20 @@ export const selectManagersForCurrentProject = createSelector(
|
||||
}
|
||||
|
||||
return projectModel
|
||||
.getOrderedManagersQuerySet()
|
||||
.getManagersQuerySet()
|
||||
.toModelArray()
|
||||
.map((projectManagerModel) => ({
|
||||
...projectManagerModel.ref,
|
||||
isPersisted: !isLocalId(projectManagerModel.id),
|
||||
user: {
|
||||
...projectManagerModel.user.ref,
|
||||
isCurrent: projectManagerModel.user.id === currentUserId,
|
||||
},
|
||||
user: projectManagerModel.user.ref,
|
||||
}));
|
||||
},
|
||||
);
|
||||
|
||||
export const selectBoardsForCurrentProject = createSelector(
|
||||
export const selectManagerUserIdsForCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project }, id, currentUserId) => {
|
||||
({ Project }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
@@ -68,14 +196,101 @@ export const selectBoardsForCurrentProject = createSelector(
|
||||
}
|
||||
|
||||
return projectModel
|
||||
.getOrderedBoardsModelArrayAvailableForUser(currentUserId)
|
||||
.map((boardModel) => ({
|
||||
...boardModel.ref,
|
||||
isPersisted: !isLocalId(boardModel.id),
|
||||
.getManagersQuerySet()
|
||||
.toRefArray()
|
||||
.map((projectManager) => projectManager.userId);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectBackgroundImageIdsForCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
({ Project }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
return projectModel
|
||||
.getBackgroundImagesQuerySet()
|
||||
.toRefArray()
|
||||
.map((backgroundImage) => backgroundImage.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectBaseCustomFieldGroupIdsForCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
({ Project }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
return projectModel
|
||||
.getBaseCustomFieldGroupsQuerySet()
|
||||
.toRefArray()
|
||||
.map((baseCustomFieldGroup) => baseCustomFieldGroup.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectBaseCustomFieldGroupsForCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
({ Project }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
return projectModel
|
||||
.getBaseCustomFieldGroupsQuerySet()
|
||||
.toRefArray()
|
||||
.map((baseCustomFieldGroup) => ({
|
||||
...baseCustomFieldGroup,
|
||||
isPersisted: !isLocalId(baseCustomFieldGroup.id),
|
||||
}));
|
||||
},
|
||||
);
|
||||
|
||||
export const selectBoardIdsForCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Project, User }, id, currentUserId) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const projectModel = Project.withId(id);
|
||||
|
||||
if (!projectModel) {
|
||||
return projectModel;
|
||||
}
|
||||
|
||||
const currentUserModel = User.withId(currentUserId);
|
||||
|
||||
return projectModel
|
||||
.getBoardsModelArrayAvailableForUser(currentUserModel)
|
||||
.map((boardModel) => boardModel.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectIsCurrentUserManagerForCurrentProject = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).projectId,
|
||||
@@ -91,13 +306,29 @@ export const selectIsCurrentUserManagerForCurrentProject = createSelector(
|
||||
return false;
|
||||
}
|
||||
|
||||
return projectModel.hasManagerForUser(currentUserId);
|
||||
return projectModel.hasManagerWithUserId(currentUserId);
|
||||
},
|
||||
);
|
||||
|
||||
export default {
|
||||
makeSelectProjectById,
|
||||
selectProjectById,
|
||||
makeSelectBoardIdsByProjectId,
|
||||
selectBoardIdsByProjectId,
|
||||
makeSelectFirstBoardIdByProjectId,
|
||||
selectFirstBoardIdByProjectId,
|
||||
makeSelectNotificationsTotalByProjectId,
|
||||
selectNotificationsTotalByProjectId,
|
||||
makeSelectIsProjectWithIdAvailableForCurrentUser,
|
||||
selectIsProjectWithIdAvailableForCurrentUser,
|
||||
makeSelectIsProjectWithIdExternalAccessibleForCurrentUser,
|
||||
selectIsProjectWithIdExternalAccessibleForCurrentUser,
|
||||
selectCurrentProject,
|
||||
selectManagersForCurrentProject,
|
||||
selectBoardsForCurrentProject,
|
||||
selectManagerUserIdsForCurrentProject,
|
||||
selectBackgroundImageIdsForCurrentProject,
|
||||
selectBaseCustomFieldGroupIdsForCurrentProject,
|
||||
selectBaseCustomFieldGroupsForCurrentProject,
|
||||
selectBoardIdsForCurrentProject,
|
||||
selectIsCurrentUserManagerForCurrentProject,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user