edit.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <div class="wrapper">
  3. <el-row class="title" justify="center">
  4. <el-col :span="18"
  5. ><el-input v-model="title" placeholder="Blog title"
  6. /></el-col>
  7. </el-row>
  8. <div class="edit">
  9. <QuillEditor
  10. theme="snow"
  11. v-model:content="content"
  12. contentType="html"
  13. toolbar="full"
  14. :modules="combineModule"
  15. />
  16. </div>
  17. <div style="margin-top: 30px">
  18. <el-row class="title" justify="center">
  19. <el-col :span="18"
  20. ><el-input v-model="bannerImageUrl" placeholder="Banner Image Url" />
  21. </el-col>
  22. </el-row>
  23. </div>
  24. <el-row
  25. justify="center"
  26. v-if="bannerImageUrl != null && bannerImageUrl != ''"
  27. >
  28. <v-lazy-image style="height: 440px; width: 950px" :src="bannerImageUrl" />
  29. </el-row>
  30. <div class="btns flex-between">
  31. <p class="post" @click="updateBlog()">Update</p>
  32. </div>
  33. </div>
  34. </template>
  35. <script setup>
  36. import VLazyImage from "v-lazy-image";
  37. import { AXIOS } from "@/common/http-commons";
  38. import ImageUploader from "quill-image-uploader";
  39. import { useRouter } from "vue-router";
  40. import { ElNotification } from "element-plus";
  41. const title = ref("");
  42. const router = useRouter();
  43. const route = useRoute();
  44. const content = ref("");
  45. const bannerImageUrl = ref("");
  46. const imageUploadModule = {
  47. name: "imageUploader",
  48. module: ImageUploader,
  49. options: {
  50. upload: (file) => {
  51. return new Promise((resolve, reject) => {
  52. const formData = new FormData();
  53. formData.append("imageFile", file);
  54. formData.append("expectedType", "blogImage");
  55. AXIOS.post("/api/v1/image/upload", formData)
  56. .then((res) => {
  57. resolve(res.data.data);
  58. })
  59. .catch((err) => {
  60. reject("Upload failed");
  61. console.error("Error:", err);
  62. });
  63. });
  64. },
  65. },
  66. };
  67. const combineModule = [imageUploadModule];
  68. function whoami() {
  69. AXIOS.get("/api/v1/user/whoami")
  70. .then(function (response) {
  71. console.log("current user in setup: " + response.data);
  72. })
  73. .catch(function (error) {
  74. console.log("currently not logged in setup: " + error);
  75. router.push("/auth/login");
  76. });
  77. }
  78. function loadBlogDetail(blogId) {
  79. AXIOS.get("/api/v1/blog/" + blogId)
  80. .then(function (response) {
  81. console.log("blog detail:");
  82. console.log(response.data);
  83. title.value = response.data.data.title;
  84. content.value = response.data.data.content;
  85. bannerImageUrl.value = response.data.data.headImageUrl;
  86. })
  87. .catch(function (error) {
  88. console.log("currently not logged in blog edit: " + error);
  89. router.push("/auth/login");
  90. });
  91. }
  92. onMounted(() => {
  93. whoami();
  94. loadBlogDetail(route.params.blogId);
  95. });
  96. function updateBlog() {
  97. const params = {};
  98. params.title = title.value;
  99. params.content = content.value;
  100. params.headImageUrl = bannerImageUrl.value;
  101. AXIOS.put("/api/v1/blog/edit-basic/" + route.params.blogId, params)
  102. .then((res) => {
  103. ElNotification({
  104. title: "Succeed",
  105. message: "Succeed updating blogs",
  106. type: "success",
  107. });
  108. })
  109. .catch((err) => {
  110. console.error("Error:", err);
  111. ElNotification({
  112. title: "Error",
  113. message: "Failed updating blogs",
  114. type: "error",
  115. });
  116. });
  117. }
  118. </script>
  119. <style scoped lang="scss">
  120. .wrapper {
  121. margin-top: 113px;
  122. }
  123. .title {
  124. margin: 0 auto;
  125. height: 95px;
  126. text-indent: 5px;
  127. color: rgba(51, 51, 51, 1);
  128. font-size: 12px;
  129. line-height: 95px;
  130. text-align: left;
  131. }
  132. .edit {
  133. margin: 20px auto 0;
  134. width: 1199px;
  135. height: 602px;
  136. }
  137. .btns {
  138. width: 882px;
  139. margin: 38px auto 0;
  140. }
  141. .post {
  142. cursor: pointer;
  143. margin-top: 20px;
  144. width: 321px;
  145. height: 58px;
  146. color: rgba(250, 250, 250, 1);
  147. background-color: rgba(42, 130, 228, 1);
  148. border-radius: 8px;
  149. font-size: 32px;
  150. line-height: 58px;
  151. text-align: center;
  152. }
  153. .draft {
  154. cursor: pointer;
  155. margin-top: 20px;
  156. width: 321px;
  157. height: 58px;
  158. color: rgba(250, 250, 250, 1);
  159. background-color: rgba(42, 130, 228, 1);
  160. border-radius: 8px;
  161. font-size: 32px;
  162. line-height: 58px;
  163. text-align: center;
  164. }
  165. </style>