Skip to content

Commit fbf67e3

Browse files
committed
Move comment related pieces from MovieItem to a new MovieComment component.
1 parent 09e5300 commit fbf67e3

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

src/components/MovieComment.vue

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<template>
2+
<div class="col-md-8 form-group mb-0">
3+
<template v-if="isEditing">
4+
<textarea
5+
class="form-control"
6+
rows="3"
7+
placeholder="Did you like this movie?"
8+
v-model="comment"
9+
></textarea>
10+
<div :class="[commentWordCount > 0 ? 'text-success' : 'text-danger']">
11+
{{ commentWordCount }} word{{ commentWordCount !== 1 ? 's' : '' }}
12+
</div>
13+
</template>
14+
<div v-else v-html="formattedComment" />
15+
<button class="btn btn-primary mt-2" @click="isEditing = !isEditing">
16+
{{ isEditing ? 'Save comment' : 'Edit comment' }}
17+
</button>
18+
</div>
19+
</template>
20+
21+
<script>
22+
export default {
23+
name: 'movie-comment',
24+
data() {
25+
return {
26+
comment: '',
27+
isEditing: false,
28+
};
29+
},
30+
computed: {
31+
commentWordCount() {
32+
return this.comment.split(/\s+/).filter(word => word !== '').length;
33+
},
34+
formattedComment() {
35+
if (!this.commentWordCount) {
36+
return 'No comment yet.';
37+
}
38+
return `Your comment: <b>${this.comment}</b>`;
39+
},
40+
},
41+
};
42+
</script>

src/components/MovieItem.vue

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,26 @@
1616
</div>
1717
</div>
1818
<div class="row mt-4">
19-
<div class="col-md-8 form-group mb-0">
20-
<template v-if="isEditing">
21-
<textarea
22-
class="form-control"
23-
rows="3"
24-
placeholder="Did you like this movie?"
25-
v-model="item.comment"
26-
></textarea>
27-
<div
28-
:class="[commentWordCount > 0 ? 'text-success' : 'text-danger']"
29-
>
30-
{{ commentWordCount }} word{{
31-
commentWordCount !== 1 ? 's' : ''
32-
}}
33-
</div>
34-
</template>
35-
<div v-else v-html="formattedComment" />
36-
<button
37-
class="btn btn-primary mt-2"
38-
@click="isEditing = !isEditing"
39-
>
40-
{{ isEditing ? 'Save comment' : 'Edit comment' }}
41-
</button>
42-
</div>
19+
<movie-comment />
4320
</div>
4421
</div>
4522
</div>
4623
</div>
4724
</template>
4825

4926
<script>
27+
import MovieComment from './MovieComment.vue';
28+
5029
export default {
5130
name: 'movie-item',
52-
data() {
53-
return {
54-
isEditing: false,
55-
};
31+
components: {
32+
MovieComment,
5633
},
5734
props: {
5835
item: {
5936
type: Object,
6037
required: true,
6138
},
6239
},
63-
computed: {
64-
commentWordCount() {
65-
return this.item.comment.split(/\s+/).filter(word => word !== '').length;
66-
},
67-
formattedComment() {
68-
if (!this.commentWordCount) {
69-
return 'No comment yet.';
70-
}
71-
return `Your comment: <b>${this.item.comment}</b>`;
72-
},
73-
},
7440
};
7541
</script>

0 commit comments

Comments
 (0)