| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import { FixedUiElement } from "../Base/FixedUiElement" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							|  |  |  | import BaseUIElement from "../BaseUIElement" | 
					
						
							|  |  |  | import Img from "../Base/Img" | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  | import { Review } from "mangrove-reviews-typescript" | 
					
						
							|  |  |  | import { Store } from "../../Logic/UIEventSource" | 
					
						
							| 
									
										
										
										
											2020-12-11 16:29:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  | export default class SingleReview extends Combine { | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |     constructor(review: Review & { madeByLoggedInUser: Store<boolean> }) { | 
					
						
							|  |  |  |         const date = new Date(review.iat * 1000) | 
					
						
							|  |  |  |         const reviewAuthor = | 
					
						
							|  |  |  |             review.metadata.nickname ?? | 
					
						
							|  |  |  |             (review.metadata.given_name ?? "") + (review.metadata.family_name ?? "") | 
					
						
							| 
									
										
										
										
											2023-05-09 00:06:51 +02:00
										 |  |  |         const authorElement =   new FixedUiElement(reviewAuthor).SetClass("font-bold") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super([ | 
					
						
							|  |  |  |             new Combine([SingleReview.GenStars(review.rating)]), | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |             new FixedUiElement(review.opinion), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new Combine([ | 
					
						
							| 
									
										
										
										
											2020-12-11 16:29:51 +01:00
										 |  |  |                 new Combine([ | 
					
						
							| 
									
										
										
										
											2023-05-09 00:06:51 +02:00
										 |  |  |                     authorElement, | 
					
						
							|  |  |  |                      review.metadata.is_affiliated | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |                         ? Translations.t.reviews.affiliated_reviewer_warning | 
					
						
							|  |  |  |                         : "", | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 ]).SetStyle("margin-right: 0.5em"), | 
					
						
							|  |  |  |                 new FixedUiElement( | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |                     `${date.getFullYear()}-${Utils.TwoDigits( | 
					
						
							|  |  |  |                         date.getMonth() + 1 | 
					
						
							|  |  |  |                     )}-${Utils.TwoDigits(date.getDate())} ${Utils.TwoDigits( | 
					
						
							|  |  |  |                         date.getHours() | 
					
						
							|  |  |  |                     )}:${Utils.TwoDigits(date.getMinutes())}`
 | 
					
						
							| 
									
										
										
										
											2023-05-09 00:06:51 +02:00
										 |  |  |                 ).SetClass("subtle"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             ]).SetClass("flex mb-4 justify-end"), | 
					
						
							|  |  |  |         ]) | 
					
						
							|  |  |  |         this.SetClass("block p-2 m-4 rounded-xl subtle-background review-element") | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |         review.madeByLoggedInUser.addCallbackAndRun((madeByUser) => { | 
					
						
							|  |  |  |             if (madeByUser) { | 
					
						
							| 
									
										
										
										
											2023-05-09 00:06:51 +02:00
										 |  |  |                 authorElement.SetClass("thanks") | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2023-05-09 00:06:51 +02:00
										 |  |  |                 authorElement.RemoveClass("thanks") | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2020-12-11 16:29:51 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public static GenStars(rating: number): BaseUIElement { | 
					
						
							|  |  |  |         if (rating === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return Translations.t.reviews.no_rating | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (rating < 10) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             rating = 10 | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const scoreTen = Math.round(rating / 10) | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  |         return new Combine([ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             ...Utils.TimesT(scoreTen / 2, (_) => | 
					
						
							|  |  |  |                 new Img("./assets/svg/star.svg").SetClass("'h-8 w-8 md:h-12") | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             scoreTen % 2 == 1 | 
					
						
							|  |  |  |                 ? new Img("./assets/svg/star_half.svg").SetClass("h-8 w-8 md:h-12") | 
					
						
							|  |  |  |                 : undefined, | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  |         ]).SetClass("flex w-max") | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |