| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div @click.prevent> | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |     <!-- User Rating --> | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     <v-hover v-slot="{ hover }"> | 
					
						
							|  |  |  |       <v-rating | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |         v-if="isOwnGroup && (userRating || hover || !ratingsLoaded)" | 
					
						
							|  |  |  |         :value="userRating" | 
					
						
							|  |  |  |         color="secondary" | 
					
						
							|  |  |  |         background-color="secondary lighten-3" | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |         length="5" | 
					
						
							|  |  |  |         :dense="small ? true : undefined" | 
					
						
							|  |  |  |         :size="small ? 15 : undefined" | 
					
						
							|  |  |  |         hover | 
					
						
							|  |  |  |         clearable | 
					
						
							|  |  |  |         @input="updateRating" | 
					
						
							|  |  |  |         @click="updateRating" | 
					
						
							|  |  |  |       /> | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |       <!-- Group Rating --> | 
					
						
							|  |  |  |       <v-rating | 
					
						
							|  |  |  |         v-else | 
					
						
							|  |  |  |         :value="groupRating" | 
					
						
							|  |  |  |         :half-increments="true" | 
					
						
							|  |  |  |         :readonly="true" | 
					
						
							|  |  |  |         color="grey darken-1" | 
					
						
							|  |  |  |         background-color="secondary lighten-3" | 
					
						
							|  |  |  |         length="5" | 
					
						
							|  |  |  |         :dense="small ? true : undefined" | 
					
						
							|  |  |  |         :size="small ? 15 : undefined" | 
					
						
							|  |  |  |         hover | 
					
						
							|  |  |  |       /> | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     </v-hover> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  | import { computed, defineComponent, ref, watch } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  | import { useLoggedInState } from "~/composables/use-logged-in-state"; | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  | import { useUserSelfRatings } from "~/composables/use-users"; | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   props: { | 
					
						
							|  |  |  |     emitOnly: { | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       type: Boolean, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     recipeId: { | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       type: String, | 
					
						
							|  |  |  |       default: "", | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     slug: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: "", | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     value: { | 
					
						
							|  |  |  |       type: Number, | 
					
						
							|  |  |  |       default: 0, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     small: { | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       type: Boolean, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |   setup(props, context) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |     const { isOwnGroup } = useLoggedInState(); | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     const { userRatings, setRating, ready: ratingsLoaded } = useUserSelfRatings(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |     const userRating = computed(() => { | 
					
						
							|  |  |  |       return userRatings.value.find((r) => r.recipeId === props.recipeId)?.rating; | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     // if a user unsets their rating, we don't want to fall back to the group rating since it's out of sync
 | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |     const hideGroupRating = ref(!!userRating.value); | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     watch( | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |       () => userRating.value, | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |       () => { | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |         if (userRating.value) { | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |           hideGroupRating.value = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |     const groupRating = computed(() => { | 
					
						
							|  |  |  |       return hideGroupRating.value ? 0 : props.value; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 14:10:44 -05:00
										 |  |  |     function updateRating(val: number | null) { | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |       if (!isOwnGroup.value) { | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2023-09-15 14:10:44 -05:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 12:54:47 -08:00
										 |  |  |       if (!props.emitOnly) { | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |         setRating(props.slug, val || 0, null); | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-10-22 12:54:47 -08:00
										 |  |  |       context.emit("input", val); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |     return { | 
					
						
							|  |  |  |       isOwnGroup, | 
					
						
							| 
									
										
										
										
											2024-04-19 10:52:41 -05:00
										 |  |  |       ratingsLoaded, | 
					
						
							|  |  |  |       groupRating, | 
					
						
							|  |  |  |       userRating, | 
					
						
							| 
									
										
										
										
											2024-04-11 21:28:43 -05:00
										 |  |  |       updateRating, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style lang="scss" scoped></style> |