-
-
Save romanlex/d12ae352dd58290f48b230374f007fc2 to your computer and use it in GitHub Desktop.
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
const emptyCounterparties = []; | |
const emptyProducts = []; | |
/** Список банков */ | |
export const getCounterparties = (state) => state.orders.data.counterparties; | |
/** Конкретный банк */ | |
export const getCounterparty = (state, props) => | |
state.orders.data.counterparties[props.bankId] || null; | |
/** Список продуктов */ | |
export const getProducts = (state) => state.orders.data.products; | |
/** Список id выбранных продуктов */ | |
export const getSelectedProductsIds = (state) => state.orders.data.selectedProducts; | |
/** Конкретный продукт */ | |
export const getProduct = (state, props) => | |
state.orders.data.products[props.productId] || null; | |
/** Продукт выбранного оффера */ | |
export const getSelectedOfferProduct = (state) => | |
state.orders.data.products[state.orders.data.selectedOfferProduct] || null; | |
/** Банк выбранного оффера */ | |
export const getSelectedOfferBank = (state) => { | |
const product = getSelectedOfferProduct(state); | |
if (!product) { | |
return null; | |
} | |
return state.orders.data.counterparties[product.product.counter_party] || null; | |
}; | |
/** Продукт выбран пользователем */ | |
export const isProductSelected = (state, props) => { | |
const product = getProduct(state, props); | |
return product ? state.orders.data.selectedProducts.has(product.product.id) : false; | |
}; | |
/** Банк конкретного продукта */ | |
export const getProductCounterparty = (state, props) => { | |
const product = getProduct(state, props); | |
let counterparty = null; | |
if (product) { | |
const bankId = product.product.counter_party; | |
counterparty = getCounterparty(state, { bankId }); | |
} | |
return counterparty; | |
}; | |
/** Массив банков */ | |
export const getCounterpartiesArray = createSelector( | |
[getCounterparties], | |
(counterparties) => { | |
if (isNull(counterparties)) { | |
return emptyCounterparties; | |
} | |
return Object.values(counterparties).sort(sortCounterparties); | |
}, | |
); | |
/** Массив банков c кредитными продуктами */ | |
export const getCounterpartiesWithCreditsArray = createSelector( | |
[getCounterpartiesArray, getProducts], | |
(counterparties, products) => { | |
return counterparties.filter((bank) => { | |
return bank.products | |
.map((productId) => products[productId]) | |
.filter(filterDeferred) | |
.length > 0; | |
}); | |
}, | |
); | |
/** Массив банков c продуктами в рассрочку */ | |
export const getCounterpartiesWithDeferredPaymentsArray = createSelector( | |
[getCounterpartiesArray, getProducts], | |
(counterparties, products) => { | |
return counterparties.filter((bank) => { | |
return bank.products | |
.map((productId) => products[productId]) | |
.filter(filterCredit) | |
.length > 0; | |
}); | |
}, | |
); | |
/** Массив продуктов */ | |
export const getProductsArray = createSelector( | |
[getProducts], | |
(products) => { | |
if (isNull(products)) { | |
return emptyProducts; | |
} | |
return Object.values(products).sort(sortProducts); | |
}, | |
); | |
/** Массив выбранных продуктов */ | |
export const getSelectedProductsArray = createSelector( | |
[getProductsArray, getSelectedProductsIds], | |
(products, selectedIds) => { | |
return products.filter((product) => selectedIds.has(product.product.id)); | |
}, | |
); | |
/** Массив доступных продуктов */ | |
export const getAvailableProductsArray = createSelector( | |
[getProductsArray], | |
(products) => { | |
return products.filter(filterPartial); | |
}, | |
); | |
/** Массив продуктов для банка */ | |
export const getCounterpartyProducts = createSelector( | |
[getCounterparty, getProducts], | |
(counterparty, products) => { | |
return counterparty | |
? counterparty.products.map((productId) => products[productId]) | |
.sort(sortProducts) | |
: []; | |
}, | |
); | |
/** Массив кредитных продуктов для банка */ | |
export const getCreditCounterpartyProducts = createSelector( | |
[getCounterpartyProducts], | |
(products) => products.filter(filterDeferred), | |
); | |
/** Массив продуктов в рассрочку для банка */ | |
export const getDeferredCounterpartyProducts = createSelector( | |
[getCounterpartyProducts], | |
(products) => products.filter(filterCredit), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment