Files
ai-service-front/src/stores/ideas.js
2026-02-24 17:32:48 +03:00

266 lines
7.7 KiB
JavaScript

import { defineStore } from 'pinia';
import { ref } from 'vue';
import { ideaService } from '../services/ideaService';
import { inspirationService } from '../services/inspirationService';
export const useIdeaStore = defineStore('ideas', () => {
const ideas = ref([]);
const inspirations = ref([]);
const currentIdea = ref(null);
const currentInspiration = ref(null); // New state
const loading = ref(false);
const error = ref(null);
const totalIdeas = ref(0);
async function fetchIdeas(limit = 10, offset = 0) {
loading.value = true;
error.value = null;
try {
const response = await ideaService.getIdeas(limit, offset);
if (response.data.ideas) {
ideas.value = response.data.ideas;
totalIdeas.value = response.data.total_count;
} else {
ideas.value = response.data;
}
} catch (err) {
console.error('Error fetching ideas:', err);
error.value = err.response?.data?.detail || 'Failed to fetch ideas';
} finally {
loading.value = false;
}
}
async function createIdea(data) {
loading.value = true;
error.value = null;
try {
const response = await ideaService.createIdea(data);
await fetchIdeas(); // Refresh list
return response.data;
} catch (err) {
console.error('Error creating idea:', err);
error.value = err.response?.data?.detail || 'Failed to create idea';
return null;
} finally {
loading.value = false;
}
}
async function fetchIdea(id) {
loading.value = true;
error.value = null;
currentIdea.value = null;
try {
const response = await ideaService.getIdea(id);
currentIdea.value = response.data;
return response.data;
} catch (err) {
console.error('Error fetching idea:', err);
error.value = err.response?.data?.detail || 'Failed to fetch idea';
return null;
} finally {
loading.value = false;
}
}
// New action to fetch a single inspiration
async function fetchInspiration(id) {
loading.value = true;
error.value = null;
currentInspiration.value = null;
try {
const response = await inspirationService.getInspiration(id);
currentInspiration.value = response.data;
return response.data;
} catch (err) {
console.error('Error fetching inspiration:', err);
error.value = err.response?.data?.detail || 'Failed to fetch inspiration';
return null;
} finally {
loading.value = false;
}
}
async function updateIdea(id, data) {
loading.value = true;
error.value = null;
try {
await ideaService.updateIdea(id, data);
if (currentIdea.value && currentIdea.value.id === id) {
await fetchIdea(id);
}
await fetchIdeas();
return true;
} catch (err) {
console.error('Error updating idea:', err);
error.value = err.response?.data?.detail || 'Failed to update idea';
return false;
} finally {
loading.value = false;
}
}
async function deleteIdea(id) {
loading.value = true;
error.value = null;
try {
await ideaService.deleteIdea(id);
await fetchIdeas(); // Refresh list
return true;
} catch (err) {
console.error('Error deleting idea:', err);
error.value = err.response?.data?.detail || 'Failed to delete idea';
return false;
} finally {
loading.value = false;
}
}
async function addGenerationToIdea(ideaId, generationId) {
loading.value = true;
error.value = null;
try {
await ideaService.addGenerationToIdea(ideaId, generationId);
if (currentIdea.value && currentIdea.value.id === ideaId) {
await fetchIdea(ideaId);
}
return true;
} catch (err) {
console.error('Error adding generation to idea:', err);
error.value = err.response?.data?.detail || 'Failed to add generation to idea';
return false;
} finally {
loading.value = false;
}
}
async function removeGenerationFromIdea(ideaId, generationId) {
loading.value = true;
error.value = null;
try {
await ideaService.removeGenerationFromIdea(ideaId, generationId);
if (currentIdea.value && currentIdea.value.id === ideaId) {
await fetchIdea(ideaId);
}
return true;
} catch (err) {
console.error('Error removing generation from idea:', err);
error.value = err.response?.data?.detail || 'Failed to remove generation from idea';
return false;
} finally {
loading.value = false;
}
}
// Assuming getIdeaGenerations is separate from getIdea
async function fetchIdeaGenerations(ideaId, limit = 100, offset = 0, onlyLiked = false) {
try {
const response = await ideaService.getIdeaGenerations(ideaId, limit, offset, onlyLiked);
return response;
} catch (err) {
console.error('Error fetching idea generations:', err);
return { data: [] };
}
}
// --- Inspirations ---
async function fetchInspirations(limit = 20, offset = 0) {
loading.value = true;
try {
const response = await inspirationService.getInspirations(limit, offset);
inspirations.value = response.data.inspirations || response.data;
} catch (err) {
console.error('Error fetching inspirations:', err);
error.value = err.response?.data?.detail || 'Failed to fetch inspirations';
} finally {
loading.value = false;
}
}
async function createInspiration(data) {
loading.value = true;
try {
await inspirationService.createInspiration(data);
await fetchInspirations();
return true;
} catch (err) {
console.error('Error creating inspiration:', err);
error.value = err.response?.data?.detail || 'Failed to create inspiration';
return false;
} finally {
loading.value = false;
}
}
async function updateInspiration(id, data) {
loading.value = true;
try {
await inspirationService.updateInspiration(id, data);
await fetchInspirations();
return true;
} catch (err) {
console.error('Error updating inspiration:', err);
error.value = err.response?.data?.detail || 'Failed to update inspiration';
return false;
} finally {
loading.value = false;
}
}
async function deleteInspiration(id) {
loading.value = true;
try {
await inspirationService.deleteInspiration(id);
await fetchInspirations();
return true;
} catch (err) {
console.error('Error deleting inspiration:', err);
error.value = err.response?.data?.detail || 'Failed to delete inspiration';
return false;
} finally {
loading.value = false;
}
}
async function completeInspiration(id) {
loading.value = true;
try {
await inspirationService.completeInspiration(id);
await fetchInspirations();
return true;
} catch (err) {
console.error('Error completing inspiration:', err);
error.value = err.response?.data?.detail || 'Failed to complete inspiration';
return false;
} finally {
loading.value = false;
}
}
return {
ideas,
inspirations,
currentIdea,
currentInspiration,
loading,
error,
totalIdeas,
fetchIdeas,
createIdea,
fetchIdea,
updateIdea,
deleteIdea,
addGenerationToIdea,
removeGenerationFromIdea,
fetchIdeaGenerations,
fetchInspirations,
createInspiration,
updateInspiration,
deleteInspiration,
completeInspiration,
fetchInspiration
};
});