2023-01-21 23:58:14 +01:00
|
|
|
import { Review } from "mangrove-reviews-typescript"
|
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"
|
2023-01-21 23:58:14 +01:00
|
|
|
import { CheckBox } 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 Toggle from "../Input/Toggle"
|
2023-01-06 03:37:22 +01:00
|
|
|
import { LoginToggle } from "../Popup/LoginButton"
|
2023-01-21 23:58:14 +01:00
|
|
|
import { SubtleButton } from "../Base/SubtleButton"
|
2020-12-08 23:44:34 +01:00
|
|
|
|
2023-01-21 23:58:14 +01:00
|
|
|
export default class ReviewForm extends LoginToggle {
|
2023-01-06 03:37:22 +01:00
|
|
|
constructor(
|
2023-01-21 23:58:14 +01:00
|
|
|
onSave: (r: Omit<Review, "sub">) => Promise<void>,
|
2023-01-06 03:37:22 +01:00
|
|
|
state: {
|
|
|
|
readonly osmConnection: OsmConnection
|
|
|
|
readonly featureSwitchUserbadge: Store<boolean>
|
|
|
|
}
|
|
|
|
) {
|
2023-01-21 23:58:14 +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,
|
2023-01-21 23:58:14 +01:00
|
|
|
date: new Date(),*/
|
|
|
|
const commentForm = 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,
|
|
|
|
})
|
2021-06-21 12:44:26 +02:00
|
|
|
|
2023-01-21 23:58:14 +01:00
|
|
|
const rating = new UIEventSource<number>(undefined)
|
|
|
|
const isAffiliated = new CheckBox(Translations.t.reviews.i_am_affiliated)
|
|
|
|
const reviewMade = new UIEventSource(false)
|
|
|
|
|
|
|
|
const postingAs = new Combine([
|
2021-06-21 03:25:23 +02:00
|
|
|
Translations.t.reviews.posting_as.Clone(),
|
|
|
|
new VariableUiElement(
|
2023-01-21 23:58:14 +01:00
|
|
|
state.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
|
|
|
|
2023-01-21 23:58:14 +01:00
|
|
|
const saveButton = new Toggle(
|
|
|
|
Translations.t.reviews.no_rating.SetClass("block alert"),
|
2023-05-09 00:06:51 +02:00
|
|
|
new SubtleButton(Svg.confirm_svg(), Translations.t.reviews.save)
|
2023-01-21 23:58:14 +01:00
|
|
|
.OnClickWithLoading(
|
|
|
|
Translations.t.reviews.saving_review.SetClass("alert"),
|
|
|
|
async () => {
|
|
|
|
const review: Omit<Review, "sub"> = {
|
|
|
|
rating: rating.data,
|
|
|
|
opinion: commentForm.GetValue().data,
|
|
|
|
metadata: { nickname: state.osmConnection.userDetails.data.name },
|
|
|
|
}
|
|
|
|
await onSave(review)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.SetClass("break-normal"),
|
|
|
|
rating.map((r) => r === undefined, [commentForm.GetValue()])
|
|
|
|
)
|
2020-12-08 23:44:34 +01:00
|
|
|
|
|
|
|
const stars = []
|
|
|
|
for (let i = 1; i <= 5; i++) {
|
|
|
|
stars.push(
|
|
|
|
new VariableUiElement(
|
2023-01-21 23:58:14 +01:00
|
|
|
rating.map((score) => {
|
|
|
|
if (score === undefined) {
|
2020-12-08 23:44:34 +01:00
|
|
|
return Svg.star_outline.replace(/#000000/g, "#ccc")
|
|
|
|
}
|
2023-01-21 23:58:14 +01:00
|
|
|
return score < i * 20 ? Svg.star_outline : Svg.star
|
2020-12-08 23:44:34 +01:00
|
|
|
})
|
|
|
|
).onClick(() => {
|
2023-01-21 23:58:14 +01:00
|
|
|
rating.setData(i * 20)
|
2022-09-08 21:40:48 +02:00
|
|
|
})
|
2020-12-08 23:44:34 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
const form = new Combine([
|
2023-01-21 23:58:14 +01:00
|
|
|
new Combine([new Combine(stars).SetClass("review-form-rating"), postingAs]).SetClass(
|
|
|
|
"flex"
|
|
|
|
),
|
|
|
|
commentForm,
|
|
|
|
new Combine([isAffiliated, saveButton]),
|
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-21 23:58:14 +01:00
|
|
|
super(
|
|
|
|
new Toggle(Translations.t.reviews.saved.Clone().SetClass("thanks"), form, reviewMade),
|
|
|
|
Translations.t.reviews.plz_login,
|
|
|
|
state
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2020-12-08 23:44:34 +01:00
|
|
|
}
|
|
|
|
}
|