/* @flow */
import { FETCHED_MEMBER } from '../../../shared/redux/modules/member.js'
import { mergeAll, converge, unapply, compose, objOf, prop } from 'ramda'

import type { Action, Reducer } from 'redux'

type State = {}

const reducer: Reducer<State, Action> =
  (state = { id: 0, address: [], name: '' }, { type, payload }) => {
    switch (type) {
      case FETCHED_MEMBER:
        return converge
          ( unapply(mergeAll)
          , [ compose(objOf('name'), getName)
            , compose(objOf('address'), getAddress)
            , compose(objOf('id'), prop('id'))
            ]
          )(payload)
      default:
        return state
    }
  }

const getName = ({ title, full_name }) => title + ' ' + full_name

const getAddress = (
  { title
  , first_name = ''
  , last_name = ''
  , address1
  , address2
  , address3
  , address4
  , county
  , postcode
  }
) =>
  [ `${title} ${first_name[0] || ''} ${last_name}`, address1, address2,
    address3, address4, county, postcode ]

export default reducer


@@ -891,16 +891,31 @@
 itle%7D $%7B
+(first_name && 
 first_na
@@ -919,16 +919,17 @@
 _name%5B0%5D
+)
  %7C%7C ''%7D 
@@ -941,16 +941,19 @@
 t_name%7D%60
+%0A  
 , addres
@@ -954,16 +954,19 @@
 address1
+%0A  
 , addres
@@ -971,13 +971,12 @@
 ess2
-,
 %0A  
- 
+,
  add
@@ -980,16 +980,19 @@
 address3
+%0A  
 , addres
@@ -993,24 +993,27 @@
 address4
+%0A  
 , county
 , postco
@@ -1004,16 +1004,19 @@
 , county
+%0A  
 , postco
@@ -1044,9 +1044,8 @@
 reducer%0A
-%0A

Fix bug that meant member details not displaying if no first name had been entered.