Skip to content

Commit d79b165

Browse files
committed
19-with-search-params
1 parent 8322969 commit d79b165

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

app/components/Battle.jsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22
import PropTypes from "prop-types";
33
import { close } from "./icons";
4-
import Results from "./Results";
4+
import { Link } from "react-router-dom";
55

66
function Instructions() {
77
return (
@@ -103,7 +103,6 @@ export default class Battle extends React.Component {
103103
this.state = {
104104
playerOne: null,
105105
playerTwo: null,
106-
battle: false,
107106
};
108107

109108
this.handleSubmit = this.handleSubmit.bind(this);
@@ -120,25 +119,22 @@ export default class Battle extends React.Component {
120119
});
121120
}
122121
render() {
123-
const { playerOne, playerTwo, battle } = this.state;
122+
const { playerOne, playerTwo } = this.state;
124123
const disabled = !playerOne || !playerTwo;
125124

126-
if (battle === true) {
127-
return <Results playerOne={playerOne} playerTwo={playerTwo} />;
128-
}
129-
130125
return (
131126
<main className="stack main-stack animate-in">
132127
<div className="split">
133128
<h1>Players</h1>
134-
<button
135-
onClick={() => {
136-
this.setState({ battle: true });
129+
<Link
130+
to={{
131+
pathname: "/results",
132+
search: `?playerOne=${playerOne}&playerTwo=${playerTwo}`,
137133
}}
138134
className={`btn primary ${disabled ? "disabled" : ""}`}
139135
>
140136
Battle
141-
</button>
137+
</Link>
142138
</div>
143139
<section className="grid">
144140
{playerOne === null ? (

app/components/Results.jsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as React from "react";
22
import { battle } from "../utils/api";
33
import PropTypes from "prop-types";
44
import Loading from "./Loading";
5+
import withSearchParams from "./withSearchParams";
6+
import { Link } from "react-router-dom";
57

68
function Card({ profile }) {
79
const {
@@ -64,7 +66,7 @@ Card.propTypes = {
6466
}).isRequired,
6567
};
6668

67-
export default class Results extends React.Component {
69+
class Results extends React.Component {
6870
constructor(props) {
6971
super(props);
7072

@@ -76,7 +78,9 @@ export default class Results extends React.Component {
7678
};
7779
}
7880
componentDidMount() {
79-
const { playerOne, playerTwo } = this.props;
81+
const sp = this.props.router.searchParams;
82+
const playerOne = sp.get("playerOne");
83+
const playerTwo = sp.get("playerTwo");
8084

8185
battle([playerOne, playerTwo])
8286
.then((players) => {
@@ -109,6 +113,9 @@ export default class Results extends React.Component {
109113
<main className="animate-in stack main-stack">
110114
<div className="split">
111115
<h1>Results</h1>
116+
<Link to="/battle" className="btn secondary">
117+
Reset
118+
</Link>
112119
</div>
113120
<section className="grid">
114121
<article className="results-container">
@@ -131,7 +138,7 @@ export default class Results extends React.Component {
131138
<Card profile={loser.profile} />
132139
<p className="results">
133140
<span>
134-
{winner.score === loser.score ? "Tie" : "Loser"}
141+
{winner.score === loser.score ? "Tie" : "Loser"}{" "}
135142
{loser.score.toLocaleString()}
136143
</span>
137144
</p>
@@ -141,3 +148,5 @@ export default class Results extends React.Component {
141148
);
142149
}
143150
}
151+
152+
export default withSearchParams(Results);

app/components/withSearchParams.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as React from "react";
2+
import { useSearchParams } from "react-router-dom";
3+
4+
export default function withSearchParams(Component) {
5+
return function ComponentWithSearchParams(props) {
6+
const [searchParams] = useSearchParams();
7+
8+
return <Component {...props} router={{ searchParams }} />;
9+
};
10+
}

app/index.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Popular from "./components/Popular";
55
import Battle from "./components/Battle";
66
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
77
import Nav from "./components/Nav";
8+
import Results from "./components/Results";
89

910
class App extends React.Component {
1011
constructor(prop) {
@@ -30,6 +31,7 @@ class App extends React.Component {
3031
<Routes>
3132
<Route path="/" element={<Popular />} />
3233
<Route path="/battle" element={<Battle />} />
34+
<Route path="/results" element={<Results />} />
3335
</Routes>
3436
</div>
3537
</div>

0 commit comments

Comments
 (0)