| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  | <template> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |   <div> | 
					
						
							|  |  |  |     <v-card-title class="headline pb-3"> | 
					
						
							|  |  |  |       <v-icon class="mr-2"> | 
					
						
							| 
									
										
										
										
											2021-06-16 18:55:32 -08:00
										 |  |  |         {{ $globals.icons.commentTextMultipleOutline }} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       </v-icon> | 
					
						
							| 
									
										
										
										
											2021-06-16 18:55:32 -08:00
										 |  |  |       {{ $t("recipe.comments") }} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |     </v-card-title> | 
					
						
							|  |  |  |     <v-divider class="mx-2"></v-divider> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |     <div class="d-flex flex-column"> | 
					
						
							|  |  |  |       <div class="d-flex mt-3" style="gap: 10px"> | 
					
						
							| 
									
										
										
										
											2021-12-18 19:04:36 -09:00
										 |  |  |         <UserAvatar size="40" :user-id="$auth.user.id" /> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |         <v-textarea | 
					
						
							|  |  |  |           v-model="comment" | 
					
						
							|  |  |  |           hide-details="" | 
					
						
							|  |  |  |           dense | 
					
						
							|  |  |  |           single-line | 
					
						
							|  |  |  |           outlined | 
					
						
							|  |  |  |           auto-grow | 
					
						
							|  |  |  |           rows="2" | 
					
						
							|  |  |  |           placeholder="Join the Conversation" | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |         </v-textarea> | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |       <div class="ml-auto mt-1"> | 
					
						
							|  |  |  |         <BaseButton small :disabled="!comment" @click="submitComment"> | 
					
						
							|  |  |  |           <template #icon>{{ $globals.icons.check }}</template> | 
					
						
							|  |  |  |           {{ $t("general.submit") }} | 
					
						
							|  |  |  |         </BaseButton> | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |     </div> | 
					
						
							|  |  |  |     <div v-for="comment in comments" :key="comment.id" class="d-flex my-2" style="gap: 10px"> | 
					
						
							| 
									
										
										
										
											2021-12-18 19:04:36 -09:00
										 |  |  |       <UserAvatar size="40" :user-id="comment.userId" /> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09: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> | 
					
						
							|  |  |  |           {{ comment.text }} | 
					
						
							|  |  |  |         </v-card-text> | 
					
						
							|  |  |  |         <v-card-actions class="justify-end mt-0 pt-0"> | 
					
						
							|  |  |  |           <v-btn | 
					
						
							|  |  |  |             v-if="$auth.user.id == comment.user.id || $auth.user.admin" | 
					
						
							|  |  |  |             color="error" | 
					
						
							|  |  |  |             text | 
					
						
							|  |  |  |             x-small | 
					
						
							|  |  |  |             @click="deleteComment(comment.id)" | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             Delete | 
					
						
							|  |  |  |           </v-btn> | 
					
						
							|  |  |  |         </v-card-actions> | 
					
						
							|  |  |  |       </v-card> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2021-11-26 22:37:06 -09:00
										 |  |  | import { defineComponent, ref, toRefs, onMounted, reactive } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  | import { RecipeCommentOut } from "~/types/api-types/recipe"; | 
					
						
							| 
									
										
										
										
											2021-12-18 19:04:36 -09:00
										 |  |  | import UserAvatar from "~/components/Domain/User/UserAvatar.vue"; | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2021-12-18 19:04:36 -09:00
										 |  |  |   components: { | 
					
						
							|  |  |  |     UserAvatar, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |   props: { | 
					
						
							|  |  |  |     slug: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							| 
									
										
										
										
											2021-08-07 16:49:55 -08:00
										 |  |  |       required: true, | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |     recipeId: { | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  |       type: String, | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |   setup(props) { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |     const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |     const comments = ref<RecipeCommentOut[]>([]); | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       comment: "", | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onMounted(async () => { | 
					
						
							|  |  |  |       const { data } = await api.recipes.comments.byRecipe(props.slug); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							|  |  |  |         comments.value = data; | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |     async function submitComment() { | 
					
						
							|  |  |  |       const { data } = await api.recipes.comments.createOne({ | 
					
						
							|  |  |  |         recipeId: props.recipeId, | 
					
						
							|  |  |  |         text: state.comment, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |       if (data) { | 
					
						
							|  |  |  |         comments.value.push(data); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       state.comment = ""; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |     async function deleteComment(id: string) { | 
					
						
							|  |  |  |       const { response } = await api.recipes.comments.deleteOne(id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (response?.status === 200) { | 
					
						
							|  |  |  |         comments.value = comments.value.filter((comment) => comment.id !== id); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { api, comments, ...toRefs(state), submitComment, deleteComment }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | </script> |