-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.jsx
More file actions
91 lines (71 loc) · 2.05 KB
/
Copy pathApp.jsx
File metadata and controls
91 lines (71 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import './App.css';
import Header from './components/Header';
import Mainboard from './components/Mainboard';
import unplash from './api/unplash';
import { useEffect, useState } from 'react';
import { Routes, Route } from "react-router";
import {SinglePin} from './components/SinglePin';
import NoteFound from './components/NoteFound';
import FirstPage from './components/FirstPage';
import Profile from './components/profile';
import AddNewPin from './components/AddNewPin';
import Upload from './components/Upload';
function App() {
const [pins,setPins]=useState ([]);
const getImages=(term)=>{
return unplash.get("https://api.unsplash.com/search/photos",{
params:{query:term}
})
}
const onSearchSubmit=(term)=>{
getImages(term).then((res)=>{
let results=res.data.results;
let newPins=[
...results,
...pins,
]
newPins.sort(function(a,b){
return 0.5-Math.random()
});
setPins(newPins);
})
}
const getNewPins=()=>{
let promises=[];
let pinData=[];
let pins=["ocean","india","tokyo","cats","Bali","car","Bike"];
pins.forEach((pin)=>{
promises.push(getImages(pin).then((res)=>{
let results=res.data.results;
pinData=pinData.concat(results);
pinData.sort(function(a,b){
return 0.5-Math.random();
})
}));
})
Promise.all(promises).then(()=>{
setPins(pinData);
})
}
useEffect(()=>{
getNewPins()
},[])
return (
<div className="App">
{
localStorage.getItem("token")!==null?<Header onSubmit={onSearchSubmit}/>:null
}
{
localStorage.getItem("token")!==null?<AddNewPin/>:null
}
<Routes>
<Route path="/" element={localStorage.getItem("token")!==null?<Mainboard pins={pins}/>:<FirstPage/>} />
<Route path="/pin/:id" element={<SinglePin/>} />
<Route path="/pin/profile" element={<Profile/>} />
<Route path="/upload" element={<Upload/>} />
<Route path="*" element={<NoteFound/>} />
</Routes>
</div>
);
}
export default App;