2020-12-08 23:44:34 +01:00
|
|
|
import { InputElement } from "../Input/InputElement"
|
|
|
|
import { Review } from "../../Logic/Web/Review"
|
|
|
|
import { UIEventSource } from "../../Logic/UIEventSource"
|
|
|
|
import { TextField } from "../Input/TextField"
|
|
|
|
import Translations from "../i18n/Translations"
|
|
|
|
import Combine from "../Base/Combine"
|
|
|
|
import Svg from "../../Svg"
|
|
|
|
import { VariableUiElement } from "../Base/VariableUIElement"
|
|
|
|
import { SaveButton } from "../Popup/SaveButton"
|
|
|
|
import CheckBoxes from "../Input/Checkboxes"
|
2021-06-14 19:21:33 +02:00
|
|
|
import UserDetails, { OsmConnection } from "../../Logic/Osm/OsmConnection"
|
2021-06-11 22:51:45 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
|
|
|
import Toggle from "../Input/Toggle"
|
2020-12-08 23:44:34 +01:00
|
|
|
|
|
|
|
export default class ReviewForm extends InputElement<Review> {
|
2021-06-21 12:44:26 +02:00
|
|
|
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false)
|
2020-12-08 23:44:34 +01:00
|
|
|
private readonly _value: UIEventSource<Review>
|
2021-06-11 22:51:45 +02:00
|
|
|
private readonly _comment: BaseUIElement
|
|
|
|
private readonly _stars: BaseUIElement
|
|
|
|
private _saveButton: BaseUIElement
|
|
|
|
private readonly _isAffiliated: BaseUIElement
|
|
|
|
private readonly _postingAs: BaseUIElement
|
2021-06-14 19:21:33 +02:00
|
|
|
private readonly _osmConnection: OsmConnection
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2021-06-14 19:21:33 +02:00
|
|
|
constructor(onSave: (r: Review, doneSaving: () => void) => void, osmConnection: OsmConnection) {
|
2020-12-08 23:44:34 +01:00
|
|
|
super()
|
2021-06-14 19:21:33 +02:00
|
|
|
this._osmConnection = osmConnection
|
2021-06-21 12:44:26 +02:00
|
|
|
this._value = new UIEventSource({
|
2020-12-11 16:29:51 +01:00
|
|
|
made_by_user: new UIEventSource<boolean>(true),
|
2020-12-08 23:44:34 +01:00
|
|
|
rating: undefined,
|
|
|
|
comment: undefined,
|
2021-06-14 19:21:33 +02:00
|
|
|
author: osmConnection.userDetails.data.name,
|
2020-12-08 23:44:34 +01:00
|
|
|
affiliated: false,
|
|
|
|
date: new Date(),
|
|
|
|
})
|
|
|
|
const comment = new TextField({
|
2021-06-21 03:25:23 +02:00
|
|
|
placeholder: Translations.t.reviews.write_a_comment.Clone(),
|
2021-06-21 03:07:49 +02:00
|
|
|
htmlType: "area",
|
2020-12-08 23:44:34 +01:00
|
|
|
textAreaRows: 5,
|
|
|
|
})
|
|
|
|
comment.GetValue().addCallback((comment) => {
|
|
|
|
self._value.data.comment = comment
|
|
|
|
self._value.ping()
|
|
|
|
})
|
|
|
|
const self = this
|
2021-06-21 12:44:26 +02:00
|
|
|
|
2021-06-21 03:07:49 +02:00
|
|
|
this._postingAs = new Combine([
|
2021-06-21 03:25:23 +02:00
|
|
|
Translations.t.reviews.posting_as.Clone(),
|
|
|
|
new VariableUiElement(
|
|
|
|
osmConnection.userDetails.map((ud: UserDetails) => ud.name)
|
2021-06-21 12:44:26 +02:00
|
|
|
).SetClass("review-author"),
|
2020-12-18 01:36:45 +01:00
|
|
|
]).SetStyle("display:flex;flex-direction: column;align-items: flex-end;margin-left: auto;")
|
2021-06-21 12:44:26 +02:00
|
|
|
|
|
|
|
const reviewIsSaved = new UIEventSource<boolean>(false)
|
|
|
|
const reviewIsSaving = new UIEventSource<boolean>(false)
|
2020-12-08 23:44:34 +01:00
|
|
|
this._saveButton = new Toggle(
|
2021-06-21 12:44:26 +02:00
|
|
|
Translations.t.reviews.saved.Clone().SetClass("thanks"),
|
|
|
|
new Toggle(
|
|
|
|
Translations.t.reviews.saving_review.Clone(),
|
|
|
|
new SaveButton(
|
|
|
|
this._value.map((r) => self.IsValid(r)),
|
|
|
|
osmConnection
|
|
|
|
).onClick(() => {
|
2022-06-05 02:24:14 +02:00
|
|
|
reviewIsSaving.setData(true)
|
|
|
|
onSave(this._value.data, () => {
|
|
|
|
reviewIsSaved.setData(true)
|
|
|
|
})
|
2021-06-21 12:44:26 +02:00
|
|
|
}),
|
|
|
|
reviewIsSaving
|
|
|
|
),
|
|
|
|
reviewIsSaved
|
|
|
|
).SetClass("break-normal")
|
2020-12-08 23:44:34 +01:00
|
|
|
|
2021-06-21 03:25:23 +02:00
|
|
|
this._isAffiliated = new CheckBoxes([Translations.t.reviews.i_am_affiliated.Clone()])
|
2020-12-08 23:44:34 +01:00
|
|
|
|
|
|
|
this._comment = comment
|
|
|
|
const stars = []
|
|
|
|
for (let i = 1; i <= 5; i++) {
|
|
|
|
stars.push(
|
|
|
|
new VariableUiElement(
|
|
|
|
this._value.map((review) => {
|
|
|
|
if (review.rating === undefined) {
|
|
|
|
return Svg.star_outline.replace(/#000000/g, "#ccc")
|
|
|
|
}
|
|
|
|
return review.rating < i * 20 ? Svg.star_outline : Svg.star
|
|
|
|
})
|
|
|
|
).onClick(() => {
|
|
|
|
self._value.data.rating = i * 20
|
|
|
|
self._value.ping()
|
2022-09-08 21:40:48 +02:00
|
|
|
})
|
2020-12-08 23:44:34 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
this._stars = new Combine(stars).SetClass("review-form-rating")
|
|
|
|
}
|
|
|
|
|
|
|
|
GetValue(): UIEventSource<Review> {
|
|
|
|
return this._value
|
|
|
|
}
|
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
InnerConstructElement(): HTMLElement {
|
|
|
|
const form = new Combine([
|
2021-06-21 03:07:49 +02:00
|
|
|
new Combine([this._stars, this._postingAs]).SetClass("flex"),
|
2020-12-08 23:44:34 +01:00
|
|
|
this._comment,
|
2020-12-11 15:27:52 +01:00
|
|
|
new Combine([this._isAffiliated, this._saveButton]).SetClass("review-form-bottom"),
|
2021-06-21 03:25:23 +02:00
|
|
|
Translations.t.reviews.tos.Clone().SetClass("subtle"),
|
2020-12-08 23:44:34 +01:00
|
|
|
])
|
2021-06-21 03:07:49 +02:00
|
|
|
.SetClass("flex flex-col p-4")
|
2021-06-21 12:44:26 +02:00
|
|
|
.SetStyle(
|
|
|
|
"border-radius: 1em;" +
|
2021-06-21 03:07:49 +02:00
|
|
|
" background-color: var(--subtle-detail-color);" +
|
|
|
|
" color: var(--subtle-detail-color-contrast);" +
|
|
|
|
" border: 2px solid var(--subtle-detail-color-contrast)"
|
|
|
|
)
|
2021-06-21 12:44:26 +02:00
|
|
|
|
2021-06-14 19:21:33 +02:00
|
|
|
const connection = this._osmConnection
|
|
|
|
const login = Translations.t.reviews.plz_login
|
|
|
|
.Clone()
|
|
|
|
.onClick(() => connection.AttemptLogin())
|
2021-06-11 22:51:45 +02:00
|
|
|
|
2021-06-21 12:44:26 +02:00
|
|
|
return new Toggle(form, login, connection.isLoggedIn).ConstructElement()
|
2020-12-08 23:44:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IsValid(r: Review): boolean {
|
|
|
|
if (r === undefined) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
(r.comment?.length ?? 0) <= 1000 &&
|
|
|
|
(r.author?.length ?? 0) <= 20 &&
|
|
|
|
r.rating >= 0 &&
|
|
|
|
r.rating <= 100
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2020-12-08 23:44:34 +01:00
|
|
|
}
|
|
|
|
}
|