Skip to content

Abstract Work #4

Description

@kennyctran

const getProductStyles = (productID) => {
return new Promise((resolve, reject) => {
q.queryStylesSKUsPhotos(productID)
.then(results => {
let [styles, photos, skus] = results;
let photosHash = {}
let skusHash = {}
photos.forEach(photo => photosHash[photo.Style_ID] === undefined ?
photosHash[photo.Style_ID] = [{thumbnail_url: photo.Thumbnail_URL, url: photo.URL}]
: photosHash[photo.Style_ID].push({thumbnail_url: photo.Thumbnail_URL, url: photo.URL}))
skus.forEach(sku => {
if (skusHash[sku.Style_ID] === undefined) {
skusHash[sku.Style_ID] = {};
skusHash[sku.Style_ID][sku.ID] = {quantity: sku.Quantity, size: sku.Size}
} else {
skusHash[sku.Style_ID][sku.ID] = {quantity: sku.Quantity, size: sku.Size}
}
})
let formattedData = {
product_id: `${productID}`,
results: []
}
class Style {
constructor (styleId, name, originalPrice, salePrice, defaultStyle, photos, skus) {
this.style_id = styleId,
this.name = name,
this.original_price = originalPrice,
this.sale_price = salePrice,
this['default?'] = defaultStyle,
this.photos = photos
this.skus = skus
}
}
styles.forEach((style, i)=> {
let isDefault = styles[i].Default_Style === 1 ? true : false
formattedData.results.push(new Style (styles[i].ID, styles[i].Name, styles[i].Original_Price, styles[i].Sale_Price, isDefault, photosHash[`${styles[i].ID}`], skusHash[`${styles[i].ID}`]))
})
resolve(formattedData);
})
.catch(err=>{
reject(err);
});
})
}

These are intensive requests and work being done afterwards as well in this file. Is there a way to abstract the work into smaller functions that you can actually reference when you need them? For example, you should just see that a request to the model is made, it is transformed in some way, and then that transformed data is sent back. You don't necessarily need to know the details of the transformation. If you need to know, you can visit the function that does the transformation!

This makes our code much easier to read.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions