/
keep1.vue
193 lines (180 loc) · 4.88 KB
/
keep1.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<template>
<div>
<!-- MODAL CODE -->
<div
class="modal fade"
id="exampleModal"
tabindex="-1"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Edit Post</h1>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">
<form v-if="blogId">
<div class="form-group">
<label for="recipient-name" class="col-form-label"
>Title:</label
>
<input type="text" v-model="blogTitle" class="form-control" />
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Body:</label>
<textarea class="form-control" v-model="blogContent"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-primary"
@click="processEdit(blogId, blogTitle, blogContent)"
>
Save
</button>
</div>
</div>
</div>
</div>
<!-- MODAL CODE ENDS -->
<div class="container customPadding">
<div class="customPadding">
<div class="card card shadow-lg p-3 mb-5 bg-white rounded mr-5 ml-5">
<div v-if="blogId" class="container">
<!-- FROM HERE -->
<div class="card-head" style="padding: 4rem">
<h1 style="font-size: 6rem">
{{ blogTitle }}
</h1>
<button
type="button"
class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
Edit
</button>
<button
type="button"
class="ms-2 btn btn-danger"
@click="processDelete(blogId)"
>
Delete
</button>
</div>
<!-- TO HERE -->
<!-- <div class="card-head" style="padding: 4rem">
<h1 style="font-size: 6rem">
{{ result.blog.data.attributes.title }}
</h1>
</div> -->
<hr />
<p style="line-height: 26pt">
{{ blogContent }}
</p>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import gql from "graphql-tag";
import { ref, watch } from "vue";
import { useQuery, useMutation } from "@vue/apollo-composable";
import { useRoute } from "vue-router";
export default {
setup() {
const route = useRoute();
const blogId = ref(route.params.id);
const blogTitle = ref("");
const blogContent = ref("");
// FETCH POST
const { result } = useQuery(gql`
query {
blog(id: ${blogId.value}) {
data {
id
attributes {
date
title
body
}
}
}
}
`);
// Update blogTitle and blogContent when result changes
watch(result, (newResult) => {
if (newResult && newResult.blog) {
const { title, body } = newResult.blog.data.attributes;
blogTitle.value = title;
blogContent.value = body;
}
});
// DELETE POST
const { mutate: deletePost } = useMutation(gql`
mutation deleteBlog($id: ID!) {
deleteBlog(id: $id) {
data {
id
}
}
}
`);
const processDelete = (blogId) => {
let check = confirm(
`Are you sure you want to delete this post ${blogId}`
);
if (check) {
let deleted = deletePost({ id: blogId });
if (deleted) {
alert("Post deleted");
}
}
};
// EDIT POST
const { mutate: editPost } = useMutation(gql`
mutation updateBlog($id: ID!, $title: String!, $body: String!) {
updateBlog(id: $id, data: { title: $title, body: $body }) {
data {
id
}
}
}
`);
const processEdit = (blogId, title, body) => {
let check = confirm(`Confirm to edit post? ${blogId}`);
if (check) {
let edited = editPost({ id: Number(blogId), title: title, body: body });
if (edited) {
alert("Post updated. Refresh!");
}
}
};
return {
blogId,
blogTitle,
blogContent,
processDelete,
processEdit,
};
},
};
</script>
<style>
.customPadding {
margin-top: 12rem !important;
margin-bottom: 12rem !important;
/* padding: 4rem; */
}
</style>