web-class-frontend/app/stores/people.ts

125 lines
3.1 KiB
TypeScript

export interface PeopleInfo {
id: number;
date: string;
name: string;
province: string;
city: string;
address: string;
zip: string;
}
export interface BasicPagination {
current: number;
pageSize: number;
defaultPageSize: number;
total: number;
defaultCurrent: number;
}
export interface PeopleState {
peopleList: PeopleInfo[];
showAddPeople: boolean;
showEditPeople: boolean;
situation: string;
pagination: BasicPagination;
currentForm: PeopleInfo;
}
export const usePeopleStore = defineStore('People', {
state: (): PeopleState => ({
peopleList: [],
showAddPeople: false,
showEditPeople: false,
situation: '',
pagination: {
current: 1,
pageSize: 10,
defaultPageSize: 10,
total: 0,
defaultCurrent: 1,
},
currentForm: {
id: 0,
date: '',
name: '',
province: '',
city: '',
address: '',
zip: '',
},
}),
actions: {
async getPeopleList() {
const { $http } = useNuxtApp();
try {
const res = await $http.GET<{ records: PeopleInfo[] }>(
'/userManager/page',
{
page: '' + this.pagination!.current,
size: '' + this.pagination!.pageSize,
situation: this.situation,
},
);
this.peopleList = res.data.records;
console.log(this.peopleList);
} catch (error) {
console.error('Error fetching people list:', error);
}
},
async getTotal() {
const { $http } = useNuxtApp();
try {
const res = await $http.GET<number>('/userManager/total', {
situation: this.situation,
});
this.pagination!.total = res.data;
if (
(this.pagination!.current - 1) * this.pagination!.pageSize >
this.pagination!.total
) {
this.pagination!.current = Math.ceil(
this.pagination!.total / this.pagination!.pageSize,
);
this.getPeopleList();
}
} catch {
return;
}
},
async addPeople() {
console.log('add');
const { $http } = useNuxtApp();
try {
await $http.POST('/userManager/addUser', this.currentForm);
this.getTotal();
this.getPeopleList();
} catch (error) {
await MessagePlugin.error('Error adding people:' + error);
}
this.showEditPeople = false;
this.showAddPeople = false;
},
async updatePeople() {
const { $http } = useNuxtApp();
try {
await $http.POST('/userManager/updateUser', this.currentForm);
this.getTotal();
this.getPeopleList();
} catch (error) {
await MessagePlugin.error('Error updating people:' + error);
}
this.showEditPeople = false;
this.showAddPeople = false;
},
async deletePeople(peopleId: number) {
const { $http } = useNuxtApp();
try {
await $http.GET('/userManager/deleteUser', { id: '' + peopleId });
this.getTotal();
this.getPeopleList();
} catch (error) {
await MessagePlugin.error('Error deleting people:' + error);
}
},
},
});