|
1 | 1 | import React from 'react';
|
2 | 2 | import logo from './logo.svg';
|
3 | 3 | import './App.css';
|
| 4 | +import Post from './Post' |
| 5 | +import { useState,useEffect } from 'react'; |
| 6 | +import { makeStyles } from '@material-ui/core/styles'; |
| 7 | +import Modal from '@material-ui/core/Modal'; |
| 8 | +import { Button, Input } from '@material-ui/core'; |
| 9 | +import {auth, db} from './firebase' |
| 10 | +import ImageUpload from './ImageUpload'; |
| 11 | +import InstagramEmbed from 'react-instagram-embed'; |
4 | 12 |
|
| 13 | +function getModalStyle() { |
| 14 | + const top = 50; |
| 15 | + const left = 50; |
| 16 | + |
| 17 | + return { |
| 18 | + top: `${top}%`, |
| 19 | + left: `${left}%`, |
| 20 | + transform: `translate(-${top}%, -${left}%)`, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +const useStyles = makeStyles((theme) => ({ |
| 25 | + paper: { |
| 26 | + position: 'absolute', |
| 27 | + width: 400, |
| 28 | + backgroundColor: theme.palette.background.paper, |
| 29 | + border: '2px solid #000', |
| 30 | + boxShadow: theme.shadows[5], |
| 31 | + padding: theme.spacing(2, 4, 3), |
| 32 | + }, |
| 33 | +})); |
5 | 34 | function App() {
|
| 35 | + const[openSignIn,setOpenSignIn]=useState(false); |
| 36 | + const [posts, setPosts] = useState([]); |
| 37 | + const [open,setOpen]=useState(false); |
| 38 | + const classes=useStyles(); |
| 39 | + const[modalStyle]=useState(getModalStyle); |
| 40 | + const [username,setUsername]=useState(''); |
| 41 | + const [email,setEmail]=useState(''); |
| 42 | + const [password,setPassword]=useState(''); |
| 43 | + const [user,setUser]=useState(''); |
| 44 | + |
| 45 | +useEffect(()=>{ |
| 46 | + |
| 47 | +const unSubscribe=auth.onAuthStateChanged((authUser)=>{ |
| 48 | + if(authUser){ |
| 49 | + console.log(authUser); |
| 50 | + setUser(authUser); |
| 51 | + }else{ |
| 52 | + setUser(null); |
| 53 | + } |
| 54 | + |
| 55 | +}) |
| 56 | +return()=>{ |
| 57 | + unSubscribe(); |
| 58 | +} |
| 59 | +},[user,username]); |
| 60 | + |
| 61 | + |
| 62 | + useEffect(()=>{ |
| 63 | + |
| 64 | + db.collection('post').orderBy('timestamp','desc').onSnapshot(snapshot =>{ |
| 65 | + setPosts(snapshot.docs.map(doc=>({ |
| 66 | + id:doc.id, |
| 67 | + // username:doc.username, |
| 68 | + post:doc.data() |
| 69 | + }))); |
| 70 | + |
| 71 | + }) |
| 72 | + |
| 73 | + |
| 74 | + },[]); |
| 75 | + |
| 76 | + const signup=(event)=>{ |
| 77 | + event.preventDefault(); |
| 78 | + auth.createUserWithEmailAndPassword(email,password).then((authUser)=>{ |
| 79 | + return authUser.user.updateProfile({ |
| 80 | + displayName:username |
| 81 | + }) |
| 82 | + }) |
| 83 | + .catch((error)=>alert(error.messages)); |
| 84 | + setOpen(false); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + const signin=(event)=>{ |
| 89 | + event.preventDefault(); |
| 90 | + auth.signInWithEmailAndPassword(email,password).catch((error)=>alert(error.messages)) |
| 91 | + |
| 92 | + setOpenSignIn(false); |
| 93 | + } |
| 94 | + |
6 | 95 | return (
|
7 |
| - <div className="App"> |
8 |
| - <header className="App-header"> |
9 |
| - <img src={logo} className="App-logo" alt="logo" /> |
10 |
| - <p> |
11 |
| - Edit <code>src/App.js</code> and save to reload. |
12 |
| - </p> |
13 |
| - <a |
14 |
| - className="App-link" |
15 |
| - href="https://reactjs.org" |
16 |
| - target="_blank" |
17 |
| - rel="noopener noreferrer" |
18 |
| - > |
19 |
| - Learn React |
20 |
| - </a> |
21 |
| - </header> |
| 96 | + <div className="app"> |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + <Modal |
| 101 | + open={open} |
| 102 | + onClose={() => setOpen(false)} |
| 103 | + > |
| 104 | + <div style={modalStyle} className={classes.paper}> |
| 105 | + <form className="app_signup"> |
| 106 | + <center> |
| 107 | + <img alt="Instagram" className="app__headerImage" src="https://www.instagram.com/static/images/web/mobile_nav_type_logo.png/735145cfe0a4.png" /> |
| 108 | + </center> |
| 109 | + <Input |
| 110 | + placeholder="username" |
| 111 | + type="text" |
| 112 | + value={username} |
| 113 | + onChange={(e)=>setUsername(e.target.value)} |
| 114 | + /> |
| 115 | + <Input |
| 116 | + placeholder="email" |
| 117 | + type="text" |
| 118 | + value={email} |
| 119 | + onChange={(e)=>setEmail(e.target.value)} |
| 120 | + /> |
| 121 | + <Input |
| 122 | + placeholder="password" |
| 123 | + type="password" |
| 124 | + value={password} |
| 125 | + onChange={(e)=>setPassword(e.target.value)} |
| 126 | + /> |
| 127 | + <Button onClick={signup}>Sign Up</Button> |
| 128 | + </form> |
| 129 | + </div> |
| 130 | +</Modal> |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +<Modal |
| 135 | + open={openSignIn} |
| 136 | + onClose={() => setOpenSignIn(false)} |
| 137 | + > |
| 138 | + <div style={modalStyle} className={classes.paper}> |
| 139 | + <form className="app_signup"> |
| 140 | + <center> |
| 141 | + <img alt="Instagram" className="app__headerImage" src="https://www.instagram.com/static/images/web/mobile_nav_type_logo.png/735145cfe0a4.png" /> |
| 142 | + </center> |
| 143 | + |
| 144 | + <Input |
| 145 | + placeholder="email" |
| 146 | + type="text" |
| 147 | + value={email} |
| 148 | + onChange={(e)=>setEmail(e.target.value)} |
| 149 | + /> |
| 150 | + <Input |
| 151 | + placeholder="password" |
| 152 | + type="password" |
| 153 | + value={password} |
| 154 | + onChange={(e)=>setPassword(e.target.value)} |
| 155 | + /> |
| 156 | + <Button onClick={signin}>Sign Up</Button> |
| 157 | + </form> |
| 158 | + </div> |
| 159 | +</Modal> |
| 160 | + |
| 161 | + <div className="app__header"> |
| 162 | + <img alt="Instagram" className="app__headerImage" src="https://www.instagram.com/static/images/web/mobile_nav_type_logo.png/735145cfe0a4.png" /> |
| 163 | + |
| 164 | + {user ?( |
| 165 | + <Button onClick={()=>auth.signOut()}>Logout</Button> |
| 166 | + ):( |
| 167 | + <div className="app_loginContainer"> |
| 168 | + <Button onClick={() => setOpen(true)}>Sign Up</Button> |
| 169 | + <Button onClick={() => setOpenSignIn(true)}>Sign In</Button> |
| 170 | + </div> |
| 171 | + )} |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + </div> |
| 176 | + |
| 177 | + <div className="app__posts"> |
| 178 | + |
| 179 | + <div className="app__postsLeft"> |
| 180 | + { |
| 181 | + posts.map(({id,post}) => ( |
| 182 | + <Post key={id} postId={id} user={user} username={post.username} caption={post.caption} imageUrl={post.imageUrl} /> |
| 183 | + )) |
| 184 | + } |
| 185 | + </div> |
| 186 | + <div className="app__postsRight"> |
| 187 | + <InstagramEmbed |
| 188 | + url='https://instagr.am/p/Zw9o4/' |
| 189 | + maxWidth={320} |
| 190 | + hideCaption={false} |
| 191 | + containerTagName='div' |
| 192 | + protocol='' |
| 193 | + injectScript |
| 194 | + onLoading={() => {}} |
| 195 | + onSuccess={() => {}} |
| 196 | + onAfterRender={() => {}} |
| 197 | + onFailure={() => {}} |
| 198 | + /> |
| 199 | + </div> |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + </div> |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + {user?.displayName ?( |
| 212 | + <ImageUpload username={user.displayName} /> |
| 213 | + ):( |
| 214 | + <h3>Sorry You need to login</h3> |
| 215 | + )} |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
22 | 222 | </div>
|
23 | 223 | );
|
24 | 224 | }
|
|
0 commit comments