+273
-31
@@ -1,14 +1,22 @@
|
||||
/*!
|
||||
* 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';
|
||||
import { selectPath } from './router';
|
||||
import { selectCurrentUserId } from './users';
|
||||
import { isLocalId } from '../utils/local-id';
|
||||
import { isListArchiveOrTrash } from '../utils/record-helpers';
|
||||
import { ListTypes } from '../constants/Enums';
|
||||
|
||||
export const makeSelectBoardById = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
@@ -16,12 +24,98 @@ export const makeSelectBoardById = () =>
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.ref;
|
||||
return {
|
||||
...boardModel.ref,
|
||||
isPersisted: !isLocalId(boardModel.id),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
export const selectBoardById = makeSelectBoardById();
|
||||
|
||||
export const makeSelectCurrentUserMembershipByBoardId = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Board }, id, currentUserId) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
const boardMembershipModel = boardModel.getMembershipModelByUserId(currentUserId);
|
||||
|
||||
if (!boardMembershipModel) {
|
||||
return boardMembershipModel;
|
||||
}
|
||||
|
||||
return boardMembershipModel.ref;
|
||||
},
|
||||
);
|
||||
|
||||
const selectCurrentUserMembershipByBoardId = makeSelectCurrentUserMembershipByBoardId();
|
||||
|
||||
export const makeSelectNotificationsTotalByBoardId = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Board }, id) => {
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.getUnreadNotificationsQuerySet().count();
|
||||
},
|
||||
);
|
||||
|
||||
export const selectNotificationsTotalByBoardId = makeSelectNotificationsTotalByBoardId();
|
||||
|
||||
export const makeSelectNotificationServiceIdsByBoardId = () =>
|
||||
createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Board }, id) => {
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel
|
||||
.getNotificationServicesQuerySet()
|
||||
.toRefArray()
|
||||
.map((notificationService) => notificationService.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectNotificationServiceIdsByBoardId = makeSelectNotificationServiceIdsByBoardId();
|
||||
|
||||
export const selectIsBoardWithIdAvailableForCurrentUser = createSelector(
|
||||
orm,
|
||||
(_, id) => id,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Board, User }, id, currentUserId) => {
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentUserModel = User.withId(currentUserId);
|
||||
return boardModel.isAvailableForUser(currentUserModel);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
@@ -43,8 +137,7 @@ export const selectCurrentBoard = createSelector(
|
||||
export const selectMembershipsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
(state) => selectCurrentUserId(state),
|
||||
({ Board }, id, currentUserId) => {
|
||||
({ Board }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
@@ -55,14 +148,35 @@ export const selectMembershipsForCurrentBoard = createSelector(
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.getOrderedMembershipsModelArray().map((boardMembershipModel) => ({
|
||||
...boardMembershipModel.ref,
|
||||
isPersisted: !isLocalId(boardMembershipModel.id),
|
||||
user: {
|
||||
...boardMembershipModel.user.ref,
|
||||
isCurrent: boardMembershipModel.user.id === currentUserId,
|
||||
},
|
||||
}));
|
||||
return boardModel
|
||||
.getMembershipsQuerySet()
|
||||
.toModelArray()
|
||||
.map((boardMembershipModel) => ({
|
||||
...boardMembershipModel.ref,
|
||||
isPersisted: !isLocalId(boardMembershipModel.id),
|
||||
user: boardMembershipModel.user.ref,
|
||||
}));
|
||||
},
|
||||
);
|
||||
|
||||
export const selectMemberUserIdsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel
|
||||
.getMembershipsQuerySet()
|
||||
.toModelArray()
|
||||
.map((boardMembershipModel) => boardMembershipModel.user.id);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -81,7 +195,7 @@ export const selectCurrentUserMembershipForCurrentBoard = createSelector(
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
const boardMembershipModel = boardModel.getMembershipModelForUser(currentUserId);
|
||||
const boardMembershipModel = boardModel.getMembershipModelByUserId(currentUserId);
|
||||
|
||||
if (!boardMembershipModel) {
|
||||
return boardMembershipModel;
|
||||
@@ -105,17 +219,59 @@ export const selectLabelsForCurrentBoard = createSelector(
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel
|
||||
.getOrderedLabelsQuerySet()
|
||||
.toRefArray()
|
||||
.map((label) => ({
|
||||
...label,
|
||||
isPersisted: !isLocalId(label.id),
|
||||
}));
|
||||
return boardModel.getLabelsQuerySet().toRefArray();
|
||||
},
|
||||
);
|
||||
|
||||
export const selectListIdsForCurrentBoard = createSelector(
|
||||
export const selectArchiveListIdForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
const listModel = boardModel.lists
|
||||
.filter({
|
||||
type: ListTypes.ARCHIVE,
|
||||
})
|
||||
.first();
|
||||
|
||||
return listModel.id;
|
||||
},
|
||||
);
|
||||
|
||||
export const selectTrashListIdForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
const listModel = boardModel.lists
|
||||
.filter({
|
||||
type: ListTypes.TRASH,
|
||||
})
|
||||
.first();
|
||||
|
||||
return listModel.id;
|
||||
},
|
||||
);
|
||||
|
||||
export const selectFiniteListIdsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
@@ -130,13 +286,14 @@ export const selectListIdsForCurrentBoard = createSelector(
|
||||
}
|
||||
|
||||
return boardModel
|
||||
.getOrderedListsQuerySet()
|
||||
.getFiniteListsQuerySet()
|
||||
.toRefArray()
|
||||
.map((list) => list.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectFilterUsersForCurrentBoard = createSelector(
|
||||
// TODO: rename?
|
||||
export const selectAvailableListsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
@@ -150,11 +307,14 @@ export const selectFilterUsersForCurrentBoard = createSelector(
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.filterUsers.toRefArray();
|
||||
return boardModel
|
||||
.getListsQuerySet()
|
||||
.toRefArray()
|
||||
.filter((list) => !isListArchiveOrTrash(list));
|
||||
},
|
||||
);
|
||||
|
||||
export const selectFilterLabelsForCurrentBoard = createSelector(
|
||||
export const selectFilteredCardIdsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
@@ -168,11 +328,11 @@ export const selectFilterLabelsForCurrentBoard = createSelector(
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.filterLabels.toRefArray();
|
||||
return boardModel.getFilteredCardsModelArray().map((cardModel) => cardModel.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectFilterTextForCurrentBoard = createSelector(
|
||||
export const selectCustomFieldGroupIdsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
@@ -186,7 +346,76 @@ export const selectFilterTextForCurrentBoard = createSelector(
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.filterText;
|
||||
return boardModel
|
||||
.getCustomFieldGroupsQuerySet()
|
||||
.toRefArray()
|
||||
.map((customFieldGroup) => customFieldGroup.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectCustomFieldGroupsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel
|
||||
.getCustomFieldGroupsQuerySet()
|
||||
.toModelArray()
|
||||
.map((customFieldGroupModel) => {
|
||||
if (!customFieldGroupModel.name) {
|
||||
return {
|
||||
...customFieldGroupModel.ref,
|
||||
name: customFieldGroupModel.baseCustomFieldGroup.name,
|
||||
};
|
||||
}
|
||||
|
||||
return customFieldGroupModel.ref;
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
export const selectFilterUserIdsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.filterUsers.toRefArray().map((user) => user.id);
|
||||
},
|
||||
);
|
||||
|
||||
export const selectFilterLabelIdsForCurrentBoard = createSelector(
|
||||
orm,
|
||||
(state) => selectPath(state).boardId,
|
||||
({ Board }, id) => {
|
||||
if (!id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
const boardModel = Board.withId(id);
|
||||
|
||||
if (!boardModel) {
|
||||
return boardModel;
|
||||
}
|
||||
|
||||
return boardModel.filterLabels.toRefArray().map((label) => label.id);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -199,13 +428,26 @@ export const selectIsBoardWithIdExists = createSelector(
|
||||
export default {
|
||||
makeSelectBoardById,
|
||||
selectBoardById,
|
||||
makeSelectCurrentUserMembershipByBoardId,
|
||||
selectCurrentUserMembershipByBoardId,
|
||||
makeSelectNotificationsTotalByBoardId,
|
||||
selectNotificationsTotalByBoardId,
|
||||
makeSelectNotificationServiceIdsByBoardId,
|
||||
selectNotificationServiceIdsByBoardId,
|
||||
selectIsBoardWithIdAvailableForCurrentUser,
|
||||
selectCurrentBoard,
|
||||
selectMembershipsForCurrentBoard,
|
||||
selectMemberUserIdsForCurrentBoard,
|
||||
selectCurrentUserMembershipForCurrentBoard,
|
||||
selectLabelsForCurrentBoard,
|
||||
selectListIdsForCurrentBoard,
|
||||
selectFilterUsersForCurrentBoard,
|
||||
selectFilterLabelsForCurrentBoard,
|
||||
selectFilterTextForCurrentBoard,
|
||||
selectArchiveListIdForCurrentBoard,
|
||||
selectTrashListIdForCurrentBoard,
|
||||
selectFiniteListIdsForCurrentBoard,
|
||||
selectAvailableListsForCurrentBoard,
|
||||
selectFilteredCardIdsForCurrentBoard,
|
||||
selectCustomFieldGroupIdsForCurrentBoard,
|
||||
selectCustomFieldGroupsForCurrentBoard,
|
||||
selectFilterUserIdsForCurrentBoard,
|
||||
selectFilterLabelIdsForCurrentBoard,
|
||||
selectIsBoardWithIdExists,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user