mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-12 13:26:45 +00:00
random button with context
This commit is contained in:
parent
6f1ae88cb4
commit
e4d6c31128
7 changed files with 14 additions and 48 deletions
|
|
@ -4,7 +4,6 @@ import "./index.css";
|
||||||
import MainLayout from "./layouts/MainLayout";
|
import MainLayout from "./layouts/MainLayout";
|
||||||
import { AppRouter } from "./router";
|
import { AppRouter } from "./router";
|
||||||
import { Router } from "./router/Router";
|
import { Router } from "./router/Router";
|
||||||
import { getData } from "./services/api";
|
|
||||||
import { MealSummary } from "./types/meal";
|
import { MealSummary } from "./types/meal";
|
||||||
import { useAuth0 } from "./utils/auth0-spa";
|
import { useAuth0 } from "./utils/auth0-spa";
|
||||||
|
|
||||||
|
|
@ -14,11 +13,6 @@ export const App: FC = () => {
|
||||||
const [searchResults, setSearchResults] = useState({
|
const [searchResults, setSearchResults] = useState({
|
||||||
meals: [] as MealSummary[],
|
meals: [] as MealSummary[],
|
||||||
});
|
});
|
||||||
const [_, setMeal] = useState(null);
|
|
||||||
|
|
||||||
const getRandomMeal = () => {
|
|
||||||
getData("random", setMeal);
|
|
||||||
};
|
|
||||||
|
|
||||||
return loading ? (
|
return loading ? (
|
||||||
<div className="container center-align valign-wrapper">
|
<div className="container center-align valign-wrapper">
|
||||||
|
|
@ -27,7 +21,6 @@ export const App: FC = () => {
|
||||||
) : (
|
) : (
|
||||||
<Router>
|
<Router>
|
||||||
<MainLayout
|
<MainLayout
|
||||||
getRandomMeal={getRandomMeal}
|
|
||||||
searchString={searchString}
|
searchString={searchString}
|
||||||
setSearchResults={setSearchResults}
|
setSearchResults={setSearchResults}
|
||||||
setSearchString={setSearchString}
|
setSearchString={setSearchString}
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,9 @@ import { Logo } from "./Logo";
|
||||||
import { LogOutButton } from "./LogOutButton";
|
import { LogOutButton } from "./LogOutButton";
|
||||||
import { RandomButton } from "./RandomButton";
|
import { RandomButton } from "./RandomButton";
|
||||||
|
|
||||||
type Props = {
|
type Props = { openNavClick: React.MouseEventHandler };
|
||||||
openNavClick: React.MouseEventHandler;
|
|
||||||
handleClick: () => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const Navbar: FC<Props> = ({ openNavClick, handleClick }) => {
|
export const Navbar: FC<Props> = ({ openNavClick }) => {
|
||||||
const { isAuthenticated } = useAuth0();
|
const { isAuthenticated } = useAuth0();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -31,7 +28,6 @@ export const Navbar: FC<Props> = ({ openNavClick, handleClick }) => {
|
||||||
)}
|
)}
|
||||||
<li>
|
<li>
|
||||||
<RandomButton
|
<RandomButton
|
||||||
handleClick={handleClick}
|
|
||||||
url={buttonURL}
|
url={buttonURL}
|
||||||
size="small"
|
size="small"
|
||||||
color="orange darken-2"
|
color="orange darken-2"
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,20 @@
|
||||||
import { FC } from "react";
|
import { FC } from "react";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
|
import { useMeal } from "../store/meal";
|
||||||
|
import { fetchRandomMeal } from "../store/meal/async";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
url: string;
|
url: string;
|
||||||
size?: string;
|
size?: string;
|
||||||
handleClick: () => void;
|
|
||||||
color?: string;
|
color?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const RandomButton: FC<Props> = ({
|
export const RandomButton: FC<Props> = ({ url, size = "large", color }) => {
|
||||||
url,
|
|
||||||
size = "large",
|
|
||||||
handleClick,
|
|
||||||
color,
|
|
||||||
}) => {
|
|
||||||
const classString = `waves-effect waves-light btn-${size} ${color}`;
|
const classString = `waves-effect waves-light btn-${size} ${color}`;
|
||||||
|
const { dispatch } = useMeal();
|
||||||
return (
|
return (
|
||||||
<Link to={url}>
|
<Link to={url}>
|
||||||
<button className={classString} onClick={handleClick}>
|
<button className={classString} onClick={() => fetchRandomMeal(dispatch)}>
|
||||||
Random Recipe
|
Random Recipe
|
||||||
</button>
|
</button>
|
||||||
</Link>
|
</Link>
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,9 @@ import { LogInButton } from "./LogInButton";
|
||||||
import { LogOutButton } from "./LogOutButton";
|
import { LogOutButton } from "./LogOutButton";
|
||||||
import { RandomButton } from "./RandomButton";
|
import { RandomButton } from "./RandomButton";
|
||||||
|
|
||||||
type Props = {
|
type Props = { showNav: boolean; closeNavClick: React.MouseEventHandler };
|
||||||
showNav: boolean;
|
|
||||||
closeNavClick: React.MouseEventHandler;
|
|
||||||
handleClick: () => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const SideNav: FC<Props> = ({ showNav, closeNavClick, handleClick }) => {
|
export const SideNav: FC<Props> = ({ showNav, closeNavClick }) => {
|
||||||
const { isAuthenticated, user } = useAuth0();
|
const { isAuthenticated, user } = useAuth0();
|
||||||
let transformStyle = {
|
let transformStyle = {
|
||||||
transform: showNav ? "translateX(0%)" : "translateX(-105%)",
|
transform: showNav ? "translateX(0%)" : "translateX(-105%)",
|
||||||
|
|
@ -65,12 +61,7 @@ export const SideNav: FC<Props> = ({ showNav, closeNavClick, handleClick }) => {
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<RandomButton
|
<RandomButton url={buttonURL} size="small" color="orange darken-2" />
|
||||||
handleClick={handleClick}
|
|
||||||
url={buttonURL}
|
|
||||||
size="small"
|
|
||||||
color="orange darken-2"
|
|
||||||
/>
|
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<Link to="#">
|
<Link to="#">
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,7 @@ export const Home: FC = () => (
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<div className="col s12 m6">
|
<div className="col s12 m6">
|
||||||
<h1 className="logo">Chef's Online Cookbook</h1>
|
<h1 className="logo">Chef's Online Cookbook</h1>
|
||||||
<RandomButton
|
<RandomButton url={buttonURL} size="large" color="orange darken-2" />
|
||||||
url={buttonURL}
|
|
||||||
size="large"
|
|
||||||
color="orange darken-2"
|
|
||||||
handleClick={() => {}}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<picture className="col s12 m6">
|
<picture className="col s12 m6">
|
||||||
<img src={HeroImage} alt="hero_image" width="100%" />
|
<img src={HeroImage} alt="hero_image" width="100%" />
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ export const NotFound: FC = () => (
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="card-content">
|
<div className="card-content">
|
||||||
<RandomButton url="/random" handleClick={() => {}} />
|
<RandomButton url="/random" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import { SideNav } from "../components/SideNav";
|
||||||
import { MealSummary } from "../types/meal";
|
import { MealSummary } from "../types/meal";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
getRandomMeal: () => void;
|
|
||||||
searchString: string;
|
searchString: string;
|
||||||
setSearchString: React.Dispatch<React.SetStateAction<string>>;
|
setSearchString: React.Dispatch<React.SetStateAction<string>>;
|
||||||
setSearchResults: React.Dispatch<
|
setSearchResults: React.Dispatch<
|
||||||
|
|
@ -14,7 +13,6 @@ type Props = {
|
||||||
>;
|
>;
|
||||||
};
|
};
|
||||||
const MainLayout: FC<Props> = ({
|
const MainLayout: FC<Props> = ({
|
||||||
getRandomMeal,
|
|
||||||
searchString,
|
searchString,
|
||||||
setSearchString,
|
setSearchString,
|
||||||
setSearchResults,
|
setSearchResults,
|
||||||
|
|
@ -43,18 +41,14 @@ const MainLayout: FC<Props> = ({
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<header>
|
<header>
|
||||||
<Navbar handleClick={getRandomMeal} openNavClick={openNavClick} />
|
<Navbar openNavClick={openNavClick} />
|
||||||
|
|
||||||
<SearchBar
|
<SearchBar
|
||||||
searchString={searchString}
|
searchString={searchString}
|
||||||
setSearchString={setSearchString}
|
setSearchString={setSearchString}
|
||||||
setSearchResults={setSearchResults}
|
setSearchResults={setSearchResults}
|
||||||
/>
|
/>
|
||||||
<SideNav
|
<SideNav showNav={showNav} closeNavClick={closeNavClick} />
|
||||||
showNav={showNav}
|
|
||||||
closeNavClick={closeNavClick}
|
|
||||||
handleClick={() => {}}
|
|
||||||
/>
|
|
||||||
</header>
|
</header>
|
||||||
<main>{children}</main>
|
<main>{children}</main>
|
||||||
<Footer />
|
<Footer />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue