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;