forum
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import React, { useState } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import Taro, { useRouter } from '@tarojs/taro'
|
||||
import { View, Text, Input, Textarea, Button, Picker } from '@tarojs/components'
|
||||
import { createTopic, uploadMedia } from '../../../api'
|
||||
import { createTopic, updateTopic, getTopicDetail, uploadMedia } from '../../../api'
|
||||
import './create.scss'
|
||||
|
||||
const CreateTopic = () => {
|
||||
const router = useRouter()
|
||||
const { id } = router.params
|
||||
|
||||
const [title, setTitle] = useState('')
|
||||
const [content, setContent] = useState('')
|
||||
const [categoryIndex, setCategoryIndex] = useState(0)
|
||||
@@ -16,6 +19,30 @@ const CreateTopic = () => {
|
||||
{ key: 'share', label: '经验分享' },
|
||||
]
|
||||
|
||||
useEffect(() => {
|
||||
if (id) {
|
||||
const fetchDetail = async () => {
|
||||
Taro.showLoading({ title: '加载中...' })
|
||||
try {
|
||||
const res = await getTopicDetail(Number(id))
|
||||
const topic = res.data
|
||||
setTitle(topic.title)
|
||||
setContent(topic.content)
|
||||
const idx = categories.findIndex(c => c.key === topic.category)
|
||||
if (idx !== -1) setCategoryIndex(idx)
|
||||
|
||||
Taro.setNavigationBarTitle({ title: '编辑话题' })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
Taro.showToast({ title: '加载失败', icon: 'none' })
|
||||
} finally {
|
||||
Taro.hideLoading()
|
||||
}
|
||||
}
|
||||
fetchDetail()
|
||||
}
|
||||
}, [id])
|
||||
|
||||
const handleCategoryChange = (e) => {
|
||||
setCategoryIndex(e.detail.value)
|
||||
}
|
||||
@@ -68,19 +95,28 @@ const CreateTopic = () => {
|
||||
|
||||
setLoading(true)
|
||||
try {
|
||||
const res = await createTopic({
|
||||
title,
|
||||
content,
|
||||
category: categories[categoryIndex].key
|
||||
})
|
||||
if (id) {
|
||||
await updateTopic(Number(id), {
|
||||
title,
|
||||
content,
|
||||
category: categories[categoryIndex].key
|
||||
})
|
||||
Taro.showToast({ title: '更新成功', icon: 'success' })
|
||||
} else {
|
||||
await createTopic({
|
||||
title,
|
||||
content,
|
||||
category: categories[categoryIndex].key
|
||||
})
|
||||
Taro.showToast({ title: '发布成功', icon: 'success' })
|
||||
}
|
||||
|
||||
Taro.showToast({ title: '发布成功', icon: 'success' })
|
||||
setTimeout(() => {
|
||||
Taro.navigateBack()
|
||||
}, 1500)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
Taro.showToast({ title: '发布失败', icon: 'none' })
|
||||
Taro.showToast({ title: id ? '更新失败' : '发布失败', icon: 'none' })
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
@@ -127,7 +163,7 @@ const CreateTopic = () => {
|
||||
onClick={handleSubmit}
|
||||
disabled={loading}
|
||||
>
|
||||
{loading ? '发布中...' : '发布话题'}
|
||||
{loading ? (id ? '更新中...' : '发布中...') : (id ? '更新话题' : '发布话题')}
|
||||
</Button>
|
||||
</View>
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import Taro, { useRouter, useShareAppMessage } from '@tarojs/taro'
|
||||
import { View, Text, Image, Video, RichText, Input, Button } from '@tarojs/components'
|
||||
import Taro, { useRouter, useShareAppMessage, useDidShow } from '@tarojs/taro'
|
||||
import { View, Text, Image, Video, RichText, Input, ScrollView } from '@tarojs/components'
|
||||
import { AtActivityIndicator, AtIcon } from 'taro-ui'
|
||||
import { getTopicDetail, createReply, uploadMedia } from '../../../api'
|
||||
import { marked } from 'marked'
|
||||
@@ -15,6 +15,7 @@ const ForumDetail = () => {
|
||||
const [replyContent, setReplyContent] = useState('')
|
||||
const [sending, setSending] = useState(false)
|
||||
const [htmlContent, setHtmlContent] = useState('')
|
||||
const [userInfo, setUserInfo] = useState<any>(null)
|
||||
|
||||
const fetchDetail = async () => {
|
||||
try {
|
||||
@@ -37,11 +38,20 @@ const ForumDetail = () => {
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const info = Taro.getStorageSync('userInfo')
|
||||
if (info) setUserInfo(info)
|
||||
|
||||
if (id) {
|
||||
fetchDetail()
|
||||
}
|
||||
}, [id])
|
||||
|
||||
useDidShow(() => {
|
||||
if (id && !loading) {
|
||||
fetchDetail()
|
||||
}
|
||||
})
|
||||
|
||||
useShareAppMessage(() => {
|
||||
return {
|
||||
title: topic?.title || '技术社区',
|
||||
@@ -53,6 +63,12 @@ const ForumDetail = () => {
|
||||
setReplyContent(e.detail.value)
|
||||
}
|
||||
|
||||
const handleEdit = () => {
|
||||
Taro.navigateTo({
|
||||
url: `/subpackages/forum/create/index?id=${id}`
|
||||
})
|
||||
}
|
||||
|
||||
const handleUpload = async () => {
|
||||
try {
|
||||
const res = await Taro.chooseMedia({
|
||||
@@ -125,6 +141,8 @@ const ForumDetail = () => {
|
||||
|
||||
return (
|
||||
<View className='forum-detail-page'>
|
||||
<ScrollView scrollY style={{height: '100vh'}}>
|
||||
<View style={{paddingBottom: 80}}>
|
||||
<View className='topic-card'>
|
||||
<View className='header'>
|
||||
{topic.is_pinned && <Text style={{color: '#ff4d4f', marginRight: 5, fontSize: 12, border: '1px solid #ff4d4f', padding: '0 4px', borderRadius: 4}}>置顶</Text>}
|
||||
@@ -140,6 +158,13 @@ const ForumDetail = () => {
|
||||
<Text>{new Date(topic.created_at).toLocaleDateString()}</Text>
|
||||
<Text>•</Text>
|
||||
<Text>{topic.view_count} 阅读</Text>
|
||||
|
||||
{userInfo && topic.author === userInfo.id && (
|
||||
<View onClick={handleEdit} style={{display: 'flex', alignItems: 'center', marginLeft: 'auto', padding: '4px 8px', background: 'rgba(255,255,255,0.1)', borderRadius: 4}}>
|
||||
<AtIcon value='edit' size='14' color='#00b96b' />
|
||||
<Text style={{fontSize: 12, color: '#00b96b', marginLeft: 4}}>编辑</Text>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
</View>
|
||||
|
||||
@@ -177,6 +202,8 @@ const ForumDetail = () => {
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
</View>
|
||||
</ScrollView>
|
||||
|
||||
<View className='reply-bar'>
|
||||
<View className='action-btn' onClick={handleUpload}>
|
||||
|
||||
Reference in New Issue
Block a user