Skip to content

Commit

Permalink
Render project workflows on the server
Browse files Browse the repository at this point in the history
Request workflows for the home page in getServerSideProps, then pass the workflow data down to the Hero component as a prop.
  • Loading branch information
eatyourgreens committed Sep 18, 2020
1 parent e73ae1f commit 1314c84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 75 deletions.
17 changes: 17 additions & 0 deletions packages/app-project/pages/projects/[owner]/[project]/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
import { projects } from '@zooniverse/panoptes-js'
import fetchWorkflowsHelper from '@screens/ProjectHomePage/components/Hero/helpers/fetchWorkflowsHelper'
export { default } from '@screens/ProjectHomePage'

async function getProjectWorkflows([project]) {
const workflows = await fetchWorkflowsHelper('en', project.links['active_workflows'], project.configuration['default_workflow'])
return workflows
}
export async function getServerSideProps({ params, req, res }) {
const { owner, project } = params
const query = {
slug: `${owner}/${project}`
}
const response = await projects.get({ query })
const workflows = await getProjectWorkflows(response.body.projects)
const props = { workflows }
return ({ props })
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ const RemainingHeightBox = styled(Box)`
`

function ProjectHomePage (props) {
const { workflows } = props
return (
<>
<Media at='default'>
<ZooHeaderWrapper />
<ProjectHeader />
<Announcements />
<Hero />
<Hero workflows={workflows} />
<Box margin='small' gap='small'>
<ThemeModeToggle />
<ZooniverseTalk />
Expand All @@ -46,7 +47,7 @@ function ProjectHomePage (props) {
<ProjectHeader />
<Announcements />
<RemainingHeightBox>
<Hero isWide={true} />
<Hero workflows={workflows} isWide={true} />
</RemainingHeightBox>
</FullHeightBox>
<Box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,20 @@ import WideLayout from './components/WideLayout'
import NarrowLayout from './components/NarrowLayout'
import fetchWorkflowsHelper from './helpers/fetchWorkflowsHelper'

function storeMapper (stores) {
return {
activeWorkflows: stores.store.project.links['active_workflows'],
defaultWorkflow: stores.store.project.configuration['default_workflow']
}
}

class HeroContainer extends Component {
constructor () {
super()
this.state = {
workflows: {
loading: asyncStates.initialized,
data: []
}
}
}

componentDidMount () {
return this.fetchWorkflows()
}

async fetchWorkflows () {
this.setState(state => ({
workflows: {
...state.workflows,
loading: asyncStates.loading
}
}))
try {
const { activeWorkflows, defaultWorkflow, language } = this.props
const workflows = await fetchWorkflowsHelper(language, activeWorkflows, defaultWorkflow)
this.setState({
workflows: {
loading: asyncStates.success,
data: workflows
}
})
} catch (error) {
if (process.browser) {
console.error(error)
}
this.setState(state => ({
workflows: {
...state.workflows,
loading: asyncStates.error
}
}))
}
}

render () {
const { workflows } = this.state
const { isWide } = this.props
console.log('hero props', this.props)
const { isWide, workflows } = this.props
const workflowData = {
loading: asyncStates.success,
data: workflows
}
return isWide
? <WideLayout workflows={workflows} />
: <NarrowLayout workflows={workflows} />
? <WideLayout workflows={workflowData} />
: <NarrowLayout workflows={workflowData} />

}
}

HeroContainer.propTypes = {
activeWorkflows: arrayOf(string),
defaultWorkflow: string,
language: string
}

HeroContainer.defaultProps = {
activeWorkflows: [],
defaultWorkflow: '',
language: 'en'
}

@inject(storeMapper)
@observer
class DecoratedHeroContainer extends HeroContainer { }

export {
DecoratedHeroContainer as default,
HeroContainer
}
export default HeroContainer

0 comments on commit 1314c84

Please sign in to comment.