Skip to content

Commit

Permalink
Add API call to app
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Nov 30, 2018
1 parent bfab636 commit dfc430e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ with custom values to setup a completely independent project:
aws ssm put-parameter --type "String" \
--name "/app/aws-lambda-edge/experimental/appConfig" \
--value '{"title":"Lambda@Edge Immutable Web App (Experimental)","reportUri":"https://httpbin.org/post"}'
--value '{"title":"Lambda@Edge Immutable Web App (Experimental)","reportUri":"https://httpbin.org/post","api":"https://httpbin.org"}'
```
5. Build and deploy the initial version with
```
Expand All @@ -118,7 +118,7 @@ with custom values to setup a completely independent project:
aws ssm put-parameter --type "String" \
--name "/app/aws-lambda-edge/live/appConfig" \
--value '{"title":"Lambda@Edge Immutable Web App","reportUri":"https://httpbin.org/post"}'
--value '{"title":"Lambda@Edge Immutable Web App","reportUri":"https://httpbin.org/post","api":"https://httpbin.org"}'
npm run build
npm run deploy:assets
Expand Down
15 changes: 15 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'

import Data from './data'

import './main.css'

const App = ({ title, api }) => (
<main>
<h1>{title}</h1>
<div className='logo' />
<Data api={api} />
</main>
)

export default App
25 changes: 25 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* global fetch */

import React, { Component } from 'react'

class Data extends Component {
constructor (...args) {
super(...args)
this.state = { data: null }
}

componentWillMount () {
loadData(this.props.api)
.then(data => { this.setState({ data }) })
.catch(console.error)
}

render () {
if (this.state.data === null) return null
return <p>{this.state.data.uuid}</p>
}
}

const loadData = api => fetch(`${api}/uuid`).then(d => d.json())

export default Data
23 changes: 12 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import React from 'react'
import { render } from 'react-dom'

import './main.css'
import App from './app'

const rootElementId = 'root'

const defaultAppConfig = {
title: 'Lambda@Edge Immutable Web App'
title: 'Lambda@Edge Immutable Web App',
api: 'https://httpbin.org'
}

const { title } = window.config || defaultAppConfig

const App = ({ title }) => (
<main>
<h1>{title}</h1>
<div className='logo' />
</main>
)
const renderApp = () => render(<App title={title} />, document.getElementById('root'))
const renderApp = () => {
const config = window.config || defaultAppConfig
render(
<App {...config} />,
document.getElementById(rootElementId)
)
}

if ('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', renderApp)
Expand Down

0 comments on commit dfc430e

Please sign in to comment.