Skip to content

Commit

Permalink
filters,sorting,set in pairs (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
burchardzosia authored Jun 10, 2024
1 parent 7eb5a13 commit 418a77e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 31 deletions.
31 changes: 18 additions & 13 deletions frontend/src/components/ContestantsCategoryList.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ function ContestansCategoryList() {
// COMUNICATE WITH BACKEND
// trzeba wziac pod uwage w ktorej stronie drabonki jest zawodnik i czy jest zaznaczona opcja
// kazdy z kazdym i na tej podstawie wygernowac drabinke
//
// OK, zróbcie POST pod /duels/generateLadder
// ze strukturą:
// {
// "weightCategory": "Men Heavy-Weight",
// "firstBracketContestant": 2, // id lub null
// "secondBracketContestant": null // id lub null
// }
// Desygnować można (nie trzeba) po jednym zawodniku do każdej ze stron.
// Nie robimy zaznaczania opcji każdy z każdym,
// mamy predefiniowane zasady dla danej liczby zawodników
//
// OK, zróbcie POST pod /duels/generateLadder
// ze strukturą:
// {
// "weightCategory": "Men Heavy-Weight",
// "firstBracketContestant": 2, // id lub null
// "secondBracketContestant": null // id lub null
// }
// Desygnować można (nie trzeba) po jednym zawodniku do każdej ze stron.
// Nie robimy zaznaczania opcji każdy z każdym,
// mamy predefiniowane zasady dla danej liczby zawodników
};

const [selected, setSelected] = useState([]);
Expand All @@ -57,7 +57,11 @@ function ContestansCategoryList() {

const handleSave = () => {
const contestantPairs = generatePairs(selected);
setPairs((prevPairs) => [...prevPairs, ...contestantPairs]);
const existingPairsSet = new Set(pairs.map((pair) => JSON.stringify(pair)));
const uniqueNewPairs = contestantPairs.filter(
(pair) => !existingPairsSet.has(JSON.stringify(pair)),
);
setPairs((prevPairs) => [...prevPairs, ...uniqueNewPairs]);
setSelected([]);
};

Expand Down Expand Up @@ -86,7 +90,8 @@ function ContestansCategoryList() {
<div>
<ul>
{pairs.map((pair, index) => (
<li key={index}>{`${pair[0]} and ${pair[1]}`}
<li key={index}>
{`${pair[0]} and ${pair[1]}`}
<button
className="remove-button"
onClick={() => handleRemove(index)}
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/components/ContestantsList.css
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,23 @@ img {
.loading-message {
text-align: center;
}
.filters{
margin-top: 10px;
margin-bottom: 10px;
margin-left: 20%;
font-size: 80px;
}
.filters select,
.filters input {
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}

.filters select {
width: 150px;
}

.filters input {
width: 100px;
}
75 changes: 60 additions & 15 deletions frontend/src/components/ContestantsList.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React, { useState, useEffect } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import useFetch from '../hooks/useFetch';
import './ContestantsList.css';
Expand All @@ -7,25 +8,76 @@ import exitIcon from '../../assets/icons/exit.png';
import config from '../config.js';

function ContestantsList() {
const {
data: data,
isPending,
error,
} = useFetch(`${config.backendUrl}/contestants`);
const [sort, setSort] = useState('');
const [minAge, setMinAge] = useState('');
const [maxAge, setMaxAge] = useState('');
const [minWeight, setMinWeight] = useState('');
const [maxWeight, setMaxWeight] = useState('');
const [sex, setSex] = useState('');

const createQueryParams = () => {
const params = new URLSearchParams();
if (sort) params.append('sort', sort);
if (minAge) params.append('minAge', minAge);
if (maxAge) params.append('maxAge', maxAge);
if (minWeight) params.append('minWeight', minWeight);
if (maxWeight) params.append('maxWeight', maxWeight);
if (sex) params.append('sex', sex);
return params.toString();
};

const { data, isPending, error } = useFetch(
`${config.backendUrl}/contestants?${createQueryParams()}`,
);
const history = useNavigate();

const handleExit = () => {
// eslint-disable-next-line no-console
console.log('Exit button pressed');
history('/');
};

// @ts-ignore
return (
<>
<h2>Wrestlers</h2>
{error && <div className="error-message">{error}</div>}
{isPending && <div className="loading-message">Loading...</div>}
<div className="filters">
<select value={sort} onChange={(e) => setSort(e.target.value)}>
<option value="">Sort By</option>
<option value="name">Name</option>
<option value="age">Age</option>
<option value="weight">Weight</option>
</select>
<input
type="number"
placeholder="Min Age"
value={minAge}
onChange={(e) => setMinAge(e.target.value)}
/>
<input
type="number"
placeholder="Max Age"
value={maxAge}
onChange={(e) => setMaxAge(e.target.value)}
/>
<input
type="number"
placeholder="Min Weight"
value={minWeight}
onChange={(e) => setMinWeight(e.target.value)}
/>
<input
type="number"
placeholder="Max Weight"
value={maxWeight}
onChange={(e) => setMaxWeight(e.target.value)}
/>
<select value={sex} onChange={(e) => setSex(e.target.value)}>
<option value="">Sex</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
{data && (
<div className="contestant-list">
{data.contestants.map((contestant) => (
Expand All @@ -37,19 +89,12 @@ function ContestantsList() {
<p className="weight-info">
{contestant.weight}kg ({contestant.weightCategory})
</p>
<p className="weight-info">Age: {contestant.age}</p>
</Link>
</div>
))}
</div>
)}
{/* Te buttony na razie nic nie robią bo task nie dotyczył filtrowania/sortowania,
to tylko koncepcja jak to mogłoby wyglądać na stronie */}
<button type="button" className="filter-button">
<img src={filterIcon} alt="filter-icon" />
</button>
<button type="button" className="sort-button">
<img src={sortIcon} alt="sort-icon" />
</button>
<button type="button" className="exit-button" onClick={handleExit}>
<img src={exitIcon} alt="exit-icon" />
</button>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/Duel.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ button:disabled {
background-color: #dddddd;
cursor: not-allowed;
}

5 changes: 3 additions & 2 deletions frontend/src/components/Duel.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Duel() {
} = useFetch(`${config.backendUrl}/contestants/${id1Contestant}`);

const { data: duel, loading } = useFetch(
`${config.backendUrl}/duels/${duelId}`,
`${config.backendUrl}/duels/curr/${duelId}`,
[],
{ suspense: true },
);
Expand All @@ -33,7 +33,8 @@ function Duel() {

useEffect(() => {
if (!loading && duel) {
const winnerExists = duel.winner !== '' && duel.winner !== undefined && duel.winner !== -1;
const winnerExists =
duel.winner !== '' && duel.winner !== undefined && duel.winner !== -1;
setIsWinnerSelected(winnerExists);
}
if (!loading && duel && duel.winner == contestant1.id) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/DuelList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function DuelList() {
data: data,
isPending,
error,
} = useFetch(`${config.backendUrl}/duels`);
} = useFetch(`${config.backendUrl}/duels/curr`);
const history = useNavigate();

const handleExit = () => {
Expand Down

0 comments on commit 418a77e

Please sign in to comment.