Repository URL to install this package:
|
Version:
2.8.0-studio-release ▾
|
import React from 'react'
import { observer } from 'xmobx/mobx-react'
import { isSafe, isFunction, isArray, isObj, NO_OP } from 'exotic'
import { omit } from '@skava/utils'
import { ObserverForm, FormState } from 'src/forms'
import { wording } from 'src/words'
import { Empty } from 'atoms/Empty'
import { MyProfileInputList } from '../fixture'
import { StyledButton } from '../styled'
import {
MyProfileForm,
Heading,
HeaderWrapper,
UserIdentifierWrapper,
UserIdentifierText,
UserIdentifierValue,
} from './styled'
import { MyProfileProps } from './typings'
class FormStateCard extends FormState {
inputsList = MyProfileInputList
}
const SubmitButton = props => {
const passThroughProps = omit(props, ['children'])
return (
<StyledButton
text={wording.saveChanges}
{...passThroughProps}
data-qa={'qa-save-changes'}
/>
)
}
const fields = ['firstName', 'lastName', 'email', 'phoneNumber']
@observer
class FormCard<
Props extends MyProfileProps = MyProfileProps
> extends ObserverForm<Props> {
SubmitButton = SubmitButton
constructor(props) {
super(props)
this.state = new FormStateCard()
}
doPreFillForm(props, state) {
const { item, shouldDisablePropsList } = props
const toList = state => isArray(state.inputsList) && state.inputsList
const list = toList(state)
list.map(inputState => {
fields.map(fieldName => {
if (
isSafe(item[fieldName]) &&
inputState.name === fieldName &&
inputState.value !== item[fieldName]
) {
inputState.setValue(item[fieldName])
}
inputState.isDisabled =
isArray(shouldDisablePropsList) &&
shouldDisablePropsList.includes(inputState.name)
})
})
}
componentDidMount() {
this.doPreFillForm(this.props, this.state)
}
componentWillReceiveProps() {
this.doPreFillForm(this.props, this.state)
}
componentWillMount() {
const { hasPhoneNumber } = this.props
const phoneNumberState = this.state.get('phoneNumber')
if (isObj(phoneNumberState)) {
phoneNumberState.isHidden =
isSafe(hasPhoneNumber) && hasPhoneNumber === true ? false : true
}
}
handleSubmit = (event: Event) => {
const { onSaveChanges } = this.props
event.preventDefault()
if (this.validateForm()) {
if (isFunction(onSaveChanges)) {
onSaveChanges(this.state)
}
}
}
}
class MyProfile extends React.PureComponent<MyProfileProps> {
static defaultProps = {
className: '',
headingLabel: 'Account overview',
// Handlers
onSaveChanges: NO_OP,
}
render() {
const { item, headingLabel } = this.props
return (
<MyProfileForm>
<HeaderWrapper>
<Heading breedType="h2" content={headingLabel} />
{isObj(item) && isSafe(item.identifier) ? (
<UserIdentifierWrapper>
<UserIdentifierText>{`${
wording.identifierLabel
}:`}</UserIdentifierText>
<UserIdentifierValue>{item.identifier}</UserIdentifierValue>
</UserIdentifierWrapper>
) : (
<Empty />
)}
</HeaderWrapper>
<FormCard {...this.props} />
</MyProfileForm>
)
}
}
export { MyProfile }
export default MyProfile