mapcomplete/UI/Reviews/ReviewForm.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
5.3 KiB
TypeScript
Raw Normal View History

2020-12-08 23:44:34 +01:00
import { InputElement } from "../Input/InputElement"
import { Review } from "../../Logic/Web/Review"
2023-01-06 03:37:22 +01:00
import { Store, UIEventSource } from "../../Logic/UIEventSource"
2020-12-08 23:44:34 +01:00
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"
2023-01-06 03:37:22 +01:00
import { LoginToggle } from "../Popup/LoginButton"
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
2023-01-06 03:37:22 +01:00
private readonly _state: {
readonly osmConnection: OsmConnection
readonly featureSwitchUserbadge: Store<boolean>
}
2022-09-08 21:40:48 +02:00
2023-01-06 03:37:22 +01:00
constructor(
onSave: (r: Review, doneSaving: () => void) => void,
state: {
readonly osmConnection: OsmConnection
readonly featureSwitchUserbadge: Store<boolean>
}
) {
2020-12-08 23:44:34 +01:00
super()
2023-01-06 03:37:22 +01:00
this._state = state
const osmConnection = state.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"),
]).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(() => {
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
2023-01-06 03:37:22 +01:00
return new LoginToggle(
form,
Translations.t.reviews.plz_login.Clone(),
this._state
).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
}
}