diff --git a/package.json b/package.json index 485e725..36c5da7 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@testing-library/user-event": "^14.4.3", "axios": "^1.3.5", "isbn-validate": "^1.0.3", + "lodash": "^4.17.21", "prop-types": "^15.8.1", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/src/components/Library/Library.jsx b/src/components/Library/Library.jsx index 925d070..867b5d3 100644 --- a/src/components/Library/Library.jsx +++ b/src/components/Library/Library.jsx @@ -1,5 +1,6 @@ import React, { useState } from 'react'; import PropTypes from 'prop-types'; +import { orderBy } from 'lodash'; import { alpha } from '@mui/material/styles'; import { useTheme } from '@mui/material/styles'; import Box from '@mui/material/Box'; @@ -65,6 +66,10 @@ export default function Library() { const [showSnackBar, handleSnackBar] = useState(false); const [removedItemName, setRemovedItemName] = useState(''); + //table sorting states + const [sortOrder,setSortOrder]=useState(false); + const [sortingColumn,setSortingColumn]=useState("") + //Modal states const [showModal, handleModalBox] = useState(false); const [name, setName] = useState(''); @@ -150,6 +155,33 @@ export default function Library() { } }; + // books table sort + const handleSort=(columnName,order)=>{ + if (columnName == 'title') { + const sortedRows = orderBy(myRows, ['name'], [order]); + setMyRows(sortedRows); + } else if (columnName == 'author') { + const sortedRows = orderBy(myRows, ['author'], [order]); + setMyRows(sortedRows); + } else if (columnName == 'category') { + const sortedRows = orderBy(myRows, ['category'], [order]); + setMyRows(sortedRows); + } else if (columnName == 'publisher') { + const sortedRows = orderBy(myRows, ['publisher'], [order]); + setMyRows(sortedRows); + } else if (columnName == 'year') { + const sortedRows = orderBy(myRows, ['year'], [order]); + setMyRows(sortedRows); + } else if (columnName == 'isbn') { + const sortedRows = orderBy(myRows, ['isbn'], [order]); + setMyRows(sortedRows); + } else if (columnName == 'edition') { + const sortedRows = orderBy(myRows, ['edition'], [order]); + setMyRows(sortedRows); + } + } + + //Check if ISBN is valid const handleISBNChange = (event) => { const value = event.target.value; @@ -271,9 +303,7 @@ export default function Library() { )} - //book review modal end// - - //book read review modal start// + //book review modal end// //book read review modal start//