-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Prices Analytics Page #2384
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { Col, Row } from 'reactstrap'; | ||
|
||
import ErrorBoundary from 'components/ErrorBoundary'; | ||
import { compareStrings, SortableTable } from 'components/SortableTable'; | ||
import { cardEtchedPrice, cardFoilPrice, cardNormalPrice, cardPrice, cardStatus } from 'utils/Card'; | ||
|
||
const Prices = ({ cards }) => { | ||
const getValue = (collection, allowedStatus, allowedFinish) => { | ||
const allowedStatuses = []; | ||
if (allowedStatus === 'Owned' || allowedStatus === 'Any') allowedStatuses.push('Ordered', 'Owned', 'Premium Owned'); | ||
if (allowedStatus === 'Not Owned' || allowedStatus === 'Any') allowedStatuses.push('Not Owned', 'Proxied'); | ||
|
||
let priceFn; | ||
switch (allowedFinish) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is OK, just preemptively flagging this as something that we might want to extract into a separate function in the future, should this functionality (getting the price of a card in a specified finish) become more widely used. |
||
case 'Non-foil': | ||
priceFn = cardNormalPrice; | ||
break; | ||
case 'Foil': | ||
priceFn = cardFoilPrice; | ||
break; | ||
case 'Etched': | ||
priceFn = cardEtchedPrice; | ||
break; | ||
default: | ||
priceFn = cardPrice; | ||
} | ||
|
||
return '$'.concat( | ||
parseFloat( | ||
collection | ||
.map((card) => { | ||
if (allowedStatuses.find((status) => status === cardStatus(card))) return priceFn(card) ?? 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition can be simplified as a call to The |
||
return 0; | ||
}) | ||
.reduce((acc, price) => acc + price), | ||
).toFixed(2), | ||
); | ||
}; | ||
|
||
const prices = useMemo( | ||
() => | ||
['Non-foil', 'Foil', 'Etched', 'All'].map((finish) => ({ | ||
label: finish, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this labeling is a little unclear, in that it's not really self-evident what the cell values mean. For example, for Feel free to come up with your own way of clarifying this, but for my money, since the headings really just represent ownership status, why not label them that way? Meaning you'd just use "Owned" as a heading instead of "Current Value" etc. I feel like that's much more clear, if ever so slightly less natural. |
||
currentValue: getValue(cards, 'Owned', finish), | ||
toComplete: getValue(cards, 'Not Owned', finish), | ||
totalValue: getValue(cards, 'Any', finish), | ||
})), | ||
[cards], | ||
); | ||
|
||
return ( | ||
<> | ||
<Row> | ||
<Col> | ||
<h4 className="d-lg-block d-none">Prices</h4> | ||
<p>View the expected value of cards owned and unowned.</p> | ||
</Col> | ||
</Row> | ||
<ErrorBoundary> | ||
<SortableTable | ||
columnProps={[ | ||
{ key: 'label', title: 'Finish', heading: true, sortable: true }, | ||
{ key: 'currentValue', title: 'Current Value', heading: false, sortable: true }, | ||
{ key: 'toComplete', title: 'To Complete', heading: false, sortable: true }, | ||
{ key: 'totalValue', title: 'Total Value', heading: false, sortable: true }, | ||
]} | ||
data={prices} | ||
sortFns={{ label: compareStrings }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you might have a problem if you try to sort by any of the price columns. You're passing the price data as strings (prefixed with "$"), but the default comparison function is just I would suggest keeping the data returned from |
||
/> | ||
</ErrorBoundary> | ||
</> | ||
); | ||
}; | ||
|
||
Prices.propTypes = { | ||
cards: PropTypes.arrayOf(PropTypes.shape({})).isRequired, | ||
}; | ||
|
||
export default Prices; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be nested inside the React component? It doesn't depend on any props, so I don't see a reason to recreate it on every render.