Last active
April 29, 2017 23:25
-
-
Save DanCassiano/6add0042720614e90ba36c7c88844754 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from 'isomorphic-fetch'; | |
import * as types from './types'; | |
export function submitForm(formData) { | |
return dispatch => { | |
dispatch(trySubmit()); //tentando fazer o submit | |
return fetch("http://localhost:3004/contact", { | |
method: "POST", | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(formData) | |
}) | |
.then(handleResults) | |
.then(result => { | |
// em caso de sucesso | |
dispatch(submitSuccess("Enviado com sucesso")); | |
return result; | |
}) | |
.catch(error => { | |
// em caso de erro | |
dispatch(submitFailure("Erro ao enviar")); | |
return error; | |
}); | |
}; | |
} | |
export function trySubmit() { | |
return { type: types.TRY_SUBMIT }; | |
} | |
export function submitSuccess(msg) { | |
return { type: types.SUBMIT_SUCCESS, msg }; | |
} | |
export function submitFailure(msg) { | |
return { type: types.SUBMIT_FAILURE, msg }; | |
} | |
export function handleResults(response) { | |
if(response.ok) return response.json(); | |
const error = response.json(); | |
throw error; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment