monolito_djanco_poonto/src/stores/spaces.js
2025-02-25 13:26:08 -03:00

171 lines
4.4 KiB
JavaScript

import { defineStore } from 'pinia';
import api from '../services/api'; // A instância personalizada do Axios
import router from '../routes/router';
import { useAuthStore } from './auth'; // Importa o AuthStore
export const useSpaceStore = defineStore('spaces', {
state: () => ({
spaces: [],
total: 0,
page: 1,
perPage: 10,
error: null,
loading: false,
}),
getters: {
currentPage(state) {
return state.page;
},
totalPages(state) {
return Math.ceil(state.total / state.perPage);
},
},
actions: {
/**
* Busca os ambientes (spaces) registrados na API
*/
async fetchSpaces(params) {
const authStore = useAuthStore();
const serviceInstanceId = authStore.getInstance;
if (!serviceInstanceId) {
this.error = 'Nenhuma instância de serviço selecionada';
return;
}
this.loading = true;
this.error = null;
try {
const response = await api.get('/spaces/', {
params: {
service_instance_id: serviceInstanceId,
page: params.page,
per_page: params.per_page,
search: params.search
}
});
this.$patch({
spaces: response.data.spaces,
total: response.data.total,
page: params.page,
perPage: params.per_page
});
return response.data.spaces;
} catch (error) {
const status = error?.response?.status;
if (status === 401) {
console.warn('Não autorizado. Redirecionando para login...');
router.push('/login');
}
this.error = error?.response?.data?.message || error.message || 'Erro ao buscar espaços';
console.error('Erro ao buscar espaços', error);
throw error; // Propaga o erro
} finally {
this.loading = false;
}
},
/**
* Busca um espaço específico pelo ID
*/
async fetchSpaceById(space_id) {
const url = `/spaces/${space_id}`;
this.loading = true;
this.error = null;
try {
const response = await api.get(url);
return response.data;
} catch (error) {
this.error = error?.response?.data?.message || error.message || 'Erro ao buscar espaço';
console.error('Erro ao buscar espaço', error);
throw error;
} finally {
this.loading = false;
}
},
/**
* Cria um novo espaço
*/
async createSpace(space) {
const url = '/spaces/';
this.loading = true;
this.error = null;
try {
const response = await api.post(url, {
description: space.description,
name: space.name,
service_instance_id: space.service_instance_id,
});
this.spaces.push(response.data);
return response.data;
} catch (error) {
this.error = error?.response?.data?.message || error.message || 'Erro ao criar espaço';
console.error('Erro ao criar espaço', error);
throw error;
} finally {
this.loading = false;
}
},
/**
* Atualiza os dados de um espaço
*/
async updateSpace(id, space) {
const url = `/spaces/${id}`;
this.loading = true;
this.error = null;
try {
const response = await api.put(url, {
description: space.description,
name: space.name,
service_instance_id: space.service_instance_id,
});
const index = this.spaces.findIndex((s) => s.id === id);
if (index !== -1) {
this.spaces[index] = response.data;
}
return response.data;
} catch (error) {
this.error = error?.response?.data?.message || error.message || 'Erro ao atualizar espaço';
console.error('Erro ao atualizar espaço', error);
throw error;
} finally {
this.loading = false;
}
},
/**
* Exclui um espaço
*/
async deleteSpace(id) {
const url = `/spaces/${id}`;
this.loading = true;
this.error = null;
try {
await api.delete(url); // Exclui o espaço da API
this.spaces = this.spaces.filter((space) => space.id !== id); // Remove da lista local
} catch (error) {
this.error = error?.response?.data?.message || error.message || 'Erro ao excluir espaço';
console.error('Erro ao excluir espaço', error);
throw error;
} finally {
this.loading = false;
}
},
},
});