Skip to content

Commit

Permalink
Merge pull request #508 from cofacts/article-type-filter
Browse files Browse the repository at this point in the history
Article type filter for Article list, Reply list and Profile page
  • Loading branch information
MrOrz authored Oct 12, 2022
2 parents 5c9e52d + 07c3820 commit b3f0bc8
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 75 deletions.
48 changes: 48 additions & 0 deletions components/ListPageControls/ArticleTypeFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { memo } from 'react';
import { useRouter } from 'next/router';
import { t } from 'ttag';
import BaseFilter from './BaseFilter';
import { goToUrlQueryAndResetPagination } from 'lib/listPage';

/**
* URL param name to read from and write to
*/
const PARAM_NAME = 'articleTypes';

const OPTIONS = [
{ value: 'TEXT', label: t`Text` },
{ value: 'IMAGE', label: t`Image` },
{ value: 'VIDEO', label: t`Video` },
{ value: 'AUDIO', label: t`Audio` },
];

/**
* @param {object} query - query from router
* @returns {Arary<keyof TYPE_NAME>} list of selected reply types; see constants/replyType for all possible values
*/
function getValues(query) {
return query[PARAM_NAME] ? query[PARAM_NAME].split(',') : [];
}

function ArticleTypeFilter() {
const { query } = useRouter();
const selectedValues = getValues(query);

return (
<BaseFilter
title={t`Format`}
selected={selectedValues}
options={OPTIONS}
onChange={selected =>
goToUrlQueryAndResetPagination({
...query,
[PARAM_NAME]: selected.join(','),
})
}
/>
);
}

const MemoizedArticleTypeFilter = memo(ArticleTypeFilter);
MemoizedArticleTypeFilter.getValues = getValues;
export default MemoizedArticleTypeFilter;
2 changes: 2 additions & 0 deletions components/ListPageControls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Filters from './Filters';
import BaseFilter from './BaseFilter';
import ArticleStatusFilter from './ArticleStatusFilter';
import CategoryFilter from './CategoryFilter';
import ArticleTypeFilter from './ArticleTypeFilter';
import ReplyTypeFilter from './ReplyTypeFilter';
import TimeRange from './TimeRange';
import SortInput from './SortInput';
Expand All @@ -14,6 +15,7 @@ export {
BaseFilter,
ArticleStatusFilter,
CategoryFilter,
ArticleTypeFilter,
ReplyTypeFilter,
TimeRange,
SortInput,
Expand Down
5 changes: 5 additions & 0 deletions components/ProfilePage/RepliedArticleTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Tools,
Filters,
CategoryFilter,
ArticleTypeFilter,
ReplyTypeFilter,
TimeRange,
SortInput,
Expand Down Expand Up @@ -187,6 +188,9 @@ function urlQuery2Filter(query = {}, userId) {
};
}

const articleTypes = ArticleTypeFilter.getValues(query);
if (articleTypes.length) filterObj.articleTypes = articleTypes;

const selectedReplyTypes = ReplyTypeFilter.getValues(query);
if (selectedReplyTypes.length)
filterObj.articleReply.replyTypes = selectedReplyTypes;
Expand Down Expand Up @@ -238,6 +242,7 @@ function RepliedArticleTab({ userId }) {
<SortInput defaultOrderBy={DEFAULT_ORDER} options={REPLIES_ORDER} />
</Tools>
<Filters className={classes.filters}>
<ArticleTypeFilter />
<ReplyTypeFilter />
<CategoryFilter />
</Filters>
Expand Down
Loading

0 comments on commit b3f0bc8

Please sign in to comment.