-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vinogradov
committed
Jun 19, 2018
1 parent
8587001
commit 99b5d12
Showing
5 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createSelector } from 'reselect'; | ||
|
||
const shopItemsSelector = state => state.shop.items; | ||
const taxPercentSelector = state => state.shop.taxPercent; | ||
|
||
const subtotalSelector = createSelector( | ||
shopItemsSelector, | ||
items => items.reduce((acc, item) => acc + item.value, 0) | ||
); | ||
|
||
const taxSelector = createSelector( | ||
subtotalSelector, | ||
taxPercentSelector, | ||
(subtotal, taxPercent) => subtotal * (taxPercent / 100) | ||
); | ||
|
||
export const totalSelector = createSelector( | ||
subtotalSelector, | ||
taxSelector, | ||
(subtotal, tax) => ({ total: subtotal + tax }) | ||
); | ||
|
||
const exampleState = { | ||
shop: { | ||
taxPercent: 8, | ||
items: [ | ||
{ name: 'apple', value: 1.20 }, | ||
{ name: 'orange', value: 0.95 }, | ||
] | ||
} | ||
}; | ||
|
||
/* eslint-disable no-console */ | ||
console.log(subtotalSelector(exampleState)); // 2.15 | ||
console.log(taxSelector(exampleState)); // 0.172 | ||
console.log(totalSelector(exampleState)); // { total: 2.322 } | ||
/* eslint-enable no-console */ | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5239,6 +5239,10 @@ [email protected], [email protected]: | |
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" | ||
|
||
[email protected]: | ||
version "3.0.1" | ||
resolved "https://registry.yarnpkg.com/reselect/-/reselect-3.0.1.tgz#efdaa98ea7451324d092b2b2163a6a1d7a9a2147" | ||
|
||
resolve-from@^1.0.0: | ||
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226" | ||
|