<commit_before>import { connect } from 'preact-redux'
import { compose } from 'react-apollo'
import { graphql } from 'react-apollo'
import { reduxForm, formValueSelector, SubmissionError } from 'redux-form'

import { memoryCreate, memoryUpdate, memoryAssetCreate, memoryAssetDelete } from '../queries'
import MemoryForm from './memory-form'

const FORM = 'memoryForm'

const REQUIRED_FIELDS = [
  'ownerFirstName', 'ownerLastName', 'ownerEmail',
  'ownerCountry', 'victimName', 'victimBornAt', 'victimDeadAt',
  'victimCity', 'victimHistory', 'victimRememberText',
  'victimPhoto', 'victimSilhouette', 'authorizedToSite',
]

const validate = (values) => {
  const errors = {}

  REQUIRED_FIELDS.map((fieldName) => {
    if (!values[fieldName]) {
      errors[fieldName] = 'Preenchimento obrigatório'
    }
  })

  return errors
}

const mapStateToProps = (state, props) => {
  const selector = formValueSelector(FORM)
  return {
    ...selector(state, 'victimName', 'authorizedToSite'),
    initialValues: props.memory
  }
}

const mapActionCreatorsToProps = (dispatch, props) => ({
  ...props,
  onSubmit: ({ memoryAssets, ...values }) => {
    if (props.memory) {
      return props.onUpdateMemory({ variables: { ...values, nodeId: props.memory.nodeId } })
        .then(({ data: { updateMemory: { memory } } }) => {
          memory.memoryAssetsByMemoryId.nodes.map(asset => {
            props.onDeleteMemoryAsset({ variables: asset })
          })
          memoryAssets.map(asset => {
            if (asset) {
              props.onCreateMemoryAsset({
                variables: {
                  memoryId: memory.id,
                  assetType: 'image',
                  updatedAt: new Date(),
                  assetUrl: asset.assetUrl
                }
              })
            }
          })
          return Promise.resolve()
        })
        .catch(error => {
          throw new SubmissionError('(500) Internal server error')
        })
    }

    return props.onCreateMemory({ variables: values })
      .then(({ data: { createMemory: { memory } } }) => {
        memoryAssets.map(memoryAsset => {
          if (memoryAsset) {
            props.onCreateMemoryAsset({
              variables: {
                memoryId: memory.id,
                assetType: 'image',
                updatedAt: new Date(),
                assetUrl: memoryAsset.assetUrl
              }
            })
          }
        })
        return Promise.resolve()
      })
      .catch(error => {
        throw new SubmissionError('(500) Internal server error')
      })
  }
})

export default compose(
  graphql(memoryCreate, { name: 'onCreateMemory' }),
  graphql(memoryAssetCreate, { name: 'onCreateMemoryAsset' }),
  graphql(memoryUpdate, { name: 'onUpdateMemory' }),
  graphql(memoryAssetDelete, { name: 'onDeleteMemoryAsset' })
)(connect(mapStateToProps, mapActionCreatorsToProps)(
  reduxForm({ form: FORM, validate })(MemoryForm),
))<commit_msg>chore(loader-hoc): add loader hoc to memory create form close #56<commit_after>import { connect } from 'preact-redux'
import { graphql, compose } from 'react-apollo'
import { reduxForm, formValueSelector, SubmissionError } from 'redux-form'
import { hoc } from '../../utils/preact'
import LoaderHOC from '../../loader'
import { memoryCreate, memoryUpdate, memoryAssetCreate, memoryAssetDelete } from '../queries'
import MemoryForm from './memory-form'

const FORM = 'memoryForm'

const REQUIRED_FIELDS = [
  'ownerFirstName', 'ownerLastName', 'ownerEmail',
  'ownerCountry', 'victimName', 'victimBornAt', 'victimDeadAt',
  'victimCity', 'victimHistory', 'victimRememberText',
  'victimPhoto', 'victimSilhouette', 'authorizedToSite',
]

const validate = (values) => {
  const errors = {}

  REQUIRED_FIELDS.map((fieldName) => {
    if (!values[fieldName]) {
      errors[fieldName] = 'Preenchimento obrigatório'
    }
    return fieldName
  })

  return errors
}

const mapStateToProps = (state, props) => {
  const selector = formValueSelector(FORM)
  return {
    ...selector(state, 'victimName', 'authorizedToSite'),
    initialValues: props.memory,
  }
}

const mapActionCreatorsToProps = (dispatch, props) => ({
  ...props,
  onSubmit: ({ memoryAssets, ...values }) => {
    if (props.memory) {
      return props.onUpdateMemory({ variables: { ...values, nodeId: props.memory.nodeId } })
        .then(({ data: { updateMemory: { memory } } }) => {
          memory.memoryAssetsByMemoryId.nodes.map((asset) => {
            props.onDeleteMemoryAsset({ variables: asset })
            return asset
          })
          memoryAssets.map((asset) => {
            if (asset) {
              props.onCreateMemoryAsset({
                variables: {
                  memoryId: memory.id,
                  assetType: 'image',
                  updatedAt: new Date(),
                  assetUrl: asset.assetUrl,
                },
              })
            }
            return asset
          })
          return Promise.resolve()
        })
        .catch(() => {
          throw new SubmissionError('(500) Internal server error')
        })
    }

    return props.onCreateMemory({ variables: values })
      .then(({ data: { createMemory: { memory } } }) => {
        memoryAssets.map((memoryAsset) => {
          if (memoryAsset) {
            props.onCreateMemoryAsset({
              variables: {
                memoryId: memory.id,
                assetType: 'image',
                updatedAt: new Date(),
                assetUrl: memoryAsset.assetUrl,
              },
            })
          }
          return memoryAsset
        })
        return Promise.resolve()
      })
      .catch(() => {
        throw new SubmissionError('(500) Internal server error')
      })
  },
})

export default compose(
  graphql(memoryCreate, { name: 'onCreateMemory' }),
  graphql(memoryAssetCreate, { name: 'onCreateMemoryAsset' }),
  graphql(memoryUpdate, { name: 'onUpdateMemory' }),
  graphql(memoryAssetDelete, { name: 'onDeleteMemoryAsset' }),
)(connect(mapStateToProps, mapActionCreatorsToProps)(
  reduxForm({ form: FORM, validate })(
    hoc(LoaderHOC, MemoryForm),
  ),
))
{'fraction_non_alphanumeric': 0.13508228776275052, 'fraction_numerical': 0.0022812449079354733, 'mean_word_length': 2.413793103448276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3947797', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1753, 'n_words': 369}