Added sorting functionality
This commit is contained in:
parent
6f8b34dc8e
commit
2e2135976f
|
@ -5,7 +5,7 @@
|
|||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "web-extracts-api",
|
||||
"name": "link-warden-api",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "web-extracts",
|
||||
"name": "link-warden",
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"@testing-library/jest-dom": "^5.16.4",
|
||||
|
|
80
src/App.js
80
src/App.js
|
@ -7,16 +7,16 @@ import Filters from './componets/Filters';
|
|||
import Sort from './componets/Sort';
|
||||
|
||||
function App() {
|
||||
const [data, setData] = useState([]);
|
||||
const [newBox, setNewBox] = useState(false);
|
||||
const [filterBox, setFilterBox] = useState(false);
|
||||
const [sortBox, setSortBox] = useState(false);
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [numberOfResults, setNumberOfResults] = useState(0);
|
||||
const [nameChecked, setNameChecked] = useState(true);
|
||||
const [descriptionChecked, setDescriptionChecked] = useState(true);
|
||||
const [tagsChecked, setTagsChecked] = useState(true);
|
||||
const [sort, setSort] = useState('Date');
|
||||
const [data, setData] = useState([]),
|
||||
[newBox, setNewBox] = useState(false),
|
||||
[filterBox, setFilterBox] = useState(false),
|
||||
[sortBox, setSortBox] = useState(false),
|
||||
[searchQuery, setSearchQuery] = useState(''),
|
||||
[numberOfResults, setNumberOfResults] = useState(0),
|
||||
[nameChecked, setNameChecked] = useState(true),
|
||||
[descriptionChecked, setDescriptionChecked] = useState(true),
|
||||
[tagsChecked, setTagsChecked] = useState(true),
|
||||
[sortBy, setSortBy] = useState('Date (Newest first)');
|
||||
|
||||
function handleNameCheckbox() {
|
||||
setNameChecked(!nameChecked);
|
||||
|
@ -45,6 +45,10 @@ function App() {
|
|||
function search(e) {
|
||||
setSearchQuery(e.target.value);
|
||||
}
|
||||
|
||||
function sortByFunc(e) {
|
||||
setSortBy(e)
|
||||
}
|
||||
|
||||
const filteredData = data.filter((e) => {
|
||||
const name = e.name.toLowerCase().includes(searchQuery.toLowerCase());
|
||||
|
@ -65,12 +69,58 @@ function App() {
|
|||
else if(descriptionChecked) { return title }
|
||||
});
|
||||
|
||||
function sortList(data = data, sortBy = 'Default') {
|
||||
let sortedData = data;
|
||||
if(sortBy === 'Date (Oldest first)') {
|
||||
sortedData.sort((a, b) => { return b-a });
|
||||
} else if(sortBy === 'Name (A-Z)') {
|
||||
sortedData.sort(function(a, b){
|
||||
const A = a.name.toLowerCase(), B = b.name.toLowerCase();
|
||||
if (A < B)
|
||||
return -1;
|
||||
if (A > B)
|
||||
return 1;
|
||||
return 0;
|
||||
});
|
||||
} else if(sortBy === 'Name (Z-A)') {
|
||||
sortedData.sort(function(a, b){
|
||||
const A = a.name.toLowerCase(), B = b.name.toLowerCase();
|
||||
if (A > B)
|
||||
return -1;
|
||||
if (A < B)
|
||||
return 1;
|
||||
return 0;
|
||||
});
|
||||
} else if(sortBy === 'Title (A-Z)') {
|
||||
sortedData.sort(function(a, b){
|
||||
const A = a.title.toLowerCase(), B = b.title.toLowerCase();
|
||||
if (A < B)
|
||||
return -1;
|
||||
if (A > B)
|
||||
return 1;
|
||||
return 0;
|
||||
});
|
||||
} else if(sortBy === 'Title (Z-A)') {
|
||||
sortedData.sort(function(a, b){
|
||||
const A = a.title.toLowerCase(), B = b.title.toLowerCase();
|
||||
if (A > B)
|
||||
return -1;
|
||||
if (A < B)
|
||||
return 1;
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
return sortedData;
|
||||
}
|
||||
|
||||
async function fetchData() {
|
||||
const address = config.api.address + ":" + config.api.port;
|
||||
const res = await fetch(address + '/api');
|
||||
const resJSON = await res.json();
|
||||
const Data = resJSON.sort((a, b) => { return b-a }); // <-- SORT IT!
|
||||
setData(Data);
|
||||
const data = resJSON.sort((a, b) => { return b-a });
|
||||
const sortedData = sortList(data, sortBy);
|
||||
setData(sortedData);
|
||||
}
|
||||
|
||||
const concatTags = () => {
|
||||
|
@ -87,7 +137,7 @@ function App() {
|
|||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, []);
|
||||
}, [sortBy]);
|
||||
|
||||
useEffect(() => {
|
||||
setNumberOfResults(filteredData.length);
|
||||
|
@ -107,8 +157,8 @@ function App() {
|
|||
<List data={filteredData} reFetch={fetchData} />
|
||||
|
||||
{sortBox ? <Sort
|
||||
onExit={exitSorting}
|
||||
reFetch={fetchData}
|
||||
sortBy={sortByFunc}
|
||||
onExit={exitSorting}
|
||||
/> : null}
|
||||
|
||||
{filterBox ? <Filters
|
||||
|
|
|
@ -13,7 +13,7 @@ const Filters = ({nameChecked, handleNameCheckbox, descriptionChecked, handleDes
|
|||
<fieldset className='filter'>
|
||||
<legend >Filter by</legend>
|
||||
<label><input type="checkbox" checked={nameChecked} onChange={handleNameCheckbox} />Name</label>
|
||||
<label><input type="checkbox" checked={descriptionChecked} onChange={handleDescriptionCheckbox} />Title</label>
|
||||
<label><input type="checkbox" checked={descriptionChecked} onChange={handleDescriptionCheckbox} />Website title</label>
|
||||
<label><input type="checkbox" checked={tagsChecked} onChange={handleTagsCheckbox} />Tags</label>
|
||||
</fieldset>
|
||||
</>
|
||||
|
|
|
@ -1,23 +1,27 @@
|
|||
import '../styles/Sort.css'
|
||||
|
||||
const Sort = ({ onExit }) => {
|
||||
const Sort = ({ sortBy, onExit }) => {
|
||||
function abort(e) {
|
||||
if (e.target.className === "sort-overlay") {
|
||||
onExit();
|
||||
}
|
||||
}
|
||||
|
||||
function sort(e) {
|
||||
sortBy(e.target.value);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className='sort-overlay' onClick={abort}></div>
|
||||
<fieldset className='sort'>
|
||||
<fieldset className='sort' onClick={sort}>
|
||||
<legend>Sort by</legend>
|
||||
<label><input name="sort" type="radio" />Date (Newest first)</label>
|
||||
<label><input name="sort" type="radio" />Date (Oldest first)</label>
|
||||
<label><input name="sort" type="radio" />Name (A-Z)</label>
|
||||
<label><input name="sort" type="radio" />Name (Z-A)</label>
|
||||
<label><input name="sort" type="radio" />Title (A-Z)</label>
|
||||
<label><input name="sort" type="radio" />Title (Z-A)</label>
|
||||
<button className='sort-by-btn' value='Default'> Date (Newest first)</button>
|
||||
<button className='sort-by-btn' value='Date (Oldest first)'> Date (Oldest first)</button>
|
||||
<button className='sort-by-btn' value='Name (A-Z)'> Name (A-Z)</button>
|
||||
<button className='sort-by-btn' value='Name (Z-A)'> Name (Z-A)</button>
|
||||
<button className='sort-by-btn' value='Title (A-Z)'> Website title (A-Z)</button>
|
||||
<button className='sort-by-btn' value='Title (Z-A)'> Website title (Z-A)</button>
|
||||
</fieldset>
|
||||
</>
|
||||
)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"api": {
|
||||
"address": "http://localhost",
|
||||
"port": 5002,
|
||||
"port": 5000,
|
||||
"mongodb_URI": "mongodb://localhost:27017",
|
||||
"database_name": "sample_db",
|
||||
"collection_name": "list",
|
||||
|
|
|
@ -33,4 +33,26 @@
|
|||
|
||||
.sort label {
|
||||
margin: 5px;
|
||||
}
|
||||
|
||||
.sort-by-btn {
|
||||
text-align: left;
|
||||
margin-bottom: 5px;
|
||||
font-family: 'Font Awesome 5 Free';
|
||||
padding: 10px;
|
||||
font-size: 1.1rem;
|
||||
cursor: pointer;
|
||||
box-shadow: rgba(0, 0, 0, 0.4) 0px 2px 4px, rgba(0, 0, 0, 0.3) 0px 7px 13px -3px, rgba(0, 0, 0, 0.2) 0px -3px 0px inset;
|
||||
color: #ffffffb6;
|
||||
background-color:#273949;
|
||||
border: none;
|
||||
transition: background-color 0.1s;
|
||||
}
|
||||
|
||||
.sort-by-btn:hover {
|
||||
background-color: rgb(76, 117, 170);
|
||||
}
|
||||
|
||||
.sort-by-btn:active {
|
||||
box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;
|
||||
}
|
Ŝarĝante…
Reference in New Issue