-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
165 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,40 @@ | ||
<script setup lang="ts"> | ||
import { useStore } from '~/store' | ||
import type { MessageItem } from '~/types/message' | ||
const { messagesStore } = useStore() | ||
const { messages, newMessages, updateMessages } = messagesStore | ||
const props = defineProps<{ | ||
needRefresh: boolean | ||
}>() | ||
onUnmounted(() => { | ||
updateMessages() | ||
const emits = defineEmits<{ | ||
(e: 'refreshed'): void | ||
}>() | ||
const { needRefresh } = toRefs(props) | ||
const messages = ref<MessageItem[]>([]) | ||
async function fetchMessages() { | ||
const data = await $fetch('/api/messages/list') | ||
if (data.success) { | ||
messages.value = data.payload ?? [] | ||
} | ||
} | ||
watch(needRefresh, async (newV) => { | ||
if (newV) { | ||
await fetchMessages() | ||
emits('refreshed') | ||
} | ||
}) | ||
onMounted(async () => { | ||
await fetchMessages() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="relative mb-5 flex w-full flex-col gap-5"> | ||
<ThoughtsMessagesItem v-for="(message, index) in messages" :key="message.id" :index="index" :message="message" /> | ||
<ThoughtsMessagesItem v-for="message in newMessages" :key="message.id" :message="message" /> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
components/hana/Message/useMessage.ts → composables/useMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
<script setup lang="ts"> | ||
const needRefresh = ref(false) | ||
function handlePublished() { | ||
needRefresh.value = true | ||
} | ||
function handleRefreshed() { | ||
needRefresh.value = false | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="w-full"> | ||
<ThoughtsMessages /> | ||
<ThoughtsInput /> | ||
<ThoughtsMessages :need-refresh="needRefresh" @refreshed="handleRefreshed" /> | ||
<ThoughtsInput @published="handlePublished" /> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,36 @@ | ||
import dayjs from 'dayjs' | ||
import prisma from '~/lib/prisma' | ||
import { formattedEventHandler } from '~/server/utils/formattedEventHandler' | ||
|
||
export default formattedEventHandler(async () => { | ||
return { payload: [] } | ||
const messages = await prisma.message.findMany({ | ||
select: { | ||
id: true, | ||
parent: { | ||
select: { | ||
id: true, | ||
content: true, | ||
}, | ||
}, | ||
author: { | ||
select: { | ||
id: true, | ||
username: true, | ||
site: true, | ||
avatar: true, | ||
}, | ||
}, | ||
content: true, | ||
publishedAt: true, | ||
editedAt: true, | ||
}, | ||
}) | ||
|
||
const result = messages.map(message => ({ | ||
...message, | ||
publishedAt: dayjs(message.publishedAt).format('YYYY-MM-DD HH:mm:ss'), | ||
editedAt: dayjs(message.editedAt).format('YYYY-MM-DD HH:mm:ss'), | ||
})) | ||
|
||
return { payload: result } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,31 @@ | ||
import { z } from 'zod' | ||
import prisma from '~/lib/prisma' | ||
import { useZodVerify } from '~/server/composables/useZodVerify' | ||
import { formattedEventHandler } from '~/server/utils/formattedEventHandler' | ||
|
||
export default formattedEventHandler(async () => { | ||
return { payload: [] } | ||
const verifySchema = z.object({ | ||
content: z.string().min(1, { message: 'Content must not be empty' }).max(1024, { message: 'Content must not exceed 1024 characters' }), | ||
}) | ||
|
||
export default formattedEventHandler(async (event) => { | ||
const id = event.context.jwtPayload.id | ||
const body = await readBody(event) | ||
const { success, errorList, result } = useZodVerify(verifySchema, body) | ||
if (!success) { | ||
return { | ||
statusCode: 400, | ||
statusMessage: 'Invalid request body', | ||
payload: errorList, | ||
success: false, | ||
} | ||
} | ||
|
||
const { content } = result | ||
|
||
await prisma.message.create({ | ||
data: { | ||
content, | ||
authorId: id, | ||
}, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
export interface MessageItem { | ||
id: number | ||
content: string | ||
author: { | ||
id: number | ||
publishedAt: string | ||
editedAt: string | ||
parent: { | ||
content: string | ||
id: number | ||
name: string | ||
site: string | ||
avatar: string | ||
} | ||
publishedAt: string // YYYY-MM-DD HH:mm | ||
editedAt: string // YYYY-MM-DD HH:mm | ||
isMe: boolean | ||
replyTo?: { | ||
} | null | ||
author: { | ||
id: number | ||
content: string | ||
} | ||
username: string | ||
site: string | null | ||
avatar: string | null | ||
} | null | ||
} |