-
Notifications
You must be signed in to change notification settings - Fork 1
/
Community.vue
166 lines (156 loc) · 4.39 KB
/
Community.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
<script setup lang="ts">
import CommunityCard from '@/components/ui/communityCard/CommunityCard.vue'
import {
PlusIcon,
XMarkIcon,
PaperAirplaneIcon
} from '@heroicons/vue/24/outline'
import { onMounted, ref } from 'vue'
import axios from 'axios'
import EmotionSelector from '@/components/ui/emotion-selector/EmotionSelector.vue'
type Post = {
postId: number
title: string
description: string
emotion: 'surprised' | 'happy' | 'scared' | 'sad' | 'angry' | 'rejecting'
}
const dialog = ref(false)
const textareaValue = ref('')
const titleValue = ref('')
const posts = ref<Post[]>([])
const pageIndex = ref(1)
const timestamp = ref('')
const end = ref(false)
const loading = ref(false)
const generateTimestamp = () => {
const currentDate = new Date()
currentDate.setHours(currentDate.getHours())
timestamp.value = currentDate.toISOString()
}
const emotion = ref<string>('')
const error = ref<string>('')
onMounted(async () => {
generateTimestamp()
await getPosts()
})
const createPost = async () => {
if (emotion.value == '') {
error.value = 'Please choose an emotion'
return
}
if (titleValue.value.length > 0 && textareaValue.value.length > 0) {
try {
await axios.post(
`${import.meta.env.VITE_BACKEND_HOST}/community/post`,
{
title: titleValue.value,
description: textareaValue.value,
emotion: emotion.value.toLowerCase()
},
{
headers: {
'Content-Type': 'application/json'
},
withCredentials: true
}
)
posts.value = []
end.value = false
titleValue.value = ''
textareaValue.value = ''
pageIndex.value = 1
error.value = ''
generateTimestamp()
await getPosts()
dialog.value = false
} catch (error) {
console.log(error)
}
}
}
const getPosts = async () => {
if (!end.value) {
try {
loading.value = true
const result = await axios.get(
`${import.meta.env.VITE_BACKEND_HOST}/community/post/paginate?page=${
pageIndex.value
}×tamp=${timestamp.value}&pageSize=10`,
{
headers: {
'Content-Type': 'application/json'
},
withCredentials: true
}
)
loading.value = false
posts.value = [...posts.value, ...result.data.posts]
if (result.data.posts.length === 0) {
end.value = true
}
} catch (error) {}
}
}
window.onscroll = () => {
let bottomOfWindow =
document.documentElement.scrollTop + window.innerHeight ===
document.documentElement.offsetHeight
if (bottomOfWindow && !loading.value) {
pageIndex.value++
getPosts()
}
}
</script>
<template>
<main>
<div class="inlay flex flex-col gap-y-6" id="scrollwindow">
<router-link
v-for="post in posts"
:to="`/posts/${post.postId}`"
class="inline-block"
>
<CommunityCard
:id="post.postId"
:title="post.title"
:content="post.description"
:variant="post.emotion"
></CommunityCard>
</router-link>
<span v-if="loading">Loading...</span>
</div>
<div class="fixed bottom-20 flex w-full flex-row-reverse px-6">
<div class="rounded-xl bg-black p-3 text-center" v-if="!dialog">
<plus-icon @click="dialog = true" class="w-8 text-white"></plus-icon>
</div>
</div>
<v-dialog v-model="dialog">
<div class="flex-column flex gap-y-4 rounded-lg bg-white p-4">
<div class="flex w-full justify-between">
<x-mark-icon class="w-6" @click="dialog = false" />
</div>
<div class="flex">
<v-text-field v-model="titleValue" label="Title" class="font-poppins">
<template v-slot:append-inner>
<EmotionSelector
@select-emotion="(e: any) => (emotion = e.title)"
></EmotionSelector>
</template>
</v-text-field>
</div>
<div>
<v-textarea
v-model="textareaValue"
label="Content"
class="font-poppins"
/>
<div class="flex w-full flex-row-reverse justify-between">
<paper-airplane-icon class="w-6" @click="createPost" />
</div>
</div>
<div v-if="error" class="w-fit rounded bg-sad-light p-2 font-poppins">
{{ error }}
</div>
</div>
</v-dialog>
</main>
</template>