Created
October 11, 2021 09:06
-
-
Save keithics/9ef92079df6c6d9f0fcce27a275f25a7 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
export const BIRTH_SUCCESS = 'BIRTH_SUCCESS'; | |
export const birthSuccess = (births) => ({ | |
type: BIRTH_SUCCESS, | |
payload: { births }, | |
}); | |
export const loadBirths = (page) => async (dispatch) => { | |
const response = await request('/certificates/birth/page/', dispatch, page); | |
if (response) { | |
dispatch(birthSuccess(response)); | |
} | |
}; | |
export const request = (url, dispatch, data = null, httpMethod) => { | |
dispatch(requestInProgress()); | |
const method = httpMethod || (!data ? 'get' : 'post'); | |
const token = getToken(); | |
// eslint-disable-next-line no-undef | |
return fetch(getApiUrl() + url, { | |
method, | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `Bearer ${token}`, | |
}, | |
body: data ? JSON.stringify(data) : null, | |
}) | |
.then((response) => { | |
if (response.status === 200) { | |
dispatch(requestSuccess()); | |
return response.json(); | |
} | |
if (response.status === 422) { | |
return response.json().then((a) => { | |
dispatch(validationError(a.message)); | |
return null; | |
}); | |
} | |
dispatch(requestFailure()); | |
return null; | |
}) | |
.catch(() => { | |
dispatch(requestFailure()); | |
return null; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment