revised structure of index file
This commit is contained in:
parent
133cac478e
commit
4ba7e4660e
4
index.js
4
index.js
@ -1,4 +0,0 @@
|
|||||||
import React from 'react'
|
|
||||||
import ReactDOM from 'react-dom'
|
|
||||||
import { App } from './src'
|
|
||||||
ReactDOM.render(<App />, document.getElementById('app'))
|
|
15
src/app.js
Normal file
15
src/app.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import { Navbar, Main, Footer } from './components'
|
||||||
|
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Navbar />
|
||||||
|
<Main />
|
||||||
|
<Footer />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App
|
@ -1,53 +1,3 @@
|
|||||||
import React, { Component } from 'react'
|
export {default as Main } from './main'
|
||||||
import axios from 'axios'
|
export {default as Navbar } from './navbar'
|
||||||
import { Items } from './items'
|
export {default as Footer } from './footer'
|
||||||
|
|
||||||
class App extends Component {
|
|
||||||
constructor() {
|
|
||||||
super()
|
|
||||||
this.state = { loading: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
this.fetchAPI()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fetchAPI() {
|
|
||||||
try {
|
|
||||||
const { data } = await axios.get('/api')
|
|
||||||
const { ascii } = await data
|
|
||||||
await console.log(ascii)
|
|
||||||
await this.fetchItems()
|
|
||||||
await this.setState({ ascii })
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fetchItems() {
|
|
||||||
try {
|
|
||||||
const { data } = await axios.get('/api/items')
|
|
||||||
this.setState({ loading: false, items: data })
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
renderData() {
|
|
||||||
return (
|
|
||||||
<div className="mt-5">
|
|
||||||
<Items {...this.state} />
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
renderError() {
|
|
||||||
return <div>{this.state.error} </div>
|
|
||||||
}
|
|
||||||
render() {
|
|
||||||
if (this.state.loading) return <div />
|
|
||||||
if (this.state.items) return this.renderData()
|
|
||||||
if (this.state.error) return this.renderError()
|
|
||||||
return <div />
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default App
|
|
||||||
|
43
src/components/main.js
Normal file
43
src/components/main.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import React, { Component } from 'react'
|
||||||
|
import axios from 'axios'
|
||||||
|
import { Items } from './items'
|
||||||
|
import {Container, Row, Col} from 'react-bootstrap'
|
||||||
|
|
||||||
|
class App extends Component {
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
this.state = { loading: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.fetchApi()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fetchApi() {
|
||||||
|
try {
|
||||||
|
const { data } = await axios.get('/api/items')
|
||||||
|
this.setState({ loading: false, items: data })
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
|
this.setState({loading: false, apiNotFound: true})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
renderData() {
|
||||||
|
return <Items {...this.state} />
|
||||||
|
}
|
||||||
|
|
||||||
|
renderApiNotFound() {
|
||||||
|
return <Container className="mt-5"><Row/><Row><Col/>
|
||||||
|
<Col>Hello! The client is working correctly, but no API was found.</Col>
|
||||||
|
<Col/></Row></Container>
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
if (this.state.loading) return <div />
|
||||||
|
if(this.state.apiNotFound) return this.renderApiNotFound()
|
||||||
|
if (this.state.items) return this.renderData()
|
||||||
|
return <div />
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App
|
14
src/index.js
14
src/index.js
@ -1,14 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom'
|
||||||
import Main from './components'
|
import App from './app'
|
||||||
import Navbar from './components/navbar'
|
ReactDOM.render(<App />, document.getElementById('app'))
|
||||||
import Footer from './components/footer'
|
|
||||||
|
|
||||||
ReactDOM.render(
|
|
||||||
<div>
|
|
||||||
<Navbar />
|
|
||||||
<Main />
|
|
||||||
<Footer />
|
|
||||||
</div>,
|
|
||||||
document.getElementById('app')
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user