Skip to content

Instantly share code, notes, and snippets.

@chranderson
Created October 29, 2015 15:08
Show Gist options
  • Save chranderson/72194f8bf58b78660cbe to your computer and use it in GitHub Desktop.
Save chranderson/72194f8bf58b78660cbe to your computer and use it in GitHub Desktop.
Push new objects to array upon DATA_SUCCESS action from api get request
const initialState = {
fieldMap = []
}
// take result, push objects to fielMap array
const createFieldMap = (result, fieldMap, fieldMapMade) => {
if (!fieldMapMade) {
result.headers.map((key, index) => {
const cardIndex = index;
const samplesArr = [];
result.samples.map((sample) => {
const newArray = __.at(sample, cardIndex).toString() || '-';
samplesArr.push(newArray);
});
fieldMap.push({header: key, dataType: '', samples: samplesArr});
});
return { fieldMap };
}
};
// when I get the result from the API (scala api), i run this function to push results to fieldMap array -
case DATA_SUCCESS:
return {
...state,
next: createFieldMap(action.result, state.fieldMap, state.fieldMapMade),
response: action.res,
gotData: true,
error: null,
};
// Action Creator
export function loadClientData(clientId, userEmail, fileName) {
return {
types: [ LOAD_DATA, DATA_SUCCESS, DATA_FAILURE ],
promise: (client) => client.get('/v3/client/' + clientId + '/files/' + fileName + '/sample', {
headers: { 'userEmail': userEmail, 'name': fileName }
})
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment