| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02: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" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import UserDetails, { OsmConnection } from "../../Logic/Osm/OsmConnection" | 
					
						
							|  |  |  | 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", | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             textAreaRows: 5, | 
					
						
							| 
									
										
										
										
											2020-12-08 23:44:34 +01:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											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([ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +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) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01: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( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 new VariableUiElement( | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |                     rating.map((score) => { | 
					
						
							|  |  |  |                         if (score === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                             return Svg.star_outline.replace(/#000000/g, "#ccc") | 
					
						
							| 
									
										
										
										
											2020-12-08 23:44:34 +01:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											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
										 |  |  |                     }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02: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]), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +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") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             .SetStyle( | 
					
						
							|  |  |  |                 "border-radius: 1em;" + | 
					
						
							|  |  |  |                     "    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
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |