Files
planka/client/src/selectors/router.js

84 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
import { createSelector as createReselectSelector } from 'reselect';
import { createSelector as createReduxOrmSelector } from 'redux-orm';
import orm from '../orm';
2022-08-04 13:31:14 +02:00
import { selectCurrentUserId } from './users';
2019-08-31 04:07:25 +05:00
import matchPaths from '../utils/match-paths';
import Paths from '../constants/Paths';
2022-08-04 13:31:14 +02:00
export const selectPathname = ({
2019-08-31 04:07:25 +05:00
router: {
location: { pathname },
},
}) => pathname;
2022-08-04 13:31:14 +02:00
export const selectPathsMatch = createReselectSelector(selectPathname, (pathname) =>
matchPaths(pathname, Object.values(Paths)),
);
2019-08-31 04:07:25 +05:00
2022-08-04 13:31:14 +02:00
export const selectPath = createReduxOrmSelector(
2019-08-31 04:07:25 +05:00
orm,
2022-08-04 13:31:14 +02:00
selectPathsMatch,
(state) => selectCurrentUserId(state),
({ Project, Board, Card }, pathsMatch, currentUserId) => {
2019-08-31 04:07:25 +05:00
if (pathsMatch) {
2022-11-21 00:54:05 +01:00
switch (pathsMatch.pattern.path) {
2019-08-31 04:07:25 +05:00
case Paths.PROJECTS: {
const projectModel = Project.withId(pathsMatch.params.id);
2022-12-26 21:10:50 +01:00
if (!projectModel || !projectModel.isAvailableForUser(currentUserId)) {
return {
projectId: null,
};
}
2019-08-31 04:07:25 +05:00
return {
projectId: projectModel.id,
2019-08-31 04:07:25 +05:00
};
}
case Paths.BOARDS: {
const boardModel = Board.withId(pathsMatch.params.id);
2022-12-26 21:10:50 +01:00
if (!boardModel || !boardModel.isAvailableForUser(currentUserId)) {
2019-08-31 04:07:25 +05:00
return {
boardId: null,
projectId: null,
};
}
return {
boardId: boardModel.id,
2022-12-26 21:10:50 +01:00
projectId: boardModel.projectId,
2019-08-31 04:07:25 +05:00
};
}
case Paths.CARDS: {
const cardModel = Card.withId(pathsMatch.params.id);
2022-12-26 21:10:50 +01:00
if (!cardModel || !cardModel.isAvailableForUser(currentUserId)) {
2019-08-31 04:07:25 +05:00
return {
cardId: null,
boardId: null,
projectId: null,
};
}
return {
cardId: cardModel.id,
2022-12-26 21:10:50 +01:00
boardId: cardModel.boardId,
projectId: cardModel.board.projectId,
2019-08-31 04:07:25 +05:00
};
}
default:
}
}
return {};
},
);
2022-08-04 13:31:14 +02:00
export default {
selectPathname,
selectPathsMatch,
selectPath,
};