@@ -18,9 +18,6 @@ import getMessage from '../utils/getMessage';
1818export const EditorActions = {
1919 ACTIVE_PHASE : 'activePhase' ,
2020 ADD_ATTACHMENT : 'addAttachment' ,
21- ADD_CONTACT_FAILED : 'addContactFailed' ,
22- ADD_CONTACT_SUCCESS : 'addContactSuccess' ,
23- ADD_CONTACT : 'addContact' ,
2421 ADD_LABEL_FAILED : 'addLabelFailed' ,
2522 ADD_LABEL_SUCCESS : 'addLabelSuccess' ,
2623 ADD_LABEL : 'addLabel' ,
@@ -200,37 +197,32 @@ export function fetchHearingEditorContactPersons() {
200197
201198export const changeHearing = ( field , value ) => dispatch => dispatch ( createAction ( EditorActions . EDIT_HEARING ) ( { field, value } ) ) ;
202199
203- export function addContact ( contact , selectedContacts ) {
204- return ( dispatch ) => {
205- const postContactAction = createAction ( EditorActions . ADD_CONTACT ) ( ) ;
206- dispatch ( postContactAction ) ;
207- const url = '/v1/contact_person/' ;
208- return post ( url , contact )
209- . then ( checkResponseStatus )
210- . then ( response => {
211- if ( response . status === 400 ) {
212- response . json ( ) . then ( errors => {
213- dispatch ( createAction ( EditorActions . ADD_CONTACT_FAILED ) ( { errors } ) ) ;
214- } ) ;
215- // Bad request with error message
216- throw Error ( 'Tarkista yhteyshenkilön tiedot.' ) ;
217- } else if ( response . status === 401 ) {
218- // Unauthorized
219- throw Error ( 'Et voi luoda yhteyshenkilöä.' ) ;
220- } else {
221- response . json ( ) . then ( contactJSON => {
222- selectedContacts . push ( contactJSON . id ) ;
223- dispatch ( createAction ( EditorActions . ADD_CONTACT_SUCCESS ) ( { contact : contactJSON } ) ) ;
224- dispatch ( changeHearing ( 'contact_persons' , selectedContacts ) ) ;
225- } ) ;
226- // TODO: Add translations
227- dispatch ( addToast ( createNotificationPayload ( NOTIFICATION_TYPES . success , HEARING_CREATED_MESSAGE ) ) ) ;
228- }
229- } )
230- . then ( ( ) => dispatch ( fetchHearingEditorContactPersons ( ) ) )
231- . catch ( requestErrorHandler ( dispatch ) ) ;
232- } ;
233- }
200+ export const addContact = ( contact , selectedContacts ) => async dispatch => {
201+ const url = '/v1/contact_person/' ;
202+ return post ( url , contact )
203+ . then ( checkResponseStatus )
204+ . then ( async response => {
205+ if ( response . status === 400 ) {
206+ // Bad request with error message
207+ throw Error ( 'Tarkista yhteyshenkilön tiedot.' ) ;
208+ } else if ( response . status === 401 ) {
209+ // Unauthorized
210+ throw Error ( 'Et voi luoda yhteyshenkilöä.' ) ;
211+ } else {
212+ response . json ( ) . then ( async contactJSON => {
213+ selectedContacts . push ( contactJSON . id ) ;
214+ await dispatch ( changeHearing ( 'contact_persons' , selectedContacts ) ) ;
215+ await dispatch ( fetchHearingEditorContactPersons ( ) ) ;
216+ } ) ;
217+ // TODO: Add translations
218+ dispatch ( addToast ( createNotificationPayload ( NOTIFICATION_TYPES . success , HEARING_CREATED_MESSAGE ) ) ) ;
219+ }
220+ } )
221+ . catch ( ( err ) => {
222+ requestErrorHandler ( dispatch ) ( err ) ;
223+ return Promise . reject ( err ) ;
224+ } ) . finally ( ( ) => true )
225+ } ;
234226
235227export function saveContact ( contact ) {
236228 return ( dispatch ) => {
0 commit comments