generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Sorted filter checkboxes in alphabet order in materials page #516
Merged
NabokinAlexandr
merged 4 commits into
develop
from
Sort-filter-checkboxes-in-alphabet-order-in-Materials-page
Aug 12, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4c42d3c
bug fix
NabokinAlexandr 625d714
Merge branch 'develop' into Sort-filter-checkboxes-in-alphabet-order-…
NabokinAlexandr 26624eb
removed sorting from origins and post types
NabokinAlexandr 4580a23
fixed according to comments in review
NabokinAlexandr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IPostType, IOrigin, IDirection } from '../types'; | ||
|
||
export const sortByAlphabet = ( | ||
array: IDirection[], | ||
sortBy: string | undefined = 'name', | ||
): IDirection[] => { | ||
return array?.sort((prev, current) => { | ||
if (prev[sortBy] > current[sortBy]) { | ||
return 1; | ||
} | ||
if (prev[sortBy] < current[sortBy]) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-shadow */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import { Box, Grid, Typography } from '@material-ui/core'; | ||
import { isEmpty, uniq } from 'lodash'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
@@ -50,6 +51,7 @@ import { | |
updateOrig, | ||
updateDir, | ||
} from '../../utilities/utilityFunctions'; | ||
import { sortByAlphabet } from '../../../lib/utilities/sorting'; | ||
|
||
const MaterialsView: React.FC = () => { | ||
const { t } = useTranslation(); | ||
|
@@ -79,57 +81,53 @@ const MaterialsView: React.FC = () => { | |
const materials = selectPostsByIds(postIds); | ||
const origins = useSelector(selectOrigins); | ||
const originsInPlural: IOrigin[] = []; | ||
|
||
// here we translate origins and put them in proper order | ||
if (origins.length) { | ||
const el1: IOrigin = { ...origins[0] }; | ||
const el2: IOrigin = { ...origins[1] }; | ||
const el3: IOrigin = { ...origins[2] }; | ||
|
||
Object.defineProperty(el1, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
value: `${t(langTokens.experts.expertOpinion, defaultPlural)}`, | ||
}); | ||
Object.defineProperty(el3, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
value: `${t(langTokens.common.translation, defaultPlural)}`, | ||
const translatedOrigins = origins.map((origin) => { | ||
const translations = { | ||
'1': `${t(langTokens.experts.expertOpinion, defaultPlural)}`, | ||
'3': `${t(langTokens.common.translation, defaultPlural)}`, | ||
}; | ||
const translatedOrigin = { ...origin }; | ||
translatedOrigin.name = translations[origin.id] || origin.name; | ||
return translatedOrigin; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code is completely unreadable. Using I suggest at least leaving a comment describing what this does and leaving a |
||
}); | ||
|
||
originsInPlural.push(el1, el3, el2); | ||
originsInPlural.push( | ||
translatedOrigins[0], | ||
translatedOrigins[2], | ||
translatedOrigins[1], | ||
); | ||
} | ||
const postTypes = useSelector(selectPostTypes); | ||
const postTypesInPlural: IPostType[] = []; | ||
// here we translate types and put them in proper order | ||
if (postTypes.length) { | ||
const el1: IPostType = { ...postTypes[0] }; | ||
const el2: IPostType = { ...postTypes[1] }; | ||
const el3: IPostType = { ...postTypes[2] }; | ||
|
||
Object.defineProperty(el1, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
value: `${t(langTokens.common.article_1, defaultPlural)}`, | ||
}); | ||
Object.defineProperty(el2, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
value: `${t(langTokens.common.video)}`, | ||
}); | ||
Object.defineProperty(el3, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
value: `${t(langTokens.common.post, defaultPlural)}`, | ||
const translatedPostTypes = postTypes.map((postType) => { | ||
const translations = { | ||
'1': `${t(langTokens.common.article_1, defaultPlural)}`, | ||
'2': `${t(langTokens.common.video)}`, | ||
'3': `${t(langTokens.common.post, defaultPlural)}`, | ||
}; | ||
const translatedPostType = { ...postType }; | ||
translatedPostType.name = translations[postType.id] || postType.name; | ||
return translatedPostType; | ||
}); | ||
|
||
postTypesInPlural.push(el1, el3, el2); | ||
postTypesInPlural.push( | ||
translatedPostTypes[0], | ||
translatedPostTypes[2], | ||
translatedPostTypes[1], | ||
); | ||
} | ||
|
||
const directions = useSelector(selectDirections); | ||
const directionsInPlural: IDirection[] = []; | ||
// here we sorted directions in alphabet order | ||
if (directions.length) { | ||
const directionsToSort = directions.map((direction) => ({ ...direction })); | ||
const sortedDirections = sortByAlphabet(directionsToSort, 'label'); | ||
directionsInPlural.push(...sortedDirections); | ||
} | ||
|
||
const propertiesLoaded = | ||
!isEmpty(postTypes) && !isEmpty(directions) && !isEmpty(origins); | ||
|
@@ -195,7 +193,6 @@ const MaterialsView: React.FC = () => { | |
const queryType = getQueryTypeByFilterType(filterType); | ||
const checkedIds = Object.keys(checked).filter((key) => checked[key]); | ||
const isQuerySame = uniq(Object.values(checked)).length === 1; // removing the query if user checks/unchecks the last box | ||
|
||
query.set(queryType, checkedIds.join(',')); | ||
if (!checkedIds.length || isQuerySame) { | ||
query.delete(queryType); | ||
|
@@ -264,7 +261,7 @@ const MaterialsView: React.FC = () => { | |
|
||
let selectedDirections: | ||
| IDirection[] | ||
| filtersStateEnum = directions?.filter((direction) => | ||
| filtersStateEnum = directionsInPlural?.filter((direction) => | ||
selectedDirectionsString?.includes(direction.id.toString()), | ||
); | ||
|
||
|
@@ -380,7 +377,6 @@ const MaterialsView: React.FC = () => { | |
chipsListType: ChipFilterType | undefined, | ||
) => { | ||
let filtersUpdatedByChips: undefined | CheckboxFormStateType; | ||
|
||
if (chipsListType === 'ORIGIN') { | ||
filtersUpdatedByChips = { ...checkedFiltersOrigins }; | ||
} else if (chipsListType === 'POST_TYPE') { | ||
|
@@ -509,7 +505,7 @@ const MaterialsView: React.FC = () => { | |
onFormChange={(checked) => | ||
setFilters(checked, FilterTypeEnum.DIRECTIONS) | ||
} | ||
possibleFilters={directions} | ||
possibleFilters={directionsInPlural} | ||
selectedFilters={selectedDirections} | ||
filterTitle={t(langTokens.common.byDirection).toLowerCase()} | ||
allTitle={t(langTokens.common.allDirections)} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.