From 5329d261659277cb3433775eab53a5d68d7e7202 Mon Sep 17 00:00:00 2001 From: Alexandre Chau Date: Thu, 8 Jul 2021 19:04:39 +0200 Subject: [PATCH] feat: move select to right, refactor to functional component, add missing translation --- src/components/main/ItemsGrid.js | 178 ++++++++++++++----------------- src/langs/en.json | 1 + src/langs/fr.json | 1 + 3 files changed, 85 insertions(+), 95 deletions(-) diff --git a/src/components/main/ItemsGrid.js b/src/components/main/ItemsGrid.js index 9f183cca0..3baac086a 100644 --- a/src/components/main/ItemsGrid.js +++ b/src/components/main/ItemsGrid.js @@ -1,14 +1,13 @@ -import { Box } from '@material-ui/core'; -import FormControl from '@material-ui/core/FormControl'; +import { Box, Typography } from '@material-ui/core'; import Grid from '@material-ui/core/Grid'; -import InputLabel from '@material-ui/core/InputLabel'; import MenuItem from '@material-ui/core/MenuItem'; import Select from '@material-ui/core/Select'; import { withStyles } from '@material-ui/core/styles'; import Pagination from '@material-ui/lab/Pagination'; import { List } from 'immutable'; import PropTypes from 'prop-types'; -import React, { Component } from 'react'; +import React, { useState } from 'react'; +import { useTranslation } from 'react-i18next'; import { withRouter } from 'react-router'; import { GRID_ITEMS_PER_PAGE_CHOICES } from '../../config/constants'; import { @@ -30,10 +29,15 @@ const styles = (theme) => ({ }, paginationControls: { padding: theme.spacing(2), + alignItems: 'center', }, itemsPerPageSelect: { position: 'absolute', - left: theme.spacing(1), + right: theme.spacing(2), + alignItems: 'center', + }, + itemsPerPageLabel: { + paddingRight: theme.spacing(1), }, formControl: { margin: theme.spacing(1), @@ -41,51 +45,23 @@ const styles = (theme) => ({ }, }); -class ItemsGrid extends Component { - static propTypes = { - items: PropTypes.instanceOf(List).isRequired, - match: PropTypes.shape({ - params: PropTypes.shape({ itemId: PropTypes.string }).isRequired, - }).isRequired, - classes: PropTypes.shape({ - title: PropTypes.string.isRequired, - empty: PropTypes.string.isRequired, - paginationControls: PropTypes.string.isRequired, - formControl: PropTypes.string.isRequired, - itemsPerPageSelect: PropTypes.string.isRequired, - }).isRequired, - title: PropTypes.string.isRequired, - itemSearch: PropTypes.shape({ - text: PropTypes.string, - input: PropTypes.instanceOf(ItemSearchInput), - }), - }; +const ItemsGrid = (props) => { + const { classes, items, title, itemSearch } = props; - static defaultProps = { - itemSearch: null, - }; + const { t } = useTranslation(); + const [page, setPage] = useState(1); + const [itemsPerPage, setItemsPerPage] = useState( + GRID_ITEMS_PER_PAGE_CHOICES[0], + ); - state = { - page: 1, - itemsPerPage: GRID_ITEMS_PER_PAGE_CHOICES[0], - }; + const pagesCount = Math.ceil(items.size / itemsPerPage); - handleItemsPerPage = (event) => { - this.setState({ - itemsPerPage: event.target.value, - }); - }; + const start = (page - 1) * itemsPerPage; + const end = start + itemsPerPage; + const itemsInPage = items.slice(start, end); - handlePagination = (event, value) => { - this.setState({ - page: value, - }); - }; - - renderItems = (items) => { - const { classes, itemSearch } = this.props; - - if (!items || !items.size) { + const renderItems = () => { + if (!itemsInPage || !itemsInPage.size) { return itemSearch && itemSearch.text ? (
@@ -97,63 +73,75 @@ class ItemsGrid extends Component { ); } - return items.map((item) => ( + return itemsInPage.map((item) => ( )); }; - render() { - const { classes, items, title, itemSearch } = this.props; - const { page, itemsPerPage } = this.state; + return ( +
+ + + {renderItems()} + + + + + {t('Items per page')} + + + + setPage(v)} + className={classes.paginationControls} + /> + +
+ ); +}; - const pagesCount = Math.ceil(items.size / itemsPerPage); +ItemsGrid.propTypes = { + items: PropTypes.instanceOf(List).isRequired, + match: PropTypes.shape({ + params: PropTypes.shape({ itemId: PropTypes.string }).isRequired, + }).isRequired, + classes: PropTypes.shape({ + title: PropTypes.string.isRequired, + empty: PropTypes.string.isRequired, + paginationControls: PropTypes.string.isRequired, + formControl: PropTypes.string.isRequired, + itemsPerPageSelect: PropTypes.string.isRequired, + itemsPerPageLabel: PropTypes.string.isRequired, + }).isRequired, + title: PropTypes.string.isRequired, + itemSearch: PropTypes.shape({ + text: PropTypes.string, + input: PropTypes.instanceOf(ItemSearchInput), + }), +}; - const start = (page - 1) * itemsPerPage; - const end = start + itemsPerPage; - const itemsInPage = items.slice(start, end); +ItemsGrid.defaultProps = { + itemSearch: null, +}; - return ( -
- - - {this.renderItems(itemsInPage)} - - - - - - Items per page - - - - - - -
- ); - } -} const StyledComponent = withStyles(styles)(ItemsGrid); export default withRouter(StyledComponent); diff --git a/src/langs/en.json b/src/langs/en.json index 9fc367c34..f18f56404 100644 --- a/src/langs/en.json +++ b/src/langs/en.json @@ -92,6 +92,7 @@ "This item is empty.": "This item is empty.", "Search…": "Search…", "No search results found.": "No search results found.", + "Items per page": "Items per page:", "perform view": "perform view", "Show Perform View": "Show Perform View" } diff --git a/src/langs/fr.json b/src/langs/fr.json index 1a8aac2f6..e54dc3449 100644 --- a/src/langs/fr.json +++ b/src/langs/fr.json @@ -92,6 +92,7 @@ "This item is empty.": "Cet élément est vide.", "Search…": "Recherche…", "No search results found.": "Aucun résultat ne correspond à la recherche.", + "Items per page": "Eléments par page:", "perform view": "vue perform", "Show Perform View": "Montrer la Vue Perform" }