Created
June 21, 2023 22:02
-
-
Save acabreragnz/9c5e47d5ce7b7eae0506d81dbf590447 to your computer and use it in GitHub Desktop.
Acapedia - Vuex store using the featureFlagProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createStore } from 'vuex'; | |
import axios from 'axios'; | |
import { featureFlagProvider } from '@/lib/featureFlag'; | |
export default createStore({ | |
state: { | |
products: [], | |
}, | |
mutations: { | |
SET_PRODUCTS(state, products) { | |
state.products = products; | |
}, | |
}, | |
actions: { | |
async fetchProducts({ commit }) { | |
const response = await axios.get('https://api.example.com/products'); | |
let products = response.data; | |
// If the feature flag is enabled, we fetch additional data (e.g. rating) for each product | |
if (featureFlagProvider.isFeatureEnabled('ADD_PRODUCT_RATING')) { | |
const ratingResponses = await Promise.all( | |
products.map(product => | |
axios.get(`https://api.example.com/products/${product.id}/rating`) | |
) | |
); | |
products = products.map((product, index) => ({ | |
...product, | |
rating: ratingResponses[index].data, | |
})); | |
} | |
commit('SET_PRODUCTS', products); | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment