I'm working on a React/Redux application. I receive a data object from the api resembling this:
const dataObj = [
{
id: 1,
name: "Filo",
activity: 9,
info: {
statusCode: 4
}
},
{
id: 2,
name: "Spot",
activity: 8,
info: {
statusCode: 2
}
}
];
I have to run the data through a series of conditional function to create dom elements at different points like this:
const getElementsIcons = dog => {
const elementData = []
elementData.push({...})
if (dog.info.statusCode === ADOPTED_ID) {
elementData.push({...})
}
if (dog.info.statusCode === PENDINGID) {
elementData.push({...})
}
if (dog.info.statusCode === PENDING_ID && dog.activity === OTHER_VALUE) {
elementData.push({...})
}
return elementData
}
However, does it make sense to have this conditional logic inline above, or should I create a 'class' like so:
function Dog(data) {
this.name = data.name;
this.isAdopted = data.statusCode === ADOPTED_ID;
this.otherCondition =
data.statusCode === PENDING_ID && data.activity === OTHER_VALUE;
}
Then deal with the modified data throughout the application?
If it makes sense to create a class, should I do this when I normalize the data from the api call where I store it in redux, or later on when I pass it to the react component that consumes the data?