Skip to content

rewrite series sales block to react #1439

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions src/main/frontend/src/components/SeriesSalesList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//
// IMPORTANT:
// You must update ResourceUrl.RESOURCES_VERSION each time whenever you're modified this file!
//

class SeriesSalesList extends React.PureComponent {

constructor(props) {
super(props);
this.state = {
sales: [],
hasServerError: false,
};
this.loadSales = this.loadSales.bind(this);
}

componentDidMount() {
this.loadSales();
}

loadSales() {
this.setState({
hasServerError: false,
sales: []
});

axios.get(this.props.url)
.then(response => {
const data = response.data;
this.setState({ sales: data });

})
.catch(error => {
console.error(error);
this.setState({ hasServerError: true });
});
}

render() {
return (
<SeriesSalesListView
l10n={this.props.l10n}
hasServerError={this.state.hasServerError}
sales={this.state.sales}
/>
)
}
}

class SeriesSalesListView extends React.PureComponent {
render() {
const { hasServerError, l10n, sales } = this.props;

return (
<div className="row">
<div className="col-sm-12">
<h5>{ l10n['t_who_selling_series'] || 'Who was selling/buying this series' }</h5>
<div className="row">
<div id="loading-series-sales-failed-msg"
className={ `alert alert-danger text-center col-sm-8 col-sm-offset-2 ${hasServerError ? '' : 'hidden'}` }>
{ l10n['t_server_error'] || 'Server error' }
</div>
</div>
<ul>
{ sales.map((sale, index) => (
<SeriesSaleItem
key={sale.id}
l10n={this.props.l10n}
sale={sale}
index={index + 1}
/>
))}
</ul>
</div>
</div>
)
}
}

class SeriesSaleItem extends React.PureComponent {
render() {
const { sale, index, l10n } = this.props;
const hasBuyer = !!sale.buyerName;
const hasCondition = !!sale.condition;
const hasDate = !!sale.date;
const hasTransactionUrl = !!sale.transactionUrl;
const hasSecondPrice = !!sale.secondPrice;

return (
<li id={ `series-sale-${index}-info` }>
{ hasDate && sale.date }
{' '}
<ParticipantLink url={sale.sellerUrl} name={sale.sellerName} />
{' '}
{ hasBuyer ?
(l10n['t_sold_to'] || 'sold to')
: (l10n['t_was_selling'] || 'was selling for')
}
{' '}
{ hasBuyer && (<ParticipantLink url={sale.buyerUrl} name={sale.buyerName} />) }
{' '}
{ hasBuyer && (l10n['t_sold_for'] || 'for') }
{' '}
{ hasTransactionUrl ?
<a href={sale.transactionUrl} id={ `series-sale-${index}-transaction` } rel="nofollow">
{ `${sale.firstPrice}\u00A0${sale.firstCurrency}` }
{ hasSecondPrice && `(${sale.secondPrice}\u00A0${sale.secondCurrency})` }
</a>
: <React.Fragment>
{ `${sale.firstPrice}\u00A0${sale.firstCurrency}` }
{ hasSecondPrice && `(${sale.secondPrice}\u00A0${sale.secondCurrency})` }
</React.Fragment>
}
{' '}
{ hasCondition && (sale.condition !== 'CANCELLED' ? sale.condition : (l10n['t_cancelled'] || 'cancelled')) }
</li>
)
}
}

class ParticipantLink extends React.PureComponent {
render() {
const { name, url } = this.props;
const hasUrl = !!url;
return (
hasUrl ?
<a href={url} rel="nofollow">{ name }</a>
: name
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class ResourceUrl {
// MUST be updated when any of our resources were modified
public static final String RESOURCES_VERSION = "v0.4.3.12";

// CheckStyle: ignore LineLength for next 15 lines
// CheckStyle: ignore LineLength for next 18 lines
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 18? You added a one line, so I'd expect that it should be 16

private static final String CATALOG_UTILS_JS = "/public/js/" + RESOURCES_VERSION + "/CatalogUtils.min.js";
private static final String COLLECTION_INFO_JS = "/public/js/" + RESOURCES_VERSION + "/collection/info.min.js";
private static final String DATE_UTILS_JS = "/public/js/" + RESOURCES_VERSION + "/DateUtils.min.js";
Expand All @@ -49,6 +49,8 @@ public final class ResourceUrl {
private static final String CATALOG_PRICE_FORM_JS = "/public/js/" + RESOURCES_VERSION + "/components/AddCatalogPriceForm.js";
private static final String CATALOG_NUMBERS_FORM_JS = "/public/js/" + RESOURCES_VERSION + "/components/AddCatalogNumbersForm.js";
private static final String RELEASE_YEAR_FORM_JS = "/public/js/" + RESOURCES_VERSION + "/components/AddReleaseYearForm.js";
private static final String SERIES_SALES_LIST_JS = "/public/js/" + RESOURCES_VERSION + "/components/SeriesSalesList.js";

private static final String BOOTSTRAP_LANGUAGE = "https://cdn.jsdelivr.net/gh/usrz/bootstrap-languages@3ac2a3d2b27ac43a471cd99e79d378a03b2c6b5f/languages.min.css";
private static final String FAVICON_ICO = "/favicon.ico";

Expand Down Expand Up @@ -88,6 +90,8 @@ public static void exposeResourcesToView(Map<String, String> resources, String h
put(resources, host, "CATALOG_PRICE_FORM_JS", CATALOG_PRICE_FORM_JS);
put(resources, host, "CATALOG_NUMBERS_FORM_JS", CATALOG_NUMBERS_FORM_JS);
put(resources, host, "RELEASE_YEAR_FORM_JS", RELEASE_YEAR_FORM_JS);
put(resources, host, "SERIES_SALES_LIST_JS", SERIES_SALES_LIST_JS);

}

// see also MvcConfig.addResourceHandlers()
Expand Down
139 changes: 137 additions & 2 deletions src/main/webapp/WEB-INF/views/series/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<html lang="en" th:lang="${#locale.language == 'ru' ? 'ru' : 'en'}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
xmlns:togglz="https://github.com/heneke/thymeleaf-extras-togglz">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Expand Down Expand Up @@ -559,7 +560,7 @@ <h5 class="text-center" th:text="#{t_similar_series}">
/*/-->
</div>

<div class="row" th:if="${not #lists.isEmpty(seriesSales)}" sec:authorize="hasAuthority('VIEW_SERIES_SALES')">
<div class="row" th:if="${not #lists.isEmpty(seriesSales)}" sec:authorize="hasAuthority('VIEW_SERIES_SALES')" togglz:inactive="USE_REACT">
<div class="col-sm-12">
<h5 th:text="#{t_who_selling_series}">Who was selling/buying this series</h5>
<ul th:remove="all-but-first">
Expand Down Expand Up @@ -623,6 +624,8 @@ <h5 th:text="#{t_who_selling_series}">Who was selling/buying this series</h5>
</div>
</div>

<div id="series-sales-list" sec:authorize="hasAuthority('VIEW_SERIES_SALES')" togglz:active="USE_REACT"></div>

<div class="row" sec:authorize="hasAuthority('ADD_SERIES_SALES')">
<div class="col-sm-12">
<h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying this series</h5>
Expand Down Expand Up @@ -999,6 +1002,105 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi

responseCount++;

return new Promise(function delayExecution(resolve) {
setTimeout(resolve, 500 /* 0.5 second */);

}).then(function returnResponse() {
return stubResponse.status == 500 ? Promise.reject(stubResponse) : Promise.resolve(stubResponse);
});
},
get: function (url) {
var possibleOutcomes = [ 'success'];
var outcome = possibleOutcomes[responseCount % possibleOutcomes.length];
var possibleResponses = {
'/series/100': {
'success': {
status: 200,
data: [
{
id: 1,
sellerName: 'James Alan Hetfield',
sellerUrl: 'http://example.com/james-alan-hetfield',
buyerName: 'Eicca Toppinen',
buyerUrl: 'http://example.com/eicca-toppinen',
transactionUrl: 'http://example.com/james-alan-hetfield/selling-stamps',
firstPrice: 100,
firstCurrency: 'USD',
condition: 'CANCELLED'
},
{
id: 2,
sellerName: 'James Alan Hetfield',
sellerUrl: 'http://example.com/james-alan-hetfield',
transactionUrl: 'http://example.com/james-alan-hetfield/selling-stamps',
firstPrice: 100,
firstCurrency: 'USD',
secondPrice: 650,
secondCurrency: 'RUB',
condition: 'CANCELLED'
},
{
id: 3,
date: '02.02.2002',
sellerName: 'Tommy Lee Jones',
sellerUrl: 'http://example.com/tommy-lee-jones',
transactionUrl: 'http://example.com/tommy-lee-jones/selling-stamps',
firstPrice: 200,
firstCurrency: 'USD',
condition: 'MNH'
},
{
id: 4,
date: '02.02.2002',
sellerName: 'Tommy Lee Jones',
sellerUrl: 'http://example.com/tommy-lee-jones',
transactionUrl: 'http://example.com/tommy-lee-jones/selling-stamps',
firstPrice: 200,
firstCurrency: 'USD',
secondPrice: 1300,
secondCurrency: 'RUB',
},
{
id: 5,
date: '03.02.2002',
sellerName: 'Eicca Toppinen',
sellerUrl: 'http://example.com/eicca-toppinen',
transactionUrl: 'http://example.com/tommy-lee-jones/selling-stamps',
firstPrice: 300,
firstCurrency: 'USD',
secondPrice: 1560,
secondCurrency: 'RUB',
},
{
id: 6,
date: '03.02.2002',
sellerName: 'Eicca Toppinen',
sellerUrl: 'http://example.com/eicca-toppinen',
buyerName: 'Kurt Cobain',
firstPrice: 300,
firstCurrency: 'USD',
secondPrice: 1560,
secondCurrency: 'RUB',
}
]
}
}
};
var stubResponse;

switch (outcome) {
case 'success':
stubResponse = possibleResponses[url][outcome];
break;
default:
stubResponse = {
status: 500,
statusText: 'Fake Server Error'
};
}

responseCount++;

return new Promise(function delayExecution(resolve) {
setTimeout(resolve, 500 /* 0.5 second */);

Expand Down Expand Up @@ -1105,6 +1207,39 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi
</th:block>
/*/-->

<!--/*/
<th:block sec:authorize="hasAuthority('VIEW_SERIES_SALES')" togglz:active="USE_REACT">
/*/-->
<script src="../../../../../../target/classes/js/components/SeriesSalesList.js" th:src="${SERIES_SALES_LIST_JS}"></script>

<script th:inline="javascript">
/*[+
var seriesSalesListProps = {
'url': [[ '__@{${INFO_SERIES_PAGE}(id=${series.id})}__' ]],
'l10n': {
't_server_error': [[ #{t_server_error} ]],
't_who_selling_series': [[ #{t_who_selling_series} ]],
't_sold_to': [[ #{t_sold_to} ]],
't_sold_for': [[ #{t_sold_for} ]],
't_was_selling': [[ #{t_was_selling} ]],
't_cancelled': [[ #{t_cancelled} ]]
}
};
+]*/

/*[- */
var seriesSalesListProps = {
'url': '/series/100',
'l10n': {}
};
/* -]*/

renderComponent(SeriesSalesList, seriesSalesListProps, 'series-sales-list');
</script>
<!--/*/
</th:block>
/*/-->

<!--/*/
<th:block sec:authorize="hasAuthority('ADD_COMMENTS_TO_SERIES')" th:if="${series.comment == null}">
/*/-->
Expand Down