|
1 | | -import React from 'react' |
2 | | -import Head from 'next/head' |
3 | | -import Nav from '../components/nav' |
| 1 | +import React, { Component } from 'react'; |
| 2 | +import Head from 'next/head'; |
| 3 | +import Nav from '../components/nav'; |
4 | 4 |
|
5 | | -const Home = () => ( |
6 | | - <div> |
7 | | - <Head> |
8 | | - <title>Home</title> |
9 | | - <link rel="icon" href="/favicon.ico" /> |
10 | | - </Head> |
| 5 | +// Firebase |
| 6 | +import loadFirebase from '../firebase.config'; |
11 | 7 |
|
12 | | - <Nav /> |
| 8 | +class Home extends Component { |
| 9 | + static async getInitialProps() { |
| 10 | + const firebase = await loadFirebase(); |
| 11 | + const db = firebase.firestore(); |
| 12 | + let result = await new Promise((resolve, reject) => { |
| 13 | + db.collection('photos') |
| 14 | + .get() |
| 15 | + .then(snapshot => { |
| 16 | + let data = []; |
| 17 | + snapshot.forEach(doc => { |
| 18 | + data.push( |
| 19 | + Object.assign( |
| 20 | + { |
| 21 | + id: doc.id |
| 22 | + }, |
| 23 | + doc.data() |
| 24 | + ) |
| 25 | + ); |
| 26 | + }); |
| 27 | + resolve(data); |
| 28 | + }) |
| 29 | + .catch(error => { |
| 30 | + reject([]); |
| 31 | + }); |
| 32 | + }); |
| 33 | + return { photos: result }; |
| 34 | + } |
13 | 35 |
|
14 | | - <div className="hero"> |
15 | | - <h1 className="title">Welcome to Next.js!</h1> |
16 | | - <p className="description"> |
17 | | - To get started, edit <code>pages/index.js</code> and save to reload. |
18 | | - </p> |
| 36 | + render() { |
| 37 | + const photos = this.props.photos; |
| 38 | + console.log('photos', photos); |
| 39 | + return ( |
| 40 | + <div> |
| 41 | + <Head> |
| 42 | + <title>Home</title> |
| 43 | + {/* <link rel='icon' href='/favicon.ico' /> */} |
| 44 | + </Head> |
19 | 45 |
|
20 | | - <div className="row"> |
21 | | - <a href="https://nextjs.org/docs" className="card"> |
22 | | - <h3>Documentation →</h3> |
23 | | - <p>Learn more about Next.js in the documentation.</p> |
24 | | - </a> |
25 | | - <a href="https://nextjs.org/learn" className="card"> |
26 | | - <h3>Next.js Learn →</h3> |
27 | | - <p>Learn about Next.js by following an interactive tutorial!</p> |
28 | | - </a> |
29 | | - <a |
30 | | - href="https://github.com/zeit/next.js/tree/master/examples" |
31 | | - className="card" |
32 | | - > |
33 | | - <h3>Examples →</h3> |
34 | | - <p>Find other example boilerplates on the Next.js GitHub.</p> |
35 | | - </a> |
36 | | - </div> |
37 | | - </div> |
| 46 | + <Nav /> |
| 47 | + |
| 48 | + <div className='hero'> |
| 49 | + <h1 className='title'>Welcome to Next.js with Firebase!</h1> |
38 | 50 |
|
39 | | - <style jsx>{` |
40 | | - .hero { |
41 | | - width: 100%; |
42 | | - color: #333; |
43 | | - } |
44 | | - .title { |
45 | | - margin: 0; |
46 | | - width: 100%; |
47 | | - padding-top: 80px; |
48 | | - line-height: 1.15; |
49 | | - font-size: 48px; |
50 | | - } |
51 | | - .title, |
52 | | - .description { |
53 | | - text-align: center; |
54 | | - } |
55 | | - .row { |
56 | | - max-width: 880px; |
57 | | - margin: 80px auto 40px; |
58 | | - display: flex; |
59 | | - flex-direction: row; |
60 | | - justify-content: space-around; |
61 | | - } |
62 | | - .card { |
63 | | - padding: 18px 18px 24px; |
64 | | - width: 220px; |
65 | | - text-align: left; |
66 | | - text-decoration: none; |
67 | | - color: #434343; |
68 | | - border: 1px solid #9b9b9b; |
69 | | - } |
70 | | - .card:hover { |
71 | | - border-color: #067df7; |
72 | | - } |
73 | | - .card h3 { |
74 | | - margin: 0; |
75 | | - color: #067df7; |
76 | | - font-size: 18px; |
77 | | - } |
78 | | - .card p { |
79 | | - margin: 0; |
80 | | - padding: 12px 0 0; |
81 | | - font-size: 13px; |
82 | | - color: #333; |
83 | | - } |
84 | | - `}</style> |
85 | | - </div> |
86 | | -) |
| 51 | + <div className='row'> |
| 52 | + {photos.map(photo => ( |
| 53 | + <div className='card' id='data' key={photo.id}> |
| 54 | + <img src={photo.url} alt='Photo' className='photo' /> |
| 55 | + <h3>{photo.title}</h3> |
| 56 | + </div> |
| 57 | + ))} |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + |
| 61 | + <style jsx>{` |
| 62 | + .hero { |
| 63 | + width: 100%; |
| 64 | + color: #333; |
| 65 | + } |
| 66 | + .title { |
| 67 | + margin: 0; |
| 68 | + width: 100%; |
| 69 | + padding-top: 80px; |
| 70 | + line-height: 1.15; |
| 71 | + font-size: 48px; |
| 72 | + } |
| 73 | + .title, |
| 74 | + .description { |
| 75 | + text-align: center; |
| 76 | + } |
| 77 | + .row { |
| 78 | + max-width: 880px; |
| 79 | + margin: 80px auto 40px; |
| 80 | + display: flex; |
| 81 | + flex-direction: row; |
| 82 | + justify-content: space-around; |
| 83 | + } |
| 84 | + .card { |
| 85 | + padding: 18px 18px 24px; |
| 86 | + width: 220px; |
| 87 | + text-align: left; |
| 88 | + text-decoration: none; |
| 89 | + color: #434343; |
| 90 | + border: 1px solid #9b9b9b; |
| 91 | + } |
| 92 | + .card:hover { |
| 93 | + border-color: #067df7; |
| 94 | + } |
| 95 | + .card h3 { |
| 96 | + margin: 0; |
| 97 | + color: #067df7; |
| 98 | + font-size: 18px; |
| 99 | + } |
| 100 | + .card p { |
| 101 | + margin: 0; |
| 102 | + padding: 12px 0 0; |
| 103 | + font-size: 13px; |
| 104 | + color: #333; |
| 105 | + } |
| 106 | + .photo { |
| 107 | + vertical-align: middle; |
| 108 | + width: 50px; |
| 109 | + height: 50px; |
| 110 | + border-radius: 50%; |
| 111 | + } |
| 112 | + `}</style> |
| 113 | + </div> |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
87 | 117 |
|
88 | | -export default Home |
| 118 | +export default Home; |
0 commit comments