mapcomplete/UI/Reviews/ReviewForm.ts

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

104 lines
4.1 KiB
TypeScript
Raw Normal View History

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"
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"
import { SubtleButton } from "../Base/SubtleButton"
2020-12-08 23:44:34 +01:00
export default class ReviewForm extends LoginToggle {
2023-01-06 03:37:22 +01:00
constructor(
onSave: (r: Omit<Review, "sub">) => Promise<void>,
2023-01-06 03:37:22 +01:00
state: {
readonly osmConnection: OsmConnection
readonly featureSwitchUserbadge: Store<boolean>
}
) {
/* 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 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
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(
state.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 saveButton = new Toggle(
Translations.t.reviews.no_rating.SetClass("block alert"),
new SubtleButton(Svg.confirm_svg(), Translations.t.reviews.save, {
extraClasses: "border-attention-catch",
})
.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(
rating.map((score) => {
if (score === undefined) {
2020-12-08 23:44:34 +01:00
return Svg.star_outline.replace(/#000000/g, "#ccc")
}
return score < i * 20 ? Svg.star_outline : Svg.star
2020-12-08 23:44:34 +01:00
})
).onClick(() => {
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([
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
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
}
}