/
orgupdatepage.js
60 lines (55 loc) · 1.8 KB
/
orgupdatepage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Component } from 'react'
import reduxApi, { withOrgs } from '../../lib/redux/reduxApi.js'
import { FormattedMessage } from 'react-intl'
import OrgDetailForm from '../../components/Org/OrgDetailForm'
import publicPage, { FullPage } from '../../hocs/publicPage'
import { message } from 'antd'
import Router from 'next/router'
class OrgUpdatePage extends Component {
static async getInitialProps ({ store, query }) {
// Get one Org
if (query && query.id) {
const orgs = await store.dispatch(reduxApi.actions.organisations.get(query))
return { orgs, query }
} else {
return {
orgs: [ { org: { name: '', type: ['vp'], about: '', imgUrl: '' } } ],
query
}
}
}
handleCancel = (org) => {
Router.back()
}
async handleAdd (org) {
if (!org) return
// Actual data request
let res = {}
if (org._id) {
res = await this.props.dispatch(reduxApi.actions.organisations.put({ id: org._id }, { body: JSON.stringify(org) }))
} else {
res = await this.props.dispatch(reduxApi.actions.organisations.post({}, { body: JSON.stringify(org) }))
}
org = res[0]
message.success('Saved.')
// go to details page
if (org && org._id) Router.push(`/orgs/${org._id}`)
}
render () {
const org = this.props.orgs[0]
return (
<FullPage>
<h1><FormattedMessage
defaultMessage='Organisation'
id='org.pageheading' />
</h1>
<small><FormattedMessage
defaultMessage='Tell us something about your organisation and link to your home website.'
id='OrgEditPrompt' />
</small>
<OrgDetailForm org={org} onSubmit={this.handleAdd.bind(this, org)} onCancel={this.handleCancel} />
</FullPage>
)
}
}
export default publicPage(withOrgs(OrgUpdatePage))