Skip to content

Commit

Permalink
Filter cards with shortcuts to users and labels
Browse files Browse the repository at this point in the history
  • Loading branch information
emmguyot-adscom authored and emmguyot committed Apr 14, 2024
1 parent 92f7578 commit 4897252
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 1 deletion.
9 changes: 9 additions & 0 deletions client/src/actions/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ const handleCardDelete = (card) => ({
},
});

const filterText = (boardId, text) => ({
type: ActionTypes.TEXT_FILTER_IN_CURRENT_BOARD,
payload: {
boardId,
text,
},
});

export default {
createCard,
handleCardCreate,
Expand All @@ -129,4 +137,5 @@ export default {
duplicateCard,
deleteCard,
handleCardDelete,
filterText,
};
6 changes: 6 additions & 0 deletions client/src/components/BoardActions/BoardActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const BoardActions = React.memo(
labels,
filterUsers,
filterLabels,
filterText,
allUsers,
canEdit,
canEditMemberships,
Expand All @@ -27,6 +28,7 @@ const BoardActions = React.memo(
onLabelUpdate,
onLabelMove,
onLabelDelete,
onTextFilterUpdate,
}) => {
return (
<div className={styles.wrapper}>
Expand All @@ -46,6 +48,7 @@ const BoardActions = React.memo(
<Filters
users={filterUsers}
labels={filterLabels}
filterText={filterText}
allBoardMemberships={memberships}
allLabels={labels}
canEdit={canEdit}
Expand All @@ -57,6 +60,7 @@ const BoardActions = React.memo(
onLabelUpdate={onLabelUpdate}
onLabelMove={onLabelMove}
onLabelDelete={onLabelDelete}
onTextFilterUpdate={onTextFilterUpdate}
/>
</div>
</div>
Expand All @@ -71,6 +75,7 @@ BoardActions.propTypes = {
labels: PropTypes.array.isRequired,
filterUsers: PropTypes.array.isRequired,
filterLabels: PropTypes.array.isRequired,
filterText: PropTypes.string.isRequired,
allUsers: PropTypes.array.isRequired,
/* eslint-enable react/forbid-prop-types */
canEdit: PropTypes.bool.isRequired,
Expand All @@ -86,6 +91,7 @@ BoardActions.propTypes = {
onLabelUpdate: PropTypes.func.isRequired,
onLabelMove: PropTypes.func.isRequired,
onLabelDelete: PropTypes.func.isRequired,
onTextFilterUpdate: PropTypes.func.isRequired,
};

export default BoardActions;
24 changes: 23 additions & 1 deletion client/src/components/BoardActions/Filters.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useCallback } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';
import { usePopup } from '../../lib/popup';
import { Input } from '../../lib/custom-ui';

import User from '../User';
import Label from '../Label';
Expand All @@ -14,6 +15,7 @@ const Filters = React.memo(
({
users,
labels,
filterText,
allBoardMemberships,
allLabels,
canEdit,
Expand All @@ -25,8 +27,10 @@ const Filters = React.memo(
onLabelUpdate,
onLabelMove,
onLabelDelete,
onTextFilterUpdate,
}) => {
const [t] = useTranslation();
const searchField = useRef(null);

const handleRemoveUserClick = useCallback(
(id) => {
Expand All @@ -42,6 +46,14 @@ const Filters = React.memo(
[onLabelRemove],
);

const handleKeyUp = useCallback(() => {
onTextFilterUpdate(searchField.current.inputRef.current.value);
}, [onTextFilterUpdate]);

useEffect(() => {
searchField.current.inputRef.current.value = filterText;
}, [filterText]);

const BoardMembershipsPopup = usePopup(BoardMembershipsStep);
const LabelsPopup = usePopup(LabelsStep);

Expand Down Expand Up @@ -100,6 +112,14 @@ const Filters = React.memo(
</span>
))}
</span>
<span className={styles.filter}>
<Input
ref={searchField}
placeholder={t('common.searchCards')}
icon="search"
onKeyUp={() => handleKeyUp()}
/>
</span>
</>
);
},
Expand All @@ -109,6 +129,7 @@ Filters.propTypes = {
/* eslint-disable react/forbid-prop-types */
users: PropTypes.array.isRequired,
labels: PropTypes.array.isRequired,
filterText: PropTypes.string.isRequired,
allBoardMemberships: PropTypes.array.isRequired,
allLabels: PropTypes.array.isRequired,
/* eslint-enable react/forbid-prop-types */
Expand All @@ -121,6 +142,7 @@ Filters.propTypes = {
onLabelUpdate: PropTypes.func.isRequired,
onLabelMove: PropTypes.func.isRequired,
onLabelDelete: PropTypes.func.isRequired,
onTextFilterUpdate: PropTypes.func.isRequired,
};

export default Filters;
1 change: 1 addition & 0 deletions client/src/constants/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export default {
CARD_DELETE__SUCCESS: 'CARD_DELETE__SUCCESS',
CARD_DELETE__FAILURE: 'CARD_DELETE__FAILURE',
CARD_DELETE_HANDLE: 'CARD_DELETE_HANDLE',
TEXT_FILTER_IN_CURRENT_BOARD: 'TEXT_FILTER_IN_CURRENT_BOARD',

/* Tasks */

Expand Down
1 change: 1 addition & 0 deletions client/src/constants/EntryActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default {
CARD_DELETE: `${PREFIX}/CARD_DELETE`,
CURRENT_CARD_DELETE: `${PREFIX}/CURRENT_CARD_DELETE`,
CARD_DELETE_HANDLE: `${PREFIX}/CARD_DELETE_HANDLE`,
TEXT_FILTER_IN_CURRENT_BOARD: `${PREFIX}/FILTER_TEXT_HANDLE`,

/* Tasks */

Expand Down
3 changes: 3 additions & 0 deletions client/src/containers/BoardActionsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const mapStateToProps = (state) => {
const labels = selectors.selectLabelsForCurrentBoard(state);
const filterUsers = selectors.selectFilterUsersForCurrentBoard(state);
const filterLabels = selectors.selectFilterLabelsForCurrentBoard(state);
const filterText = selectors.selectFilterTextForCurrentBoard(state);
const currentUserMembership = selectors.selectCurrentUserMembershipForCurrentBoard(state);

const isCurrentUserEditor =
Expand All @@ -23,6 +24,7 @@ const mapStateToProps = (state) => {
labels,
filterUsers,
filterLabels,
filterText,
allUsers,
canEdit: isCurrentUserEditor,
canEditMemberships: isCurrentUserManager,
Expand All @@ -43,6 +45,7 @@ const mapDispatchToProps = (dispatch) =>
onLabelUpdate: entryActions.updateLabel,
onLabelMove: entryActions.moveLabel,
onLabelDelete: entryActions.deleteLabel,
onTextFilterUpdate: entryActions.filterText,
},
dispatch,
);
Expand Down
8 changes: 8 additions & 0 deletions client/src/entry-actions/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ const handleCardDelete = (card) => ({
},
});

const filterText = (text) => ({
type: EntryActionTypes.TEXT_FILTER_IN_CURRENT_BOARD,
payload: {
text,
},
});

export default {
createCard,
handleCardCreate,
Expand All @@ -120,4 +127,5 @@ export default {
deleteCard,
deleteCurrentCard,
handleCardDelete,
filterText,
};
1 change: 1 addition & 0 deletions client/src/locales/en/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default {
searchLabels: 'Search labels...',
searchMembers: 'Search members...',
searchUsers: 'Search users...',
searchCards: 'Search cards...',
seconds: 'Seconds',
selectBoard: 'Select board',
selectList: 'Select list',
Expand Down
4 changes: 4 additions & 0 deletions client/src/locales/fr/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export default {
project: 'Projet',
projectNotFound_title: 'Projet introuvable',
removeMember_title: 'Supprimer le membre',
searchLabels: 'Chercher une étiquette...',
searchMembers: 'Chercher un membre...',
searchUsers: 'Chercher un utilisateur...',
searchCards: 'Chercher une carte...',
seconds: 'Secondes',
selectBoard: 'Sélectionner une carte',
selectList: 'Sélectionner une liste',
Expand Down
46 changes: 46 additions & 0 deletions client/src/models/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { attr, fk, many } from 'redux-orm';
import BaseModel from './BaseModel';
import ActionTypes from '../constants/ActionTypes';

import User from './User';
import Label from './Label';

export default class extends BaseModel {
static modelName = 'Board';

Expand All @@ -25,6 +28,9 @@ export default class extends BaseModel {
}),
filterUsers: many('User', 'filterBoards'),
filterLabels: many('Label', 'filterBoards'),
filterText: attr({
getDefault: () => '',
}),
};

static reducer({ type, payload }, Board) {
Expand Down Expand Up @@ -167,6 +173,46 @@ export default class extends BaseModel {
Board.withId(payload.boardId).filterLabels.remove(payload.id);

break;
case ActionTypes.TEXT_FILTER_IN_CURRENT_BOARD: {
const board = Board.withId(payload.boardId);
let filterText = payload.text;

// Shortcut to user filters
const posAT = filterText.indexOf('@');
if (posAT >= 0 && posAT < filterText.length - 1) {
const userId = User.findUsersFromText(
filterText.substring(posAT + 1),
board.memberships.toModelArray().map((membership) => membership.user),
);
if (
userId &&
board.filterUsers.toModelArray().filter((user) => user.id === userId).length === 0
) {
board.filterUsers.add(userId);
filterText = filterText.substring(0, posAT);
}
}

// Shortcut to label filters
const posSharp = filterText.indexOf('#');
if (posSharp >= 0 && posSharp < filterText.length - 1) {
const labelId = Label.findLabelsFromText(
filterText.substring(posSharp + 1),
board.labels.toModelArray(),
);
if (
labelId &&
board.filterLabels.toModelArray().filter((label) => label.id === labelId).length === 0
) {
board.filterLabels.add(labelId);
filterText = filterText.substring(0, posSharp);
}
}

board.update({ filterText });

break;
}
default:
}
}
Expand Down
12 changes: 12 additions & 0 deletions client/src/models/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ export default class extends BaseModel {
default:
}
}

static findLabelsFromText(filterText, labels) {
const selectLabel = filterText.toLocaleLowerCase();
const matchingLabels = labels.filter((label) =>
label.name.toLocaleLowerCase().startsWith(selectLabel),
);
if (matchingLabels.length === 1) {
// Appens the user to the filter
return matchingLabels[0].id;
}
return null;
}
}
17 changes: 17 additions & 0 deletions client/src/models/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ export default class extends BaseModel {
getFilteredOrderedCardsModelArray() {
let cardModels = this.getOrderedCardsQuerySet().toModelArray();

const { filterText } = this.board;

if (filterText !== '') {
let re = null;
if (filterText.startsWith('/')) {
re = new RegExp(filterText.substring(1), 'i');
}
if (re) {
cardModels = cardModels.filter((cardModel) => re.test(cardModel.name));
} else {
const lowerCasedFilter = filterText.toLocaleLowerCase();
cardModels = cardModels.filter(
(cardModel) => cardModel.name.toLocaleLowerCase().indexOf(lowerCasedFilter) >= 0,
);
}
}

const filterUserIds = this.board.filterUsers.toRefArray().map((user) => user.id);
const filterLabelIds = this.board.filterLabels.toRefArray().map((label) => label.id);

Expand Down
14 changes: 14 additions & 0 deletions client/src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,18 @@ export default class extends BaseModel {
},
);
}

static findUsersFromText(filterText, users) {
const selectUser = filterText.toLocaleLowerCase();
const matchingUsers = users.filter(
(user) =>
user.name.toLocaleLowerCase().startsWith(selectUser) ||
user.username.toLocaleLowerCase().startsWith(selectUser),
);
if (matchingUsers.length === 1) {
// Appens the user to the filter
return matchingUsers[0].id;
}
return null;
}
}
7 changes: 7 additions & 0 deletions client/src/sagas/core/services/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ export function* handleCardDelete(card) {
yield put(actions.handleCardDelete(card));
}

export function* handleTextFilter(text) {
const { boardId } = yield select(selectors.selectPath);

yield put(actions.filterText(boardId, text));
}

export default {
createCard,
handleCardCreate,
Expand All @@ -222,4 +228,5 @@ export default {
deleteCard,
deleteCurrentCard,
handleCardDelete,
handleTextFilter,
};
3 changes: 3 additions & 0 deletions client/src/sagas/core/watchers/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ export default function* cardsWatchers() {
takeEvery(EntryActionTypes.CARD_DELETE_HANDLE, ({ payload: { card } }) =>
services.handleCardDelete(card),
),
takeEvery(EntryActionTypes.TEXT_FILTER_IN_CURRENT_BOARD, ({ payload: { text } }) =>
services.handleTextFilter(text),
),
]);
}

0 comments on commit 4897252

Please sign in to comment.