完善投稿功能
This commit is contained in:
@@ -89,3 +89,76 @@ export const saveDraft = (content: string): void => {
|
||||
export const getDraft = (): string | null => {
|
||||
return localStorage.getItem('draft');
|
||||
};
|
||||
|
||||
export const fetch_id_token = async (): Promise<string> => {
|
||||
try {
|
||||
const response = await fetch('/api/get_id_token');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const json = await response.json();
|
||||
if (json.code === 1000) {
|
||||
return json.data;
|
||||
} else {
|
||||
throw new Error(json.data || 'Failed to fetch identity token');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch identity token:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const get_id_token = async (): Promise<string> => {
|
||||
let token = localStorage.getItem('identity_token');
|
||||
if (!token) {
|
||||
token = await fetch_id_token();
|
||||
localStorage.setItem('identity_token', token);
|
||||
}
|
||||
return token;
|
||||
};
|
||||
|
||||
export interface CreatePostResponse {
|
||||
code: number;
|
||||
data: any;
|
||||
}
|
||||
|
||||
export const createPost = async (content: string): Promise<CreatePostResponse> => {
|
||||
try {
|
||||
const identity = await get_id_token();
|
||||
|
||||
// 解析标签:#开头,后跟非空白字符
|
||||
const hashtopic: string[] = [];
|
||||
const regex = /#\S+/g;
|
||||
const matches = content.match(regex);
|
||||
if (matches) {
|
||||
matches.forEach(tag => {
|
||||
const cleanTag = tag.substring(1);
|
||||
// 去重添加
|
||||
if (!hashtopic.includes(cleanTag)) {
|
||||
hashtopic.push(cleanTag);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const response = await fetch('/api/post', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
content,
|
||||
hashtopic,
|
||||
identity
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Failed to create post:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -23,8 +23,9 @@ import {
|
||||
Send24Regular,
|
||||
Save24Regular
|
||||
} from '@fluentui/react-icons';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useLayout } from '../context/LayoutContext';
|
||||
import { saveDraft, getDraft } from '../api';
|
||||
import { saveDraft, getDraft, createPost } from '../api';
|
||||
|
||||
const useStyles = makeStyles({
|
||||
container: {
|
||||
@@ -118,6 +119,7 @@ const remarkTagPlugin = () => {
|
||||
|
||||
const CreatePost: React.FC = () => {
|
||||
const styles = useStyles();
|
||||
const navigate = useNavigate();
|
||||
const { isDarkMode, toasterId } = useLayout();
|
||||
const { dispatchToast } = useToastController(toasterId);
|
||||
const [value, setValue] = useState<string | undefined>("");
|
||||
@@ -135,6 +137,60 @@ const CreatePost: React.FC = () => {
|
||||
const activeElapsedRef = useRef<number>(0);
|
||||
const hasStartedAutoSaveRef = useRef(false);
|
||||
|
||||
const handlePostSubmit = async () => {
|
||||
if (!value || !value.trim()) {
|
||||
dispatchToast(
|
||||
<Toast>
|
||||
<ToastTitle>内容不能为空</ToastTitle>
|
||||
</Toast>,
|
||||
{ intent: 'error' }
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await createPost(value);
|
||||
if (response.code === 1001 || response.code === 1002) {
|
||||
// 清除草稿
|
||||
saveDraft('');
|
||||
setValue('');
|
||||
|
||||
// 停止自动保存计时器
|
||||
if (autoSaveIntervalRef.current !== null) {
|
||||
window.clearInterval(autoSaveIntervalRef.current);
|
||||
autoSaveIntervalRef.current = null;
|
||||
}
|
||||
activeElapsedRef.current = 0;
|
||||
hasStartedAutoSaveRef.current = false;
|
||||
|
||||
dispatchToast(
|
||||
<Toast>
|
||||
<ToastTitle>{response.code === 1002 ? "投稿成功,等待审核" : "投稿成功!"}</ToastTitle>
|
||||
</Toast>,
|
||||
{ intent: 'success' }
|
||||
);
|
||||
|
||||
navigate('/');
|
||||
} else if (response.code === 2005) {
|
||||
dispatchToast(
|
||||
<Toast>
|
||||
<ToastTitle>投稿中包含违禁词!</ToastTitle>
|
||||
</Toast>,
|
||||
{ intent: 'error' }
|
||||
);
|
||||
} else {
|
||||
throw new Error(response.data || 'Unknown error');
|
||||
}
|
||||
} catch (error) {
|
||||
dispatchToast(
|
||||
<Toast>
|
||||
<ToastTitle>投稿失败,请稍后再试。</ToastTitle>
|
||||
</Toast>,
|
||||
{ intent: 'error' }
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (
|
||||
@@ -270,6 +326,7 @@ const CreatePost: React.FC = () => {
|
||||
<Button
|
||||
appearance="primary"
|
||||
icon={<Send24Regular />}
|
||||
onClick={handlePostSubmit}
|
||||
>
|
||||
提交
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user