Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(containers): simplify Links container, add data fetching
Browse files Browse the repository at this point in the history
feat(containers): simplify Links container, add data fetching
  • Loading branch information
Metnew committed Dec 5, 2017
1 parent 757c302 commit a303334
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/common/containers/Links/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import React, {Component} from 'react'
import {connect} from 'react-redux'
import Helmet from 'react-helmet'
import {Loader} from 'semantic-ui-react'
import {Loader, Grid, List} from 'semantic-ui-react'
import {GET_LINKS} from 'actions/links'
import LinksComponent from './components'
import LinkItem from './components/LinkItem'
import {getEntitiesLinksState} from 'selectors'
import _ from 'lodash'
import type {GlobalState} from 'reducers'

type Props = {
Expand All @@ -17,16 +18,22 @@ type Props = {
isLinksLoaded: boolean
}

class Links extends Component <Props> {
componentDidMount () {
const {isLinksLoaded} = this.props
class Links extends Component {
props: Props

async asyncBootstrap () {
const {isLinksLoaded, getLinks} = this.props
if (!isLinksLoaded) {
this.getLinks()
await getLinks()
}
return true
}

getLinks () {
this.props.getLinks()
componentDidMount () {
const {isLinksLoaded, getLinks} = this.props
if (!isLinksLoaded) {
getLinks()
}
}

render () {
Expand All @@ -39,7 +46,15 @@ class Links extends Component <Props> {
{isLinksLoading ? (
<Loader active>Loading data...</Loader>
) : (
<LinksComponent links={links} />
<Grid stackable>
<Grid.Column width={16}>
<List relaxed divided animated>
{_.map(links, (link: LinkItem, i) => {
return <LinkItem key={i} item={link} />
})}
</List>
</Grid.Column>
</Grid>
)}
</div>
)
Expand All @@ -49,14 +64,14 @@ class Links extends Component <Props> {
function mapStateToProps (state: GlobalState) {
const linksState = getEntitiesLinksState(state)
const links = linksState.entities
const isLinksLoading = linksState.isLoading
const isLinksLoaded = linksState.isLoaded
const isLinksLoading = linksState.fetchStatus === 'loading'
const isLinksLoaded = linksState.fetchStatus === 'loaded'
return {links, isLinksLoading, isLinksLoaded}
}

const mapDispatchToProps = dispatch => ({
getLinks () {
dispatch(GET_LINKS())
async getLinks () {
return dispatch(GET_LINKS())
}
})

Expand Down

0 comments on commit a303334

Please sign in to comment.