Skip to content

Commit

Permalink
Remove bot service and refactor snapshot views to use centralized sna…
Browse files Browse the repository at this point in the history
…pshot service functions.
  • Loading branch information
swuecho committed Sep 8, 2024
1 parent 83d9a5d commit 9054864
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
23 changes: 18 additions & 5 deletions web/src/service/bot.ts → web/src/service/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { displayLocaleDate, formatYearMonth } from '@/utils/date'


export function post_url(uuid: string): string {
return `#/bot/${uuid}`
}



export function generateAPIHelper(uuid: string, apiToken: string, origin: string) {
Expand All @@ -25,6 +23,16 @@ export function getChatbotPosts(posts: Snapshot.Snapshot[]) {
}))
}

export function getSnapshotPosts(posts: Snapshot.Snapshot[]) {
return posts
.filter((post: Snapshot.Snapshot) => post.typ === 'snapshot')
.map((post: Snapshot.Snapshot): Snapshot.PostLink => ({
uuid: post.uuid,
date: displayLocaleDate(post.createdAt),
title: post.title,
}))
}

export function postsByYearMonthTransform(posts: Snapshot.PostLink[]) {
const init: Record<string, Snapshot.PostLink[]> = {}
return posts.reduce((acc, post) => {
Expand All @@ -37,7 +45,12 @@ export function postsByYearMonthTransform(posts: Snapshot.PostLink[]) {
}, init)
}

export function getPostLinks(snapshots: Snapshot.Snapshot[]): Record<string, Snapshot.PostLink[]> {
const chatbotPosts = getChatbotPosts(snapshots)
export function getSnapshotPostLinks(snapshots: Snapshot.Snapshot[]): Record<string, Snapshot.PostLink[]> {
const snapshotPosts = getSnapshotPosts(snapshots)
return postsByYearMonthTransform(snapshotPosts)
}

export function getBotPostLinks(bots: Snapshot.Snapshot[]): Record<string, Snapshot.PostLink[]> {
const chatbotPosts = getChatbotPosts(bots)
return postsByYearMonthTransform(chatbotPosts)
}
31 changes: 22 additions & 9 deletions web/src/views/bot/all.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { NModal, useDialog, useMessage } from 'naive-ui'
import Search from '../snapshot/components/Search.vue'
import { fetchSnapshotAll, fetchSnapshotDelete } from '@/api'
import { HoverButton, SvgIcon } from '@/components/common'
import { generateAPIHelper, post_url, getPostLinks } from '@/service/bot'
import { generateAPIHelper, getBotPostLinks } from '@/service/snapshot'
import request from '@/utils/request/axios'
import { t } from '@/locales'
const dialog = useDialog()
const nui_msg = useMessage()
const search_visible = ref(false)
const apiToken = ref('')
const postsByYearMonth = ref<Record<string, Snapshot.PostLink[]>>({})
onMounted(async () => {
const rawPosts: Snapshot.Snapshot[] = await fetchSnapshotAll()
postsByYearMonth.value = getPostLinks(rawPosts)
await refreshSnapshot()
try {
const response = await request.get('/token_10years')
if (response.status === 200) {
Expand All @@ -30,6 +30,12 @@ onMounted(async () => {
}
})
async function refreshSnapshot() {
const bots: Snapshot.Snapshot[] = await fetchSnapshotAll()
postsByYearMonth.value = getBotPostLinks(bots)
}
function handleDelete(post: Snapshot.PostLink) {
const dialogBox = dialog.warning({
title: t('chat_snapshot.deletePost'),
Expand All @@ -40,8 +46,7 @@ function handleDelete(post: Snapshot.PostLink) {
try {
dialogBox.loading = true
await fetchSnapshotDelete(post.uuid)
const snapshots: Snapshot.Snapshot[] = await fetchSnapshotAll()
postsByYearMonth.value = getPostLinks(snapshots)
await refreshSnapshot()
dialogBox.loading = false
nui_msg.success(t('chat_snapshot.deleteSuccess'))
Promise.resolve()
Expand All @@ -56,9 +61,6 @@ function handleDelete(post: Snapshot.PostLink) {
})
}
function genAPIHelper(post: Snapshot.PostLink) {
return generateAPIHelper(post.uuid, apiToken.value, window.location.origin)
}
function handleShowCode(post: Snapshot.PostLink) {
const dialogBox = dialog.info({
Expand All @@ -73,6 +75,17 @@ function handleShowCode(post: Snapshot.PostLink) {
},
})
}
function post_url(uuid: string): string {
return `#/bot/${uuid}`
}
function genAPIHelper(post: Snapshot.PostLink) {
return generateAPIHelper(post.uuid, apiToken.value, window.location.origin)
}
</script>

<template>
Expand Down Expand Up @@ -111,7 +124,7 @@ function handleShowCode(post: Snapshot.PostLink) {
<div class="flex">
<time :datetime="post.date" class="mb-1 text-sm font-medium text-gray-600">{{
post.date
}}</time>
}}</time>
<div class="ml-2 text-sm" @click="handleDelete(post)">
<SvgIcon icon="ic:baseline-delete-forever" />
</div>
Expand Down
45 changes: 10 additions & 35 deletions web/src/views/snapshot/all.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,29 @@ import { onMounted, ref } from 'vue'
import { NModal, useDialog, useMessage } from 'naive-ui'
import Search from './components/Search.vue'
import { fetchSnapshotAll, fetchSnapshotDelete } from '@/api'
import { displayLocaleDate, formatYearMonth } from '@/utils/date'
import { HoverButton, SvgIcon } from '@/components/common'
import { getSnapshotPostLinks } from '@/service/snapshot'
import { t } from '@/locales'
const dialog = useDialog()
const nui_msg = useMessage()
const search_visible = ref(false)
interface PostLink {
uuid: string
date: string
title: string
}
function post_url(uuid: string): string {
return `#/snapshot/${uuid}`
}
const postsByYearMonth = ref<Record<string, PostLink[]>>({})
function postsByYearMonthTransform(posts: PostLink[]) {
const init: Record<string, PostLink[]> = {}
return posts.reduce((acc, post) => {
const yearMonth = formatYearMonth(new Date(post.date))
if (!acc[yearMonth])
acc[yearMonth] = []
acc[yearMonth].push(post)
return acc
}, init)
}
async function getPostLinks() {
const rawPosts = await fetchSnapshotAll()
const rawPostsFormated = rawPosts.filter( (post: any) => post.typ === 'snapshot' ).map((post: any) => {
return {
uuid: post.uuid,
date: displayLocaleDate(post.createdAt),
title: post.title,
}
})
return postsByYearMonthTransform(rawPostsFormated)
}
const postsByYearMonth = ref<Record<string, Snapshot.PostLink[]>>({})
onMounted(async () => {
postsByYearMonth.value = await getPostLinks()
await refreshSnapshot()
})
function handleDelete(post: PostLink) {
async function refreshSnapshot() {
const snapshots: Snapshot.Snapshot[] = await fetchSnapshotAll()
postsByYearMonth.value = getSnapshotPostLinks(snapshots)
}
function handleDelete(post: Snapshot.PostLink) {
const dialogBox = dialog.warning({
title: t('chat_snapshot.deletePost'),
content: post.title,
Expand All @@ -60,7 +35,7 @@ function handleDelete(post: PostLink) {
try {
dialogBox.loading = true
await fetchSnapshotDelete(post.uuid)
postsByYearMonth.value = await getPostLinks()
await refreshSnapshot()
dialogBox.loading = false
nui_msg.success(t('chat_snapshot.deleteSuccess'))
Promise.resolve()
Expand Down

0 comments on commit 9054864

Please sign in to comment.