add crud store/api service
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 7s

This commit is contained in:
2026-03-07 00:53:08 -06:00
parent 533571859f
commit 31225b51b2
9 changed files with 412 additions and 4 deletions

View File

@@ -3,3 +3,46 @@
// Pinia (?) i kinda dont get it because in angular you just hook a component to a service and that's it,
// though I guess the service handled the state management
// sighh
import { defineStore } from "pinia";
import type { User } from "../models/User.ts";
import * as api from "../api/UsersApi";
interface UserState {
users: User[];
loading: boolean;
}
export const useUsersStore = defineStore("users", {
state: (): UserState => ({
users: [],
loading: false
}),
actions: {
async fetchItems() {
this.loading = true;
const response = await api.getUsers();
this.users = response.data;
this.loading = false;
},
async addUser(user: User) {
const response = await api.createUser(user);
this.users.push(response.data);
},
async updateUser(id: number, user: User) {
await api.updateUser(id, user);
const index = this.users.findIndex(i => i.id === id);
this.users[index] = user;
},
async removeUser(id: number) {
await api.deleteUser(id);
this.users = this.users.filter(i => i.id !== id);
}
}
});