| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div> | 
					
						
							|  |  |  |     <v-card-title class="headline pb-3"> | 
					
						
							|  |  |  |       <v-icon class="mr-2"> | 
					
						
							|  |  |  |         {{ $globals.icons.commentTextMultipleOutline }} | 
					
						
							|  |  |  |       </v-icon> | 
					
						
							|  |  |  |       {{ $t("recipe.comments") }} | 
					
						
							|  |  |  |     </v-card-title> | 
					
						
							|  |  |  |     <v-divider class="mx-2"></v-divider> | 
					
						
							|  |  |  |     <div v-if="user.id" class="d-flex flex-column"> | 
					
						
							|  |  |  |       <div class="d-flex mt-3" style="gap: 10px"> | 
					
						
							| 
									
										
										
										
											2024-10-11 19:36:26 -05:00
										 |  |  |         <UserAvatar :tooltip="false" size="40" :user-id="user.id" /> | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         <v-textarea | 
					
						
							|  |  |  |           v-model="comment" | 
					
						
							|  |  |  |           hide-details="" | 
					
						
							|  |  |  |           dense | 
					
						
							|  |  |  |           single-line | 
					
						
							|  |  |  |           outlined | 
					
						
							|  |  |  |           auto-grow | 
					
						
							|  |  |  |           rows="2" | 
					
						
							|  |  |  |           :placeholder="$t('recipe.join-the-conversation')" | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |         </v-textarea> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <div class="ml-auto mt-1"> | 
					
						
							|  |  |  |         <BaseButton small :disabled="!comment" @click="submitComment"> | 
					
						
							|  |  |  |           <template #icon>{{ $globals.icons.check }}</template> | 
					
						
							|  |  |  |           {{ $t("general.submit") }} | 
					
						
							|  |  |  |         </BaseButton> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							| 
									
										
										
										
											2024-06-21 19:44:22 -05:00
										 |  |  |     <div v-for="comment in recipe.comments" :key="comment.id" class="d-flex my-2" style="gap: 10px"> | 
					
						
							| 
									
										
										
										
											2024-10-11 19:36:26 -05:00
										 |  |  |       <UserAvatar :tooltip="false" size="40" :user-id="comment.userId" /> | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       <v-card outlined class="flex-grow-1"> | 
					
						
							|  |  |  |         <v-card-text class="pa-3 pb-0"> | 
					
						
							|  |  |  |           <p class="">{{ comment.user.username }} • {{ $d(Date.parse(comment.createdAt), "medium") }}</p> | 
					
						
							| 
									
										
										
										
											2023-11-24 10:41:58 +01:00
										 |  |  |           <SafeMarkdown :source="comment.text" /> | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |         </v-card-text> | 
					
						
							|  |  |  |         <v-card-actions class="justify-end mt-0 pt-0"> | 
					
						
							|  |  |  |           <v-btn | 
					
						
							|  |  |  |             v-if="user.id == comment.user.id || user.admin" | 
					
						
							|  |  |  |             color="error" | 
					
						
							|  |  |  |             text | 
					
						
							|  |  |  |             x-small | 
					
						
							|  |  |  |             @click="deleteComment(comment.id)" | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             {{ $t("general.delete") }} | 
					
						
							|  |  |  |           </v-btn> | 
					
						
							|  |  |  |         </v-card-actions> | 
					
						
							|  |  |  |       </v-card> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2024-06-21 19:44:22 -05:00
										 |  |  | import { defineComponent, toRefs, reactive } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2024-06-21 19:44:22 -05:00
										 |  |  | import { Recipe } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | import UserAvatar from "~/components/Domain/User/UserAvatar.vue"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { NoUndefinedField } from "~/lib/api/types/non-generated"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | import { usePageUser } from "~/composables/recipe-page/shared-state"; | 
					
						
							| 
									
										
										
										
											2023-11-24 10:41:58 +01:00
										 |  |  | import SafeMarkdown from "~/components/global/SafeMarkdown.vue"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   components: { | 
					
						
							|  |  |  |     UserAvatar, | 
					
						
							| 
									
										
										
										
											2023-11-24 10:41:58 +01:00
										 |  |  |     SafeMarkdown | 
					
						
							|  |  |  | }, | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |   props: { | 
					
						
							|  |  |  |     recipe: { | 
					
						
							|  |  |  |       type: Object as () => NoUndefinedField<Recipe>, | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   setup(props) { | 
					
						
							|  |  |  |     const api = useUserApi(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { user } = usePageUser(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       comment: "", | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function submitComment() { | 
					
						
							|  |  |  |       const { data } = await api.recipes.comments.createOne({ | 
					
						
							|  |  |  |         recipeId: props.recipe.id, | 
					
						
							|  |  |  |         text: state.comment, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							| 
									
										
										
										
											2024-06-21 19:44:22 -05:00
										 |  |  |         // @ts-ignore username is always populated here
 | 
					
						
							|  |  |  |         props.recipe.comments.push(data); | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       state.comment = ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function deleteComment(id: string) { | 
					
						
							|  |  |  |       const { response } = await api.recipes.comments.deleteOne(id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (response?.status === 200) { | 
					
						
							| 
									
										
										
										
											2024-06-21 19:44:22 -05:00
										 |  |  |         props.recipe.comments = props.recipe.comments.filter((comment) => comment.id !== id); | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-21 19:44:22 -05:00
										 |  |  |     return { api, ...toRefs(state), submitComment, deleteComment, user }; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |