Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step 7 - Hooks useState #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 27 additions & 41 deletions src/components/SearchCat/SearchCat.jsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
import React, { Component, Fragment } from "react";
import "./SearchCat.css";
import "./SearchCatForm";
import SearchCatForm from "./SearchCatForm";
import React, {Fragment, useState} from 'react';
import './SearchCat.css';
import './SearchCatForm';
import SearchCatForm from './SearchCatForm';

class SearchCat extends Component {
constructor(props) {
super(props);
this.state = {
displayed: false
};
this.toggleForm = this.toggleForm.bind(this);
}
function SearchCat(props) {
const [displayed, setDisplayed] = useState(false);
const toggleForm = () => setDisplayed(!displayed);

toggleForm() {
this.setState(state => ({ displayed: !state.displayed }));
}

render() {
return (
<Fragment>
<button className="SearchCat-button" onClick={this.toggleForm}>
Search cat
</button>
{this.state.displayed && (
<div className="SearchCat-overlay" onClick={this.toggleForm}>
<div
className="SearchCat-content"
onClick={e => e.stopPropagation()}
>
<SearchCatForm
limit={100}
excludedCats={this.props.excludedCats}
addCats={cats => {
this.setState({ displayed: false });
this.props.addCats(cats);
}}
/>
</div>
return (
<Fragment>
<button className="SearchCat-button" onClick={toggleForm}>
Search cat
</button>
{displayed && (
<div className="SearchCat-overlay" onClick={toggleForm}>
<div className="SearchCat-content" onClick={e => e.stopPropagation()}>
<SearchCatForm
limit={100}
excludedCats={props.excludedCats}
addCats={cats => {
setDisplayed(false);
props.addCats(cats);
}}
/>
</div>
)}
</Fragment>
);
}
</div>
)}
</Fragment>
);
}

export default SearchCat;
8 changes: 0 additions & 8 deletions src/redux/reducers/cats.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
const defaultState = [
{
url:
"https://images.mentalfloss.com/sites/default/files/styles/mf_image_16x9/public/549585-istock-909106260.jpg"
},
{
url:
"https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80"
},
{ url: "https://cdn2.thecatapi.com/images/c0.jpg" },
{ url: "https://cdn2.thecatapi.com/images/c3.jpg" },
{ url: "https://cdn2.thecatapi.com/images/dq.jpg" },
Expand Down