Skip to content

Instantly share code, notes, and snippets.

@lmuntaner
Last active January 24, 2018 06:32
Show Gist options
  • Save lmuntaner/dca17dddc78e889dde1b5cb436c75177 to your computer and use it in GitHub Desktop.
Save lmuntaner/dca17dddc78e889dde1b5cb436c75177 to your computer and use it in GitHub Desktop.
Snippets for: How we connect Redux to our Services API article
const getFiles = () => ({
type: API,
path: '/files',
onSuccess: addFiles,
});
const addFiles = (files) => ({
type: ADD_FILES,
payload: files,
});
const middleware = ({ dispatch, getState }) => (next) => (action) => {
if (action.type !== API) {
return next(action);
}
const token = getState().user.token;
const headers = new Headers({
Authorization: `Bearer ${token}`,
});
// see how we use `action.path`. Which in our example is `/files`
const url = `https://www.someurl.com/api${action.path}`;
return fetch(url, { headers })
.then((res) => res.json())
// `action.onSuccess` was `addFiles` which is an Action Creator
.then((data) => dispatch(action.onSuccess(data)));
}
const getFiles = () => async ({ dispatch }) => {
const response = await services.getFiles();
const files = await res.json();
const finalAction = addFiles(files);
return dispatch(finalAction);
};
const getFiles = () => async ({ dispatch, getState }) => {
const token = getState().user.token;
const headers = new Headers({
Authorization: `Bearer ${token}`,
});
const url = 'https://www.someurl.com/api/files';
const response = await fetch(url, { headers });
const files = await res.json();
const finalAction = addFiles(files);
return dispatch(finalAction);
}
const myFetchFactory = () => (url, configuration) => {
return fetch(url, configuration)
};
const myFetch = myFetchFactory();
const myFetchFactory = (store) => (url, configuration) => {
const token = store.getState().user.token;
const headers = new Headers({
Authorization: `Bearer ${token}`,
});
const newConfiguration = {
...configuration,
headers,
};
return fetch(url, newConfiguration);
};
const myFetch = myFetchFactory(store);
// Export `myFetch`
export default myFetch;
import appStore from 'store';
const myFetchFactory = (store) => (url, configuration) => {
return fetch(url, configuration)
};
const myFetch = myFetchFactory(store);
const myFetch = (url, configuration) => {
return fetch(url, configuration)
};
const services = {
getFiles: () => {
const url = 'https://www.someurl.com/api/files';
return fetch(url);
},
}
// Import our dependency
import myFetch from './myFetch';
const servicesFactory = (myFetch) => ({
getFiles: () => {
const url = 'https://www.someurl.com/api/files';
return myFetch(url);
},
});
const services = servicesFactory(myFetch);
const uploadFile = (file) => async ({ dispatch }) => {
const response = await services.getUploadUrl(file);
const { newFile, uploadUrl } = await response.json():
// we have a helper that upload a file to a URL
await uploadFile(file, uploadUrl);
// the polling starts
let retries = 0;
let exists = false;
while (!exists || retries <= MAX_RETRIES) {
exists = await services.fileExists(newFile.id);
retries += 1;
}
if (!exists) {
return dispatch(errorNotification('file did not upload successfully, please try again'))
}
return dispatch(addFile(newFile));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment