1- import React , { Component } from 'react'
1+ import React from 'react' ;
2+ import { Query } from 'react-apollo' ;
3+ import gql from 'graphql-tag' ;
24
3- const workplaces = [
4- {
5- id : '1' ,
6- name : 'To Fast To Sleep' ,
7- coverImage : 'https://images.unsplash.com/photo-1514371879740-2e7d2068f502?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2c8b4f34e0f1761ddb4fa0543fce2a0a&auto=format&fit=crop&w=1334&q=80' ,
8- description : 'Coffee Shop in Bangkok, Thailand' ,
9- phone : '086 300 9955' ,
10- rating : 4 ,
11- } ,
12- {
13- id : '2' ,
14- name : 'To Fast To Sleep' ,
15- coverImage : 'https://images.unsplash.com/photo-1514371879740-2e7d2068f502?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2c8b4f34e0f1761ddb4fa0543fce2a0a&auto=format&fit=crop&w=1334&q=80' ,
16- description : 'Coffee Shop in Bangkok, Thailand' ,
17- phone : '086 300 9955' ,
18- rating : 4 ,
19- } ,
20- {
21- id : '3' ,
22- name : 'To Fast To Sleep' ,
23- coverImage : 'https://images.unsplash.com/photo-1514371879740-2e7d2068f502?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2c8b4f34e0f1761ddb4fa0543fce2a0a&auto=format&fit=crop&w=1334&q=80' ,
24- description : 'Coffee Shop in Bangkok, Thailand' ,
25- phone : '086 300 9955' ,
26- rating : 4 ,
27- } ,
28- {
29- id : '4' ,
30- name : 'To Fast To Sleep' ,
31- coverImage : 'https://images.unsplash.com/photo-1514371879740-2e7d2068f502?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2c8b4f34e0f1761ddb4fa0543fce2a0a&auto=format&fit=crop&w=1334&q=80' ,
32- description : 'Coffee Shop in Bangkok, Thailand' ,
33- phone : '086 300 9955' ,
34- rating : 4 ,
35- } ,
36- {
37- id : '5' ,
38- name : 'To Fast To Sleep' ,
39- coverImage : 'https://images.unsplash.com/photo-1514371879740-2e7d2068f502?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2c8b4f34e0f1761ddb4fa0543fce2a0a&auto=format&fit=crop&w=1334&q=80' ,
40- description : 'Coffee Shop in Bangkok, Thailand' ,
41- phone : '086 300 9955' ,
42- rating : 4 ,
43- } ,
44- ]
5+ const Home = ( ) => (
6+ < Query
7+ query = { gql `
8+ query workplaces {
9+ workplaces {
10+ id
11+ name
12+ description
13+ coverImage
14+ }
15+ }
16+ ` }
17+ >
18+ { ( { loading, error, data } ) => {
19+ if ( loading ) return < p > Loading...</ p > ;
20+ else if ( error ) return < p > whoops!! somthing wrong.</ p > ;
4521
46- class Home extends Component {
47- render ( ) {
48- return (
49- < div >
50- < div style = { { display : 'flex' , flexWrap : 'wrap' } } >
51- { workplaces . map ( ( { id, name, description, coverImage, phone, rating } ) => (
52- < div key = { id } style = { { width : 'calc(33.3333% - 2rem - 2px)' , margin : '1rem' , border : '1px solid #eee' , borderRadius : 5 } } >
53- < img src = { coverImage } style = { { width : '100%' } } />
54- < div style = { { padding : 50 } } >
55- < h2 > { name } </ h2 >
56- < p > { description } </ p >
57- < div style = { { display : 'flex' , justifyContent : 'space-between' } } >
58- < p > contact: { phone } </ p >
59- < p > rating: { rating } /5</ p >
60- </ div >
61- </ div >
62- </ div >
63- ) ) }
22+ return (
23+ < div >
24+ < div style = { { display : 'flex' , flexWrap : 'wrap' } } >
25+ { data . workplaces &&
26+ data . workplaces . map ( ( { id, name, description, coverImage } ) => (
27+ < div
28+ key = { id }
29+ style = { {
30+ width : 'calc(33.3333% - 2rem - 2px)' ,
31+ margin : '1rem' ,
32+ border : '1px solid #eee' ,
33+ overflow : 'hidden' ,
34+ borderRadius : 5 ,
35+ } }
36+ >
37+ < img
38+ src = { coverImage }
39+ alt = { name }
40+ style = { { width : '100%' } }
41+ />
42+ < div style = { { padding : 50 } } >
43+ < h2 > { name } </ h2 >
44+ < p > { description } </ p >
45+ </ div >
46+ </ div >
47+ ) )
48+ }
49+ </ div >
6450 </ div >
65- </ div >
66- )
67- }
68- }
51+ ) ;
52+ } }
53+ </ Query >
54+ ) ;
6955
70- export default Home
56+ export default Home ;
0 commit comments