Feat: Add comment write

This commit is contained in:
jhynsoo 2024-05-23 15:21:39 +09:00
parent a5019812ce
commit 4a65a80b40
3 changed files with 26 additions and 16 deletions

View File

@ -1,18 +1,20 @@
<script setup> <script setup>
import { getArticles } from '@/api/article'; import { getArticles } from '@/api/article';
import { ref, reactive, watch } from 'vue'; import { ref, watch, computed } from 'vue';
import { RouterLink } from 'vue-router'; import { RouterLink } from 'vue-router';
import FilledButton from '../common/FilledButton.vue'; import FilledButton from '../common/FilledButton.vue';
import { useMemberStore } from '@/stores/memberStore';
const articles = ref([]); const articles = ref([]);
const params = reactive({ const param = computed(() => {
pageNo: 1, return {
key: 'all', pageNo: currentPage.value,
word: '', };
}); });
const currentPage = ref(1);
const hasNextPage = ref(true); const hasNextPage = ref(true);
const lastElement = ref(null); const lastElement = ref(null);
const memberStore = useMemberStore();
watch(lastElement, (el) => { watch(lastElement, (el) => {
if (!el) { if (!el) {
@ -21,7 +23,8 @@ watch(lastElement, (el) => {
const observer = new IntersectionObserver( const observer = new IntersectionObserver(
([entry]) => { ([entry]) => {
if (entry.isIntersecting && hasNextPage.value) { if (entry.isIntersecting && hasNextPage.value) {
params.pageNo += 1; // params.pageNo += 1;
currentPage.value += 1;
searchList(); searchList();
} }
}, },
@ -32,8 +35,8 @@ watch(lastElement, (el) => {
}); });
function searchList() { function searchList() {
getArticles(params).then(({ data }) => { getArticles(param.value).then(({ data }) => {
if ((data?.articles?.length ?? 0) === 0) { if (data?.page?.total === currentPage.value || (data?.articles?.length ?? 0) === 0) {
hasNextPage.value = false; hasNextPage.value = false;
return; return;
} }
@ -46,7 +49,7 @@ searchList();
<template> <template>
<header> <header>
<h1>게시판</h1> <h1>게시판</h1>
<RouterLink :to="{ name: 'article-create' }"> <RouterLink v-if="memberStore.isLogin" :to="{ name: 'article-create' }">
<FilledButton primary class="write">글쓰기</FilledButton> <FilledButton primary class="write">글쓰기</FilledButton>
</RouterLink> </RouterLink>
</header> </header>

View File

@ -2,9 +2,16 @@
import { useMemberStore } from '@/stores/memberStore'; import { useMemberStore } from '@/stores/memberStore';
import CommentForm from './CommentForm.vue'; import CommentForm from './CommentForm.vue';
import CommentList from './CommentList.vue'; import CommentList from './CommentList.vue';
import { toRefs } from 'vue';
const { articleId, comments } = defineProps({ articleId: Number, comments: Array }); const props = defineProps({ articleId: Number, comments: Array });
const { articleId } = props;
const { comments } = toRefs(props);
const memberStore = useMemberStore(); const memberStore = useMemberStore();
function updateList(comment) {
comments.value.push(comment);
}
</script> </script>
<template> <template>
@ -12,7 +19,7 @@ const memberStore = useMemberStore();
<h2> <h2>
댓글 <span>{{ comments.length }}</span> 댓글 <span>{{ comments.length }}</span>
</h2> </h2>
<CommentForm :article-id="articleId" v-if="memberStore.isLogin" /> <CommentForm @updateList="updateList" :article-id="articleId" v-if="memberStore.isLogin" />
<CommentList :comments="comments" /> <CommentList :comments="comments" />
</div> </div>
</template> </template>

View File

@ -4,7 +4,8 @@ import FilledButton from '../common/FilledButton.vue';
import TextButton from '../common/TextButton.vue'; import TextButton from '../common/TextButton.vue';
import { addComment } from '@/api/comment'; import { addComment } from '@/api/comment';
const { id } = defineProps({ id: Number }); const emit = defineEmits(['updateList']);
const { articleId } = defineProps({ articleId: Number });
const isActive = ref(false); const isActive = ref(false);
const textDiv = ref(null); const textDiv = ref(null);
const text = ref(''); const text = ref('');
@ -19,9 +20,8 @@ function handleCancel() {
} }
function handleSubmit() { function handleSubmit() {
addComment({ id, text: text.value }).then(({ data }) => { addComment({ articleId, text: text.value }).then(({ data }) => {
console.log(data); emit('updateList', data);
// TODO:
}); });
text.value = ''; text.value = '';
isActive.value = false; isActive.value = false;