Skip to content

Instantly share code, notes, and snippets.

@keithics
Created October 11, 2021 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithics/9ef92079df6c6d9f0fcce27a275f25a7 to your computer and use it in GitHub Desktop.
Save keithics/9ef92079df6c6d9f0fcce27a275f25a7 to your computer and use it in GitHub Desktop.
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