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

#245: add comments filter #246

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/features/auth/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const MANAGE_USERS_ROLES = [MANAGER];
export const WRITE_PROJECT_ROLES = [MANAGER];
export const READ_COMMENT_ROLES = [MANAGER, MEMBER];
export const WRITE_COMMENT_ROLES = [MANAGER, MEMBER];
export const QUERY_COMMENTS_ROLES = [MANAGER, MEMBER];

export const hasRole = (currRoles, targetRoles = []) =>
currRoles && (currRoles.includes(SUPER_USER) || currRoles.some((role) => targetRoles.includes(role)));
63 changes: 63 additions & 0 deletions src/features/filters/CommentsFilter.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import Accordion from '../../components/Accordion';
import { Formik, Form, Field, ErrorMessage } from 'formik';
import { FormWrapper, FormError, FieldRow, ButtonRow } from '../../components/Form.jsx';
import { styled } from '../../theme/stitches.config.js';
import Button from '../../components/Button.jsx';
import { useDispatch, useSelector } from 'react-redux';
import { commentsFilterChanged, selectCommentsFilter } from './filtersSlice.js';

const StyledFieldRow = styled(FieldRow, {
display: 'block',
});

const StyledButtonRow = styled(ButtonRow, {
paddingBottom: '$0',
});

export const CommentsFilter = () => {
const commentsFilter = useSelector(selectCommentsFilter);
const dispatch = useDispatch();

const handleRemoveButtonClick = () => dispatch(commentsFilterChanged(null));

const handleCommentsFilterSubmit = (values) => {
dispatch(commentsFilterChanged(values.filter));
};

return (
<Accordion label="Comments" expandedDefault={false}>
<div>
<FormWrapper>
<Formik
enableReinitialize
initialValues={{ filter: commentsFilter || '' }}
onSubmit={(values) => handleCommentsFilterSubmit(values)}
>
<Form>
<StyledFieldRow>
<Field
name="filter"
id="filter"
component="textarea"
placeholder="Comments with the phrase..."
onKeyDown={(e) => e.stopPropagation()}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a 'bug' where keyboard navigation would work even when entering text in the text area. The reproduction steps are

  1. Open the comments popover
  2. Create a new comment
  3. Type in the comment filter text area
  4. Check that keyboard navigation is working

onKeyDownCapture={(e) => e.stopPropagation()}
/>
<ErrorMessage component={FormError} name="filter" />
</StyledFieldRow>
<StyledButtonRow>
<Button type="button" size="small" onClick={handleRemoveButtonClick}>
Remove
</Button>
<Button type="submit" size="small">
Apply
</Button>
</StyledButtonRow>
</Form>
</Formik>
</FormWrapper>
</div>
</Accordion>
);
}
8 changes: 6 additions & 2 deletions src/features/filters/FiltersPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { useSelector } from 'react-redux';
import { styled } from '../../theme/stitches.config.js';
import { selectUserCurrentRoles } from '../auth/authSlice.js';
import { hasRole, QUERY_WITH_CUSTOM_FILTER } from '../auth/roles.js';
import { hasRole, QUERY_COMMENTS_ROLES, QUERY_WITH_CUSTOM_FILTER } from '../auth/roles.js';
import PanelHeader from '../../components/PanelHeader.jsx';
import StyledScrollArea from '../../components/ScrollArea.jsx';
import DeploymentFilter from './DeploymentFilter.jsx';
Expand All @@ -11,6 +11,7 @@ import DateFilter from './DateFilter.jsx';
import LabelFilter from './LabelFilter.jsx';
import CustomFilter from './CustomFilter.jsx';
import FiltersPanelFooter from './FiltersPanelFooter.jsx';
import { CommentsFilter } from './CommentsFilter.jsx';


const PanelBody = styled('div', {
Expand Down Expand Up @@ -49,6 +50,9 @@ const FiltersPanel = ({ toggleFiltersPanel }) => {
<ReviewFilter/>
<DateFilter type='created'/>
<DateFilter type='added'/>
{hasRole(userRoles, QUERY_COMMENTS_ROLES) &&
<CommentsFilter />
}
{hasRole(userRoles, QUERY_WITH_CUSTOM_FILTER) &&
<CustomFilter />
}
Expand All @@ -59,4 +63,4 @@ const FiltersPanel = ({ toggleFiltersPanel }) => {
);
}

export default FiltersPanel;
export default FiltersPanel;
7 changes: 7 additions & 0 deletions src/features/filters/filtersSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const initialState = {
addedEnd: null,
reviewed: null,
custom: null,
comments: null,
},
};

Expand Down Expand Up @@ -66,6 +67,10 @@ export const filtersSlice = createSlice({
state.activeFilters.custom = payload;
},

commentsFilterChanged: (state, { payload }) => {
state.activeFilters.comments = payload;
},

dateFilterChanged: (state, { payload }) => {
state.activeFilters[payload.type + 'Start'] = payload.startDate;
state.activeFilters[payload.type + 'End'] = payload.endDate;
Expand Down Expand Up @@ -188,6 +193,7 @@ export const {
setActiveFilters,
bulkSelectToggled,
checkboxOnlyButtonClicked,
commentsFilterChanged,
} = filtersSlice.actions;

// Selectors
Expand All @@ -206,5 +212,6 @@ export const selectDateCreatedFilter = (state) => ({
start: state.filters.activeFilters.createdStart,
end: state.filters.activeFilters.createdEnd,
});
export const selectCommentsFilter = (state) => state.filters.activeFilters.comments;

export default filtersSlice.reducer;
2 changes: 2 additions & 0 deletions src/features/projects/projectsMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
reviewedFilterToggled,
selectActiveFilters,
customFilterChanged,
commentsFilterChanged,
checkboxOnlyButtonClicked,
} from '../filters/filtersSlice';
import {
Expand Down Expand Up @@ -72,6 +73,7 @@ export const diffFilters = (store) => (next) => (action) => {
dateFilterChanged.match(action) ||
reviewedFilterToggled.match(action) ||
customFilterChanged.match(action) ||
commentsFilterChanged.match(action) ||
setSelectedProjAndView.match(action) ||
editDeploymentsSuccess.match(action) ||
saveViewSuccess.match(action) ||
Expand Down
Loading