| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  | <template> | 
					
						
							| 
									
										
										
										
											2021-07-09 14:33:23 -08:00
										 |  |  |   <v-tooltip bottom nudge-right="50" :color="buttonStyle ? 'info' : 'secondary'"> | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     <template #activator="{ on, attrs }"> | 
					
						
							| 
									
										
										
										
											2021-06-12 14:59:14 +08:00
										 |  |  |       <v-btn | 
					
						
							|  |  |  |         v-if="isFavorite || showAlways" | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |         small | 
					
						
							| 
									
										
										
										
											2021-07-09 14:33:23 -08:00
										 |  |  |         :color="buttonStyle ? 'info' : 'secondary'" | 
					
						
							| 
									
										
										
										
											2021-06-12 14:59:14 +08:00
										 |  |  |         :icon="!buttonStyle" | 
					
						
							|  |  |  |         :fab="buttonStyle" | 
					
						
							|  |  |  |         v-bind="attrs" | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |         @click.prevent="toggleFavorite" | 
					
						
							| 
									
										
										
										
											2021-06-12 14:59:14 +08:00
										 |  |  |         v-on="on" | 
					
						
							|  |  |  |       > | 
					
						
							| 
									
										
										
										
											2021-07-09 14:33:23 -08:00
										 |  |  |         <v-icon :small="!buttonStyle" :color="buttonStyle ? 'white' : 'secondary'"> | 
					
						
							| 
									
										
										
										
											2021-06-16 18:55:32 -08:00
										 |  |  |           {{ isFavorite ? $globals.icons.heart : $globals.icons.heartOutline }} | 
					
						
							| 
									
										
										
										
											2021-06-12 14:59:14 +08:00
										 |  |  |         </v-icon> | 
					
						
							|  |  |  |       </v-btn> | 
					
						
							|  |  |  |     </template> | 
					
						
							|  |  |  |     <span>{{ isFavorite ? $t("recipe.remove-from-favorites") : $t("recipe.add-to-favorites") }}</span> | 
					
						
							|  |  |  |   </v-tooltip> | 
					
						
							| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { computed, defineComponent, useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2022-02-23 15:04:45 -09:00
										 |  |  | import { UserOut } from "~/types/api-types/user"; | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  |   props: { | 
					
						
							|  |  |  |     slug: { | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       type: String, | 
					
						
							| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  |       default: "", | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     showAlways: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     buttonStyle: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |   setup(props) { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |     const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     const { $auth } = useContext(); | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     // TODO Setup the correct type for $auth.user
 | 
					
						
							|  |  |  |     // See https://github.com/nuxt-community/auth-module/issues/1097
 | 
					
						
							|  |  |  |     const user = computed(() => $auth.user as unknown as UserOut); | 
					
						
							|  |  |  |     const isFavorite = computed(() => user.value?.favoriteRecipes?.includes(props.slug)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function toggleFavorite() { | 
					
						
							|  |  |  |       if (!isFavorite.value) { | 
					
						
							|  |  |  |         await api.users.addFavorite(user.value?.id, props.slug); | 
					
						
							| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |         await api.users.removeFavorite(user.value?.id, props.slug); | 
					
						
							| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       $auth.fetchUser(); | 
					
						
							| 
									
										
										
										
											2022-02-23 15:04:45 -09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { isFavorite, toggleFavorite }; | 
					
						
							| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-05-29 15:54:18 -08:00
										 |  |  | </script> |