Skip to content

Commit dfc430e

Browse files
committed
Add API call to app
1 parent bfab636 commit dfc430e

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ with custom values to setup a completely independent project:
9191
9292
aws ssm put-parameter --type "String" \
9393
--name "/app/aws-lambda-edge/experimental/appConfig" \
94-
--value '{"title":"Lambda@Edge Immutable Web App (Experimental)","reportUri":"https://httpbin.org/post"}'
94+
--value '{"title":"Lambda@Edge Immutable Web App (Experimental)","reportUri":"https://httpbin.org/post","api":"https://httpbin.org"}'
9595
```
9696
5. Build and deploy the initial version with
9797
```
@@ -118,7 +118,7 @@ with custom values to setup a completely independent project:
118118
119119
aws ssm put-parameter --type "String" \
120120
--name "/app/aws-lambda-edge/live/appConfig" \
121-
--value '{"title":"Lambda@Edge Immutable Web App","reportUri":"https://httpbin.org/post"}'
121+
--value '{"title":"Lambda@Edge Immutable Web App","reportUri":"https://httpbin.org/post","api":"https://httpbin.org"}'
122122
123123
npm run build
124124
npm run deploy:assets

src/app.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
3+
import Data from './data'
4+
5+
import './main.css'
6+
7+
const App = ({ title, api }) => (
8+
<main>
9+
<h1>{title}</h1>
10+
<div className='logo' />
11+
<Data api={api} />
12+
</main>
13+
)
14+
15+
export default App

src/data.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* global fetch */
2+
3+
import React, { Component } from 'react'
4+
5+
class Data extends Component {
6+
constructor (...args) {
7+
super(...args)
8+
this.state = { data: null }
9+
}
10+
11+
componentWillMount () {
12+
loadData(this.props.api)
13+
.then(data => { this.setState({ data }) })
14+
.catch(console.error)
15+
}
16+
17+
render () {
18+
if (this.state.data === null) return null
19+
return <p>{this.state.data.uuid}</p>
20+
}
21+
}
22+
23+
const loadData = api => fetch(`${api}/uuid`).then(d => d.json())
24+
25+
export default Data

src/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import React from 'react'
22
import { render } from 'react-dom'
33

4-
import './main.css'
4+
import App from './app'
5+
6+
const rootElementId = 'root'
57

68
const defaultAppConfig = {
7-
title: 'Lambda@Edge Immutable Web App'
9+
title: 'Lambda@Edge Immutable Web App',
10+
api: 'https://httpbin.org'
811
}
912

10-
const { title } = window.config || defaultAppConfig
11-
12-
const App = ({ title }) => (
13-
<main>
14-
<h1>{title}</h1>
15-
<div className='logo' />
16-
</main>
17-
)
18-
const renderApp = () => render(<App title={title} />, document.getElementById('root'))
13+
const renderApp = () => {
14+
const config = window.config || defaultAppConfig
15+
render(
16+
<App {...config} />,
17+
document.getElementById(rootElementId)
18+
)
19+
}
1920

2021
if ('addEventListener' in document) {
2122
document.addEventListener('DOMContentLoaded', renderApp)

0 commit comments

Comments
 (0)