| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <v-card> | 
					
						
							|  |  |  |     <v-card-title class="headline"> | 
					
						
							|  |  |  |       <v-icon large 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-08-01 19:24:47 -08:00
										 |  |  |     <v-card v-for="(comment, index) in comments" :key="comment.id" class="ma-2"> | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       <v-list-item two-line> | 
					
						
							|  |  |  |         <v-list-item-avatar color="accent" class="white--text"> | 
					
						
							|  |  |  |           <img :src="getProfileImage(comment.user.id)" /> | 
					
						
							|  |  |  |         </v-list-item-avatar> | 
					
						
							|  |  |  |         <v-list-item-content> | 
					
						
							|  |  |  |           <v-list-item-title> {{ comment.user.username }}</v-list-item-title> | 
					
						
							|  |  |  |           <v-list-item-subtitle> {{ $d(new Date(comment.dateAdded), "short") }} </v-list-item-subtitle> | 
					
						
							|  |  |  |         </v-list-item-content> | 
					
						
							|  |  |  |         <v-card-actions v-if="loggedIn"> | 
					
						
							|  |  |  |           <TheButton | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |             v-if="!editKeys[comment.id] && (user.admin || comment.user.id === user.id)" | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |             small | 
					
						
							|  |  |  |             minor | 
					
						
							|  |  |  |             delete | 
					
						
							|  |  |  |             @click="deleteComment(comment.id)" | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |           <TheButton | 
					
						
							|  |  |  |             v-if="!editKeys[comment.id] && comment.user.id === user.id" | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |             small | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |             edit | 
					
						
							|  |  |  |             @click="editComment(comment.id)" | 
					
						
							|  |  |  |           /> | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |           <TheButton v-else-if="editKeys[comment.id]" small update @click="updateComment(comment.id, index)" /> | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |         </v-card-actions> | 
					
						
							|  |  |  |       </v-list-item> | 
					
						
							|  |  |  |       <div> | 
					
						
							|  |  |  |         <v-card-text> | 
					
						
							|  |  |  |           {{ !editKeys[comment.id] ? comment.text : null }} | 
					
						
							|  |  |  |           <v-textarea v-if="editKeys[comment.id]" v-model="comment.text"> </v-textarea> | 
					
						
							|  |  |  |         </v-card-text> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </v-card> | 
					
						
							|  |  |  |     <v-card-text v-if="loggedIn"> | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       <v-textarea v-model="newComment" auto-grow row-height="1" outlined> </v-textarea> | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       <div class="d-flex"> | 
					
						
							| 
									
										
										
										
											2021-06-16 18:55:32 -08:00
										 |  |  |         <TheButton class="ml-auto" create @click="createNewComment"> {{ $t("recipe.comment-action") }} </TheButton> | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     </v-card-text> | 
					
						
							|  |  |  |   </v-card> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script> | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  | import { useApiSingleton } from "~/composables/use-api"; | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  | const NEW_COMMENT_EVENT = "new-comment"; | 
					
						
							|  |  |  | const UPDATE_COMMENT_EVENT = "update-comment"; | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   props: { | 
					
						
							|  |  |  |     comments: { | 
					
						
							|  |  |  |       type: Array, | 
					
						
							| 
									
										
										
										
											2021-08-07 16:49:55 -08:00
										 |  |  |       default: () => [], | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |     slug: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							| 
									
										
										
										
											2021-08-07 16:49:55 -08:00
										 |  |  |       required: true, | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  |   setup() { | 
					
						
							|  |  |  |     const api = useApiSingleton(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { api }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |   data() { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       newComment: "", | 
					
						
							|  |  |  |       editKeys: {}, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   computed: { | 
					
						
							|  |  |  |     user() { | 
					
						
							|  |  |  |       return this.$store.getters.getUserData; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     loggedIn() { | 
					
						
							|  |  |  |       return this.$store.getters.getIsLoggedIn; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   watch: { | 
					
						
							|  |  |  |     comments() { | 
					
						
							|  |  |  |       for (const comment of this.comments) { | 
					
						
							|  |  |  |         this.$set(this.editKeys, comment.id, false); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   methods: { | 
					
						
							|  |  |  |     resetImage() { | 
					
						
							| 
									
										
										
										
											2021-08-07 16:49:55 -08:00
										 |  |  |       this.hideImage = false; | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  |     getProfileImage() { | 
					
						
							|  |  |  |       // TODO Actually get Profile Image
 | 
					
						
							|  |  |  |       return null; | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |     editComment(id) { | 
					
						
							|  |  |  |       this.$set(this.editKeys, id, true); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async updateComment(id, index) { | 
					
						
							|  |  |  |       this.$set(this.editKeys, id, false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  |       await this.api.recipes.updateComment(this.slug, id, this.comments[index]); | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       this.$emit(UPDATE_COMMENT_EVENT); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async createNewComment() { | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  |       await this.api.recipes.createComment(this.slug, { text: this.newComment }); | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       this.$emit(NEW_COMMENT_EVENT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       this.newComment = ""; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async deleteComment(id) { | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  |       await this.api.recipes.deleteComment(this.slug, id); | 
					
						
							| 
									
										
										
										
											2021-05-29 20:50:17 -08:00
										 |  |  |       this.$emit(UPDATE_COMMENT_EVENT); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style lang="scss" scoped> | 
					
						
							|  |  |  | </style> |