change users crud to items
This commit is contained in:
48
client/src/stores/ItemsStore.ts
Normal file
48
client/src/stores/ItemsStore.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
// stores are for component state management
|
||||
// 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 { Item } from "../models/Item.ts";
|
||||
import * as itemsApi from "../api/ItemsApi";
|
||||
|
||||
interface ItemState {
|
||||
items: Item[];
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
export const useItemsStore = defineStore("items", {
|
||||
|
||||
state: (): ItemState => ({
|
||||
items: [],
|
||||
loading: false
|
||||
}),
|
||||
|
||||
actions: {
|
||||
async fetchItems() {
|
||||
this.loading = true;
|
||||
const response = await itemsApi.getItems();
|
||||
this.items = response.data;
|
||||
this.loading = false;
|
||||
},
|
||||
|
||||
async addItem(item: Item) {
|
||||
const response = await itemsApi.createItem(item);
|
||||
this.items.push(response.data);
|
||||
},
|
||||
|
||||
async updateItem(id: number, item: Item) {
|
||||
await itemsApi.updateItem(id, item);
|
||||
const index = this.items.findIndex(i => i.id === id);
|
||||
this.items[index] = item;
|
||||
},
|
||||
|
||||
async removeItem(id: number) {
|
||||
await itemsApi.deleteItem(id);
|
||||
this.items = this.items.filter(i => i.id !== id);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
@@ -1,48 +0,0 @@
|
||||
|
||||
// stores are for component state management
|
||||
// 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 usersApi from "../api/UsersApi";
|
||||
|
||||
interface UserState {
|
||||
users: User[];
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
export const useUsersStore = defineStore("users", {
|
||||
|
||||
state: (): UserState => ({
|
||||
users: [],
|
||||
loading: false
|
||||
}),
|
||||
|
||||
actions: {
|
||||
async fetchUsers() {
|
||||
this.loading = true;
|
||||
const response = await usersApi.getUsers();
|
||||
this.users = response.data;
|
||||
this.loading = false;
|
||||
},
|
||||
|
||||
async addUser(user: User) {
|
||||
const response = await usersApi.createUser(user);
|
||||
this.users.push(response.data);
|
||||
},
|
||||
|
||||
async updateUser(id: number, user: User) {
|
||||
await usersApi.updateUser(id, user);
|
||||
const index = this.users.findIndex(i => i.id === id);
|
||||
this.users[index] = user;
|
||||
},
|
||||
|
||||
async removeUser(id: number) {
|
||||
await usersApi.deleteUser(id);
|
||||
this.users = this.users.filter(i => i.id !== id);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user