Skip to content

Commit 0049399

Browse files
committedJul 23, 2022
fetch problem solved
1 parent 40d684d commit 0049399

File tree

7 files changed

+110
-10
lines changed

7 files changed

+110
-10
lines changed
 

‎unsplash_clone/src/Components/Menubar.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const Menubar = () => {
2323
borderRight="2px solid lightgrey"
2424
>
2525
<Box className="border-b">
26-
<Link to="/editorial">Editorial</Link>
26+
<Link to="/">Editorial</Link>
2727
</Box>
2828

2929
<Box className="border-b">

‎unsplash_clone/src/Components/Navbar.jsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323

2424
import "../App.css";
2525

26-
import React, { useRef, useState } from "react";
26+
import React, { useContext, useRef, useState } from "react";
2727
import unIcon from "../icons/icon1.png";
2828
import bell from "../icons/bell.png";
2929
import hemb from "../icons/hemb.png";
@@ -36,9 +36,16 @@ import SubmitPhoto from "../PopOver/SubmitPhoto.jsx";
3636
import ExtraLink from "../PopOver/ExtraLink";
3737
import Recomandation from "../PopOver/Recomandation";
3838
import Menubar from "./Menubar";
39+
import { ImgContext } from "../Context/ImgContext";
40+
import { useNavigate } from "react-router-dom";
3941

4042
//////////////////////////////////////////////////////////////////////////////////
4143
const Navbar = () => {
44+
45+
const {fetchdata} = useContext(ImgContext)
46+
47+
const nav = useNavigate()
48+
4249
const { isOpen, onOpen, onClose } = useDisclosure();
4350
const [scrollBehavior, setScrollBehavior] = useState("inside");
4451

@@ -79,6 +86,12 @@ const Navbar = () => {
7986
placeholder=" Search free high resolution-photos"
8087
borderRadius="40px"
8188
bgColor="rgb(238,238,238)"
89+
// value={query}
90+
onKeyPress={(e) => {
91+
if (e.key === "Enter") {
92+
fetchdata(e.target.value)
93+
nav("/search")
94+
}}}
8295
/>
8396
</PopoverTrigger>
8497

‎unsplash_clone/src/Context/ImgContext.jsx

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
import { Box, Text } from "@chakra-ui/react";
2-
import { createContext, useEffect } from "react";
2+
import { createContext, useEffect, useState } from "react";
33

44
export const ImgContext = createContext();
55

6+
7+
68
function ImgContextProvider({ children }) {
7-
return <ImgContext.Provider>{children}</ImgContext.Provider>;
9+
10+
const [fdata,setFdata] = useState([])
11+
12+
// let query = "cars"
13+
14+
function fetchdata(query){
15+
16+
fetch(`https://api.unsplash.com/search/photos/?client_id=EzVvR2dvr-RYFFKN-LL8BgG-tneKy3hCT4dts2kqu_g&query=${query}`)
17+
.then(r => r.json())
18+
.then( data => setFdata(data.results))
19+
20+
}
21+
22+
useEffect(()=>{
23+
fetchdata()
24+
25+
},[])
26+
27+
console.log(fdata)
28+
29+
return(<ImgContext.Provider value={{fetchdata,fdata}} >{children}</ImgContext.Provider>)
830
}
931

1032
export default ImgContextProvider;
1133

1234
//EzVvR2dvr-RYFFKN-LL8BgG-tneKy3hCT4dts2kqu_g
1335

14-
// sequerty
15-
//9n43FlEse2MkigATyuLB9Yj17diDm5apvz-yJb13NiU
1636

1737
//https://api.unsplash.com/photos/?client_id=YOUR_ACCESS_KEY
1838

‎unsplash_clone/src/Demo/Demo.jsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import React from "react";
1+
import React, { useContext } from "react";
22
import { useState } from "react";
33
import { editorial } from "../Api";
44
import "../App.css";
5+
import { ImgContext } from "../Context/ImgContext";
56

67
const Demo = () => {
8+
9+
const {fetchdata} = useContext(ImgContext)
10+
711
const [model, setModel] = useState(false);
812

913
const [tempimgSrc, setTempImgSrc] = useState("");
@@ -14,6 +18,8 @@ const Demo = () => {
1418
};
1519

1620
return (
21+
<>
22+
1723
<div style={{ width: "90%", margin: "auto" }}>
1824
<div className={model ? "model open" : "model"}>
1925
<img src={tempimgSrc} />
@@ -38,6 +44,7 @@ const Demo = () => {
3844
})}
3945
</div>
4046
</div>
47+
</>
4148
);
4249
};
4350

‎unsplash_clone/src/Pages/Editorial.jsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ import Recomandation from "../PopOver/Recomandation";
1414
import imgsearch from "../icons/imgs.png";
1515
import search from "../icons/search.png";
1616

17-
import React from "react";
17+
import React, { useContext } from "react";
1818
import { useState } from "react";
1919
import { editorial } from "../Api";
2020
import "../App.css";
21+
import { ImgContext } from "../Context/ImgContext";
22+
import { useNavigate } from "react-router-dom";
2123

2224
const Editorial = () => {
25+
26+
const {fetchdata} = useContext(ImgContext)
27+
28+
const nav = useNavigate()
29+
2330
const [model, setModel] = useState(false);
2431

2532
const [tempimgSrc, setTempImgSrc] = useState("");
@@ -78,6 +85,11 @@ const Editorial = () => {
7885
padding="25px"
7986
placeholder=" Search free high resolution-photos"
8087
bgColor="rgb(238,238,238)"
88+
onKeyPress={(e) => {
89+
if (e.key === "Enter") {
90+
fetchdata(e.target.value)
91+
nav("/search")
92+
}}}
8193
/>
8294
</PopoverTrigger>
8395

‎unsplash_clone/src/Routes/AllRoutes.jsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ import StreetPhotograph from "../Pages/StreetPhotography";
2424
import TexturesPatterns from "../Pages/TexturesPatterns";
2525
import Travel from "../Pages/Travel";
2626
import Wallpapers from "../Pages/Wallpapers";
27+
import Search from "../Search";
2728

2829
const AllRoutes = () => {
2930
return (
3031
<>
3132
<Routes>
32-
<Route path="/" element={<Home />} />
33+
{/* <Route path="/" element={<Home />} /> */}
3334
<Route path="/renders" element={<Renders />} />
3435
<Route path="/animals" element={<Animals />} />
3536
<Route path="/architecture" element={<Architecture />} />
3637
<Route path="/artsCulture" element={<ArtsCulture />} />
3738
<Route path="/athletics" element={<Athletics />} />
3839
<Route path="/businessWork" element={<BusinessWork />} />
3940
<Route path="/currentEvents" element={<CurrentEvents />} />
40-
<Route path="/editorial" element={<Editorial />} />
41+
<Route path="/" element={<Editorial />} />
4142
<Route path="/experimental" element={<Experimental />} />
4243
<Route path="/fashion" element={<Fashion />} />
4344
<Route path="/film" element={<Film />} />
@@ -53,6 +54,7 @@ const AllRoutes = () => {
5354
<Route path="/texturesPatterns" element={<TexturesPatterns />} />
5455
<Route path="/travel" element={<Travel />} />
5556
<Route path="/wallpapers" element={<Wallpapers />} />
57+
<Route path="/search" element={<Search />} />
5658
</Routes>
5759
</>
5860
);

‎unsplash_clone/src/Search.jsx

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React, { useContext, useState } from 'react'
2+
import { ImgContext } from './Context/ImgContext'
3+
import "./App.css"
4+
5+
const Search = () => {
6+
const {fdata} = useContext(ImgContext)
7+
8+
const [model, setModel] = useState(false);
9+
10+
const [tempimgSrc, setTempImgSrc] = useState("");
11+
12+
const getImg = (imgsrc) => {
13+
setTempImgSrc(imgsrc);
14+
setModel(true);
15+
};
16+
return (
17+
<>
18+
<div style={{ width: "90%", margin: "auto" }}>
19+
<div className={model ? "model open" : "model"}>
20+
<img src={tempimgSrc} />
21+
<i
22+
onClick={() => setModel(false)}
23+
style={{ backgroundColor: "white", color: "black" }}
24+
class="fa-solid fa-xmark-large"
25+
></i>
26+
</div>
27+
28+
<div className="gallery">
29+
{fdata.map((item) => {
30+
return (
31+
<div className="pics" key={item.id}>
32+
<img
33+
src={item.urls.full}
34+
style={{ width: "100%" }}
35+
onClick={() => getImg(item.urls.full)}
36+
/>
37+
</div>
38+
);
39+
})}
40+
</div>
41+
</div>
42+
</>
43+
)
44+
}
45+
46+
export default Search

0 commit comments

Comments
 (0)
Please sign in to comment.