forked from MapComplete/MapComplete
Refactoring fixes to the review elements
This commit is contained in:
parent
aa1c9d0398
commit
6689e0a658
2 changed files with 41 additions and 50 deletions
|
@ -1,63 +1,54 @@
|
||||||
/**
|
|
||||||
* Shows the reviews and scoring base on mangrove.reviews
|
|
||||||
* The middle element is some other component shown in the middle, e.g. the review input element
|
|
||||||
*/
|
|
||||||
import {UIEventSource} from "../../Logic/UIEventSource";
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
||||||
import {Review} from "../../Logic/Web/Review";
|
import {Review} from "../../Logic/Web/Review";
|
||||||
import {UIElement} from "../UIElement";
|
|
||||||
import Combine from "../Base/Combine";
|
import Combine from "../Base/Combine";
|
||||||
import Translations from "../i18n/Translations";
|
import Translations from "../i18n/Translations";
|
||||||
import SingleReview from "./SingleReview";
|
import SingleReview from "./SingleReview";
|
||||||
import BaseUIElement from "../BaseUIElement";
|
import BaseUIElement from "../BaseUIElement";
|
||||||
|
import Img from "../Base/Img";
|
||||||
|
import {VariableUiElement} from "../Base/VariableUIElement";
|
||||||
|
import Link from "../Base/Link";
|
||||||
|
|
||||||
export default class ReviewElement extends UIElement {
|
/**
|
||||||
private readonly _reviews: UIEventSource<Review[]>;
|
* Shows the reviews and scoring base on mangrove.reviews
|
||||||
private readonly _subject: string;
|
* The middle element is some other component shown in the middle, e.g. the review input element
|
||||||
private readonly _middleElement: BaseUIElement;
|
*/
|
||||||
|
export default class ReviewElement extends VariableUiElement {
|
||||||
|
|
||||||
constructor(subject: string, reviews: UIEventSource<Review[]>, middleElement: BaseUIElement) {
|
constructor(subject: string, reviews: UIEventSource<Review[]>, middleElement: BaseUIElement) {
|
||||||
super(reviews);
|
super(
|
||||||
this._middleElement = middleElement;
|
reviews.map(revs => {
|
||||||
if (reviews === undefined) {
|
const elements = [];
|
||||||
throw "No reviews UIEVentsource Given!"
|
revs.sort((a, b) => (b.date.getTime() - a.date.getTime())); // Sort with most recent first
|
||||||
}
|
const avg = (revs.map(review => review.rating).reduce((a, b) => a + b, 0) / revs.length);
|
||||||
this._reviews = reviews;
|
elements.push(
|
||||||
this._subject = subject;
|
new Combine([
|
||||||
}
|
SingleReview.GenStars(avg),
|
||||||
|
new Link(
|
||||||
|
revs.length === 1 ? Translations.t.reviews.title_singular.Clone() :
|
||||||
|
Translations.t.reviews.title.Clone()
|
||||||
|
.Subs({count: "" + revs.length}),
|
||||||
|
`https://mangrove.reviews/search?sub=${encodeURIComponent(subject)}`,
|
||||||
|
true
|
||||||
|
),
|
||||||
|
])
|
||||||
|
|
||||||
|
.SetClass("font-2xl flex justify-between items-center pl-2 pr-2"));
|
||||||
|
|
||||||
InnerRender(): BaseUIElement {
|
elements.push(middleElement);
|
||||||
|
|
||||||
const elements = [];
|
elements.push(...revs.map(review => new SingleReview(review)));
|
||||||
const revs = this._reviews.data;
|
elements.push(
|
||||||
revs.sort((a, b) => (b.date.getTime() - a.date.getTime())); // Sort with most recent first
|
new Combine([
|
||||||
const avg = (revs.map(review => review.rating).reduce((a, b) => a + b, 0) / revs.length);
|
Translations.t.reviews.attribution.Clone(),
|
||||||
elements.push(
|
new Img('./assets/mangrove_logo.png')
|
||||||
new Combine([
|
])
|
||||||
SingleReview.GenStars(avg),
|
|
||||||
`<a target="_blank" href='https://mangrove.reviews/search?sub=${encodeURIComponent(this._subject)}'>`,
|
|
||||||
revs.length === 1 ? Translations.t.reviews.title_singular :
|
|
||||||
Translations.t.reviews.title
|
|
||||||
.Subs({count: "" + revs.length})
|
|
||||||
,
|
|
||||||
"</a>"
|
|
||||||
])
|
|
||||||
|
|
||||||
.SetClass("font-2xl flex justify-between items-center pl-2 pr-2"));
|
.SetClass("review-attribution"))
|
||||||
|
|
||||||
elements.push(this._middleElement);
|
|
||||||
|
|
||||||
elements.push(...revs.map(review => new SingleReview(review)));
|
return new Combine(elements).SetClass("block");
|
||||||
elements.push(
|
})
|
||||||
new Combine([
|
);
|
||||||
Translations.t.reviews.attribution,
|
|
||||||
"<img src='./assets/mangrove_logo.png'>"
|
|
||||||
])
|
|
||||||
|
|
||||||
.SetClass("review-attribution"))
|
|
||||||
|
|
||||||
return new Combine(elements).SetClass("block");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -4,8 +4,8 @@ import Combine from "../Base/Combine";
|
||||||
import {FixedUiElement} from "../Base/FixedUiElement";
|
import {FixedUiElement} from "../Base/FixedUiElement";
|
||||||
import Translations from "../i18n/Translations";
|
import Translations from "../i18n/Translations";
|
||||||
import {Utils} from "../../Utils";
|
import {Utils} from "../../Utils";
|
||||||
import ReviewElement from "./ReviewElement";
|
|
||||||
import BaseUIElement from "../BaseUIElement";
|
import BaseUIElement from "../BaseUIElement";
|
||||||
|
import Img from "../Base/Img";
|
||||||
|
|
||||||
export default class SingleReview extends UIElement{
|
export default class SingleReview extends UIElement{
|
||||||
private _review: Review;
|
private _review: Review;
|
||||||
|
@ -23,8 +23,8 @@ export default class SingleReview extends UIElement{
|
||||||
}
|
}
|
||||||
const scoreTen = Math.round(rating / 10);
|
const scoreTen = Math.round(rating / 10);
|
||||||
return new Combine([
|
return new Combine([
|
||||||
"<img src='./assets/svg/star.svg' class='h-8 md:h-12'/>".repeat(Math.floor(scoreTen / 2)),
|
...Utils.TimesT(scoreTen / 2, _ => new Img('./assets/svg/star.svg').SetClass("'h-8 w-8 md:h-12")),
|
||||||
scoreTen % 2 == 1 ? "<img src='./assets/svg/star_half.svg' class='h-8 md:h-12'/>" : ""
|
scoreTen % 2 == 1 ? new Img('./assets/svg/star_half.svg').SetClass('h-8 w-8 md:h-12') : undefined
|
||||||
]).SetClass("flex w-max")
|
]).SetClass("flex w-max")
|
||||||
}
|
}
|
||||||
InnerRender(): BaseUIElement {
|
InnerRender(): BaseUIElement {
|
||||||
|
@ -39,7 +39,7 @@ export default class SingleReview extends UIElement{
|
||||||
new Combine([
|
new Combine([
|
||||||
new Combine([
|
new Combine([
|
||||||
|
|
||||||
new Combine(["<b>",review.author,"</b>"]),
|
new FixedUiElement(review.author).SetClass("font-bold"),
|
||||||
review.affiliated ? Translations.t.reviews.affiliated_reviewer_warning : "",
|
review.affiliated ? Translations.t.reviews.affiliated_reviewer_warning : "",
|
||||||
]).SetStyle("margin-right: 0.5em"),
|
]).SetStyle("margin-right: 0.5em"),
|
||||||
new FixedUiElement(`${d.getFullYear()}-${Utils.TwoDigits(d.getMonth() + 1)}-${Utils.TwoDigits(d.getDate())} ${Utils.TwoDigits(d.getHours())}:${Utils.TwoDigits(d.getMinutes())}`)
|
new FixedUiElement(`${d.getFullYear()}-${Utils.TwoDigits(d.getMonth() + 1)}-${Utils.TwoDigits(d.getDate())} ${Utils.TwoDigits(d.getHours())}:${Utils.TwoDigits(d.getMinutes())}`)
|
||||||
|
@ -48,7 +48,7 @@ export default class SingleReview extends UIElement{
|
||||||
|
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
el.SetClass("block p-2 m-1 rounded-xl subtle-background review-element");
|
el.SetClass("block p-2 m-4 rounded-xl subtle-background review-element");
|
||||||
if(review.made_by_user.data){
|
if(review.made_by_user.data){
|
||||||
el.SetClass("border-attention-catch")
|
el.SetClass("border-attention-catch")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue