diff --git a/client/src/api/UsersApi.ts b/client/src/api/UsersApi.ts new file mode 100644 index 0000000..484315d --- /dev/null +++ b/client/src/api/UsersApi.ts @@ -0,0 +1,11 @@ + +import api from "./axios.ts" +import type { User } from "../models/User.ts"; + +const API_URL: string = "/users"; + +export const getUsers = () => api.get(`${API_URL}`); + +export const getUser = (id: number) => api.get(`${API_URL}/${id}`); + +export const deleteUser = (id: number) => api.delete(`${API_URL}/${id}`); diff --git a/client/src/components/UsersTable.vue b/client/src/components/UsersTable.vue new file mode 100644 index 0000000..e69de29 diff --git a/client/src/pages/UsersForm.vue b/client/src/pages/UsersForm.vue new file mode 100644 index 0000000..7350e46 --- /dev/null +++ b/client/src/pages/UsersForm.vue @@ -0,0 +1,56 @@ + + + + + \ No newline at end of file diff --git a/client/src/pages/UsersList.vue b/client/src/pages/UsersList.vue new file mode 100644 index 0000000..e69de29 diff --git a/client/src/router/index.ts b/client/src/router/index.ts index 183b38a..1575f9d 100644 --- a/client/src/router/index.ts +++ b/client/src/router/index.ts @@ -17,7 +17,8 @@ const routes = [ { path: "/register", component: RegisterForm }, { path: "/items", component: ItemsList, meta: { requiresAuth: true } }, { path: "/item/new", component: ItemForm, meta: { requiresAuth: true } }, - { path: "/item/:id", component: ItemForm, meta: { requiresAuth: true } } + { path: "/item/:id", component: ItemForm, meta: { requiresAuth: true } }, + { path: "/users", component: ItemsList, meta: { requiresAuth: true } } ]; // I really like this const router = createRouter({ @@ -36,5 +37,6 @@ router.beforeEach((to, from, next) => { } }); +// if the api responds unauthorized (401) then it also will auto-redirect to the login page export default router; diff --git a/client/src/stores/UsersStore.ts b/client/src/stores/UsersStore.ts new file mode 100644 index 0000000..064f686 --- /dev/null +++ b/client/src/stores/UsersStore.ts @@ -0,0 +1,32 @@ + +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 removeUser(id: number) { + await usersApi.deleteUser(id); + this.users = this.users.filter(i => i.id !== id); + } + } + +});