New-Thanasoft/thanasoft-front/src/stores/clientGroupStore.ts

222 lines
5.5 KiB
TypeScript

import { defineStore } from "pinia";
import { ref, computed } from "vue";
import ClientGroupService, {
ClientGroup,
CreateClientGroupPayload,
UpdateClientGroupPayload,
} from "@/services/clientGroup";
export const useClientGroupStore = defineStore("clientGroup", () => {
// State
const clientGroups = ref<ClientGroup[]>([]);
const currentClientGroup = ref<ClientGroup | null>(null);
const loading = ref(false);
const error = ref<string | null>(null);
// Pagination state
const pagination = ref({
current_page: 1,
last_page: 1,
per_page: 10,
total: 0,
});
// Getters
const allClientGroups = computed(() => clientGroups.value);
const isLoading = computed(() => loading.value);
const hasError = computed(() => error.value !== null);
const getError = computed(() => error.value);
const getClientGroupById = computed(() => (id: number) =>
clientGroups.value.find((group) => group.id === id)
);
const getPagination = computed(() => pagination.value);
// Actions
const setLoading = (isLoading: boolean) => {
loading.value = isLoading;
};
const setError = (err: string | null) => {
error.value = err;
};
const setClientGroups = (newClientGroups: ClientGroup[]) => {
clientGroups.value = newClientGroups;
};
const setCurrentClientGroup = (group: ClientGroup | null) => {
currentClientGroup.value = group;
};
const setPagination = (meta: any) => {
if (meta) {
pagination.value = {
current_page: meta.current_page || 1,
last_page: meta.last_page || 1,
per_page: meta.per_page || 10,
total: meta.total || 0,
};
}
};
/**
* Fetch all client groups with optional pagination and filters
*/
const fetchClientGroups = async (params?: {
page?: number;
per_page?: number;
search?: string;
}) => {
setLoading(true);
setError(null);
try {
const response = await ClientGroupService.getAllClientGroups(params);
setClientGroups(response.data);
if (response.meta) {
setPagination(response.meta);
}
return response;
} catch (err: any) {
const errorMessage =
err.response?.data?.message || err.message || "Failed to fetch client groups";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Fetch a single client group by ID
*/
const fetchClientGroup = async (id: number) => {
setLoading(true);
setError(null);
try {
const response = await ClientGroupService.getClientGroup(id);
setCurrentClientGroup(response.data);
return response.data;
} catch (err: any) {
const errorMessage =
err.response?.data?.message || err.message || "Failed to fetch client group";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Create a new client group
*/
const createClientGroup = async (payload: CreateClientGroupPayload) => {
setLoading(true);
setError(null);
try {
const response = await ClientGroupService.createClientGroup(payload);
// Add the new group to the list
clientGroups.value.push(response.data);
setCurrentClientGroup(response.data);
return response.data;
} catch (err: any) {
const errorMessage =
err.response?.data?.message || err.message || "Failed to create client group";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Update an existing client group
*/
const updateClientGroup = async (payload: UpdateClientGroupPayload) => {
setLoading(true);
setError(null);
try {
const response = await ClientGroupService.updateClientGroup(payload);
const updatedGroup = response.data;
// Update in the groups list
const index = clientGroups.value.findIndex(
(group) => group.id === updatedGroup.id
);
if (index !== -1) {
clientGroups.value[index] = updatedGroup;
}
// Update current group if it's the one being edited
if (currentClientGroup.value && currentClientGroup.value.id === updatedGroup.id) {
setCurrentClientGroup(updatedGroup);
}
return updatedGroup;
} catch (err: any) {
const errorMessage =
err.response?.data?.message || err.message || "Failed to update client group";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
/**
* Delete a client group
*/
const deleteClientGroup = async (id: number) => {
setLoading(true);
setError(null);
try {
const response = await ClientGroupService.deleteClientGroup(id);
// Remove from the groups list
clientGroups.value = clientGroups.value.filter((group) => group.id !== id);
// Clear current group if it's the one being deleted
if (currentClientGroup.value && currentClientGroup.value.id === id) {
setCurrentClientGroup(null);
}
return response;
} catch (err: any) {
const errorMessage =
err.response?.data?.message || err.message || "Failed to delete client group";
setError(errorMessage);
throw err;
} finally {
setLoading(false);
}
};
return {
// State
clientGroups,
currentClientGroup,
loading,
error,
pagination,
// Getters
allClientGroups,
isLoading,
hasError,
getError,
getClientGroupById,
getPagination,
// Actions
fetchClientGroups,
fetchClientGroup,
createClientGroup,
updateClientGroup,
deleteClientGroup,
};
});