Initial commit: React + Django 城市手册项目
- Django 4.2 + DRF + JWT + GraphQL - React 18 + MobX + styled-components - PostgreSQL 数据库 - Docker + Docker Compose + Nginx - 完整的功能模块(用户、版块、文章、服务、交互、版主管理) - 完整的文档(需求、部署、测试)
This commit is contained in:
152
frontend/src/stores/ArticleStore.js
Normal file
152
frontend/src/stores/ArticleStore.js
Normal file
@@ -0,0 +1,152 @@
|
||||
import { makeAutoObservable } from 'mobx';
|
||||
import api from '../services/api';
|
||||
|
||||
class ArticleStore {
|
||||
articles = [];
|
||||
currentArticle = null;
|
||||
loading = false;
|
||||
error = null;
|
||||
|
||||
constructor() {
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
async fetchArticles(params = {}) {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await api.get('/api/articles/', { params });
|
||||
this.articles = response.data.results || response.data;
|
||||
} catch (error) {
|
||||
this.error = error.response?.data || 'Failed to fetch articles';
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async fetchArticle(id) {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await api.get(`/api/articles/${id}/`);
|
||||
this.currentArticle = response.data;
|
||||
} catch (error) {
|
||||
this.error = error.response?.data || 'Failed to fetch article';
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async createArticle(data) {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await api.post('/api/articles/', data);
|
||||
return { success: true, article: response.data };
|
||||
} catch (error) {
|
||||
this.error = error.response?.data || 'Failed to create article';
|
||||
return { success: false, error: this.error };
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async updateArticle(id, data) {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await api.put(`/api/articles/${id}/`, data);
|
||||
return { success: true, article: response.data };
|
||||
} catch (error) {
|
||||
this.error = error.response?.data || 'Failed to update article';
|
||||
return { success: false, error: this.error };
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteArticle(id) {
|
||||
try {
|
||||
await api.delete(`/api/articles/${id}/`);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.response?.data || 'Failed to delete article',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async submitArticle(id) {
|
||||
try {
|
||||
await api.post(`/api/articles/${id}/submit/`);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.response?.data || 'Failed to submit article',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async approveArticle(id, reason = '') {
|
||||
try {
|
||||
await api.post(`/api/articles/${id}/approve/`, { action: 'approve', reason });
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.response?.data || 'Failed to approve article',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async rejectArticle(id, reason) {
|
||||
try {
|
||||
await api.post(`/api/articles/${id}/reject/`, { action: 'reject', reason });
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.response?.data || 'Failed to reject article',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async likeArticle(id) {
|
||||
try {
|
||||
const response = await api.post(`/api/articles/${id}/like/`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async fetchArticleComments(id) {
|
||||
try {
|
||||
const response = await api.get(`/api/articles/${id}/comments/`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async fetchArticleStats(id) {
|
||||
try {
|
||||
const response = await api.get(`/api/articles/${id}/stats/`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
clearCurrentArticle() {
|
||||
this.currentArticle = null;
|
||||
}
|
||||
}
|
||||
|
||||
export default ArticleStore;
|
||||
Reference in New Issue
Block a user