alai-server/frontend/src/views/OsCrud.vue

274 lines
10 KiB
Vue
Raw Normal View History

2023-02-01 23:51:02 -03:00
<script setup>
import { FilterMatchMode } from 'primevue/api';
import { ref, onMounted, onBeforeMount } from 'vue';
import { useToast } from 'primevue/usetoast';
2023-02-06 22:28:38 -03:00
import axios from 'axios';
import auth from '../utils/Auth';
2023-02-01 23:51:02 -03:00
const toast = useToast();
2023-02-14 18:23:22 -03:00
const users = ref(null);
const userDialog = ref(false);
const userDialogChange = ref(false);
const deleteUserDialog = ref(false);
const deleteUsersDialog = ref(false);
2023-02-06 22:28:38 -03:00
const os = ref({});
2023-02-14 18:23:22 -03:00
const selectedUsers = ref(null);
2023-02-01 23:51:02 -03:00
const dt = ref(null);
const filters = ref({});
const submitted = ref(false);
2023-02-06 22:28:38 -03:00
const checkAuth = () => {
auth.checkToken(true);
};
2023-03-01 23:34:29 -03:00
const url = new URL(window.location.href);
2023-03-01 23:55:56 -03:00
const api = (url.port == "5173") ? "http://localhost:3001" : "/api/v1";
2023-02-06 22:28:38 -03:00
2023-02-14 18:23:22 -03:00
async function showTable() {
2023-02-06 22:28:38 -03:00
try {
2023-03-01 23:34:29 -03:00
const response = await axios.get(api + `/os`);
2023-02-06 22:28:38 -03:00
console.log(response.data);
2023-02-14 18:23:22 -03:00
users.value = response.data;
2023-02-06 22:28:38 -03:00
}
catch (error) {
console.error(error);
}
}
2023-02-01 23:51:02 -03:00
2023-02-06 22:28:38 -03:00
async function onCreateClick() {
2023-02-09 23:38:39 -03:00
submitted.value = true;
if (os.value.name === "") {
throw new Error("name empty");
}
2023-02-06 22:28:38 -03:00
var newOS =
{
name: os.value.name
};
try {
2023-03-01 23:34:29 -03:00
const response = await axios.post(api + `/os`, newOS, auth.getTokenHeader());
2023-02-06 22:28:38 -03:00
if (response.status === 204) {
toast.add({ severity: 'success', summary: 'Successful', detail: 'OS Created', life: 3000 });
2023-02-14 18:23:22 -03:00
userDialog.value = false;
showTable();
2023-02-06 22:28:38 -03:00
}
else {
console.error(response);
}
}
catch (error) {
console.error(error);
}
}
2023-02-01 23:51:02 -03:00
onBeforeMount(() => {
initFilters();
});
onMounted(() => {
2023-02-06 22:28:38 -03:00
checkAuth();
2023-02-14 18:23:22 -03:00
showTable();
2023-02-01 23:51:02 -03:00
});
const openNew = () => {
2023-02-06 22:28:38 -03:00
os.value = {};
2023-02-01 23:51:02 -03:00
submitted.value = false;
2023-02-14 18:23:22 -03:00
userDialog.value = true;
2023-02-01 23:51:02 -03:00
};
const hideDialog = () => {
2023-02-14 18:23:22 -03:00
userDialog.value = false;
2023-02-01 23:51:02 -03:00
submitted.value = false;
};
2023-02-07 23:32:16 -03:00
const hideDialogChange = () => {
2023-02-14 18:23:22 -03:00
userDialogChange.value = false;
2023-02-07 23:32:16 -03:00
submitted.value = false;
};
2023-02-14 18:23:22 -03:00
const editUser = (editUser) => {
os.value = { ...editUser };
2023-02-07 23:32:16 -03:00
console.log(os.value.ID);
2023-02-14 18:23:22 -03:00
userDialogChange.value = true;
2023-02-01 23:51:02 -03:00
};
2023-02-07 23:32:16 -03:00
async function onChangeOS() {
var changeOS =
{
name: os.value.name
};
2023-02-10 22:11:42 -03:00
console.log(os.value.ID);
2023-02-07 23:32:16 -03:00
try {
2023-03-01 23:34:29 -03:00
const response = await axios.patch(api + `/os/` + os.value.ID, changeOS, auth.getTokenHeader());
2023-02-07 23:32:16 -03:00
if (response.status === 204) {
toast.add({ severity: 'success', summary: 'Successful', detail: 'OS Modified', life: 3000 });
2023-02-14 18:23:22 -03:00
userDialogChange.value = false;
showTable();
2023-02-07 23:32:16 -03:00
}
else {
console.error(response);
}
}
catch (error) {
console.error(error);
}
}
2023-02-01 23:51:02 -03:00
2023-02-14 18:23:22 -03:00
const confirmDeleteUser = (editUser) => {
os.value = editUser;
deleteUserDialog.value = true;
2023-02-01 23:51:02 -03:00
};
2023-02-14 18:23:22 -03:00
async function deleteUser() {
2023-02-06 22:28:38 -03:00
try {
2023-03-01 23:34:29 -03:00
const response = await axios.delete(api + `/os/` + os.value.ID, auth.getTokenHeader());
2023-02-06 22:28:38 -03:00
if (response.status !== 204) {
console.error(response);
2023-02-01 23:51:02 -03:00
}
2023-02-14 18:23:22 -03:00
showTable();
2023-02-01 23:51:02 -03:00
}
2023-02-06 22:28:38 -03:00
catch (error) {
console.error(error);
2023-02-01 23:51:02 -03:00
}
2023-02-14 18:23:22 -03:00
users.value = users.value.filter((val) => val.ID !== os.value.ID);
deleteUserDialog.value = false;
2023-02-06 22:28:38 -03:00
os.value = {};
toast.add({ severity: 'success', summary: 'Successful', detail: 'OS Deleted', life: 3000 });
2023-02-01 23:51:02 -03:00
};
const confirmDeleteSelected = () => {
2023-02-14 18:23:22 -03:00
deleteUsersDialog.value = true;
2023-02-01 23:51:02 -03:00
};
2023-02-14 18:23:22 -03:00
const deleteSelectedUsers = () => {
selectedUsers.value.forEach(element => {
2023-02-06 22:28:38 -03:00
try {
2023-03-01 23:34:29 -03:00
const response = axios.delete(api + `/os/` + element.ID, auth.getTokenHeader());
2023-02-06 22:28:38 -03:00
if (response.status !== 204) {
console.error(response);
}
}
catch (error) {
console.error(error);
}
});
2023-02-14 18:23:22 -03:00
console.log(selectedUsers.value);
2023-02-06 22:28:38 -03:00
2023-02-14 18:23:22 -03:00
users.value = users.value.filter((val) => !selectedUsers.value.includes(val));
deleteUsersDialog.value = false;
selectedUsers.value = null;
toast.add({ severity: 'success', summary: 'Successful', detail: 'Users Deleted', life: 3000 });
2023-02-01 23:51:02 -03:00
};
const initFilters = () => {
filters.value = {
global: { value: null, matchMode: FilterMatchMode.CONTAINS }
};
};
</script>
<template>
<div class="grid">
<div class="col-12">
<div class="card">
<Toast />
<Toolbar class="mb-4">
<template v-slot:start>
<div class="my-2">
<Button label="New" icon="pi pi-plus" class="p-button-success mr-2" @click="openNew" />
2023-03-01 23:34:29 -03:00
<Button label="Delete" icon="pi pi-trash" class="p-button-danger" @click="confirmDeleteSelected"
:disabled="!selectedUsers || !selectedUsers.length" />
2023-02-01 23:51:02 -03:00
</div>
</template>
</Toolbar>
2023-02-14 18:23:22 -03:00
<DataTable ref="dt" :value="users" v-model:selection="selectedUsers" dataKey="ID" :paginator="true"
:rows="10" :filters="filters"
2023-02-01 23:51:02 -03:00
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
:rowsPerPageOptions="[5, 10, 25]"
2023-03-01 23:34:29 -03:00
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} users" responsiveLayout="scroll">
2023-02-01 23:51:02 -03:00
<template #header>
<div class="flex flex-column md:flex-row md:justify-content-between md:align-items-center">
2023-02-14 18:23:22 -03:00
<h5 class="m-0">Manage Users</h5>
2023-02-01 23:51:02 -03:00
<span class="block mt-2 md:mt-0 p-input-icon-left">
<i class="pi pi-search" />
<InputText v-model="filters['global'].value" placeholder="Search..." />
</span>
</div>
</template>
<Column selectionMode="multiple" headerStyle="width: 3rem"></Column>
2023-02-06 22:28:38 -03:00
<Column field="name" header="OS Name" :sortable="true" headerStyle="width:14%; Fin-width:10rem;">
2023-02-01 23:51:02 -03:00
<template #body="slotProps">
2023-02-06 22:28:38 -03:00
<span class="p-column-title">OS Name</span>
2023-02-01 23:51:02 -03:00
{{ slotProps.data.name }}
</template>
</Column>
<Column headerStyle="min-width:10rem;">
<template #body="slotProps">
<Button icon="pi pi-pencil" class="p-button-rounded p-button-success mr-2"
2023-02-14 18:23:22 -03:00
@click="editUser(slotProps.data)" />
2023-02-01 23:51:02 -03:00
<Button icon="pi pi-trash" class="p-button-rounded p-button-warning mt-2"
2023-02-14 18:23:22 -03:00
@click="confirmDeleteUser(slotProps.data)" />
2023-02-01 23:51:02 -03:00
</template>
</Column>
</DataTable>
2023-02-14 18:23:22 -03:00
<Dialog v-model:visible="userDialog" :style="{ width: '450px' }" header="New OS" :modal="true"
2023-02-07 23:32:16 -03:00
class="p-fluid">
2023-02-01 23:51:02 -03:00
<div class="field">
<label for="name">Name</label>
2023-02-06 22:28:38 -03:00
<InputText id="name" v-model.trim="os.name" required="true" autofocus
:class="{ 'p-invalid': submitted && !os.name }" />
<small class="p-invalid" v-if="submitted && !os.name">Name is required.</small>
2023-02-01 23:51:02 -03:00
</div>
<template #footer>
<Button label="Cancel" icon="pi pi-times" class="p-button-text" @click="hideDialog" />
2023-02-06 22:28:38 -03:00
<Button label="Save" icon="pi pi-check" class="p-button-text" @click="onCreateClick" />
2023-02-01 23:51:02 -03:00
</template>
</Dialog>
2023-02-14 18:23:22 -03:00
<Dialog v-model:visible="userDialogChange" :style="{ width: '450px' }" header="New OS" :modal="true"
2023-02-07 23:32:16 -03:00
class="p-fluid">
<div class="field">
<label for="name">Name</label>
<InputText id="name" v-model.trim="os.name" required="true" autofocus
:class="{ 'p-invalid': submitted && !os.name }" />
<small class="p-invalid" v-if="submitted && !os.name">Name is required.</small>
</div>
<template #footer>
<Button label="Cancel" icon="pi pi-times" class="p-button-text" @click="hideDialogChange" />
<Button label="Save" icon="pi pi-check" class="p-button-text" @click="onChangeOS" />
</template>
</Dialog>
2023-02-01 23:51:02 -03:00
2023-02-14 18:23:22 -03:00
<Dialog v-model:visible="deleteUserDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
2023-02-01 23:51:02 -03:00
<div class="flex align-items-center justify-content-center">
<i class="pi pi-exclamation-triangle mr-3" style="font-size: 2rem" />
2023-02-06 22:28:38 -03:00
<span v-if="os">Are you sure you want to delete <b>{{ os.name }}</b>?</span>
2023-02-01 23:51:02 -03:00
</div>
<template #footer>
2023-02-14 18:23:22 -03:00
<Button label="No" icon="pi pi-times" class="p-button-text" @click="deleteUserDialog = false" />
<Button label="Yes" icon="pi pi-check" class="p-button-text" @click="deleteUser" />
2023-02-01 23:51:02 -03:00
</template>
</Dialog>
2023-02-14 18:23:22 -03:00
<Dialog v-model:visible="deleteUsersDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
2023-02-01 23:51:02 -03:00
<div class="flex align-items-center justify-content-center">
<i class="pi pi-exclamation-triangle mr-3" style="font-size: 2rem" />
2023-02-14 18:23:22 -03:00
<span v-if="os">Are you sure you want to delete the selected users?</span>
2023-02-01 23:51:02 -03:00
</div>
<template #footer>
2023-03-01 23:34:29 -03:00
<Button label="No" icon="pi pi-times" class="p-button-text" @click="deleteUsersDialog = false" />
2023-02-14 18:23:22 -03:00
<Button label="Yes" icon="pi pi-check" class="p-button-text" @click="deleteSelectedUsers" />
2023-02-01 23:51:02 -03:00
</template>
</Dialog>
</div>
</div>
</div>
</template>
<style scoped lang="scss">
@import '@/assets/badges.scss';
</style>