aghabalaguluzade
<script setup>
import { Head, Link, useForm, usePage, router } from "@inertiajs/vue3";
import axios from "axios";
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout.vue";
import SearchArea from "@/Components/SearchArea.vue";
import Modal from "@/Components/Modal.vue";
import Table from "@/Components/Table.vue";
import Pagination from "@/Components/Pagination.vue";
import { ref, reactive } from "vue";
import { computed } from "vue";
import { __, useCan } from "@/Modules/general";
const showModal = ref(false);
const roles = reactive(usePage().props.roles);
const users = reactive(usePage().props.users);
const search = (payload) => {
router.get(route("user.index"), {
search: payload,
});
console.log(payload);
};
const saveUser = () => {
if (form.id) {
form.put(route("user.update", { id: form.id }));
} else {
form.post(route("user.store"));
}
closeModal();
router.reload({ only: ['users'] })
};
const newItem = () => {
form.reset();
showModal.value = true;
};
const closeModal = () => {
form.reset();
showModal.value = false;
};
const form = useForm({
id: null,
email: "",
name: "",
password: "",
photo: "",
role: "",
});
const fields = [
{ key: "id", name: __("Id") },
{ key: "photo", name: __("Fotoğraf") },
{ key: "name", name: __("Adı") },
{ key: "email", name: __("E-Posta") },
{ key: "role", name: __("Rolü") },
];
const formatedItems = computed(() => {
let items = [];
users.data.forEach((item, index) => {
items.push({
id: item.id,
name: item.name,
photo: item.photo,
email: item.email,
role: item.roles.map((role) => role.name).join(", "),
});
});
return items;
});
const editUser = (payload) => {
form.reset();
axios
.post(route("user.show"), {
id: payload,
})
.then((response) => {
let data = response.data.data;
form.id = data.id;
form.photo = data.photo;
form.name = data.name;
form.photo = data.photo;
form.email = data.email;
form.role = data.roles[0].id;
showModal.value = true;
});
};
const deleteUser = (payload) => {
router.delete(route("user.delete", { id: payload }));
};
</script>
<template>
<AuthenticatedLayout
:title="__('Kullanıcılar')"
:class="{ 'blur-background': showModal }"
>
<Head :title="__('Kullanıcılar')" />
<div class="container-fluid">
<SearchArea
@search="search($event)"
@newItem="newItem()"
:new-item-button-text="__('Kullanıcı Ekle')"
/>
<Table
:fields="fields"
:items.sync="formatedItems"
:canDelete="'delete user'"
:canEdit="'edit user'"
@edit="editUser($event)"
@delete="deleteUser($event)"
>
</Table>
<Pagination :links="users.links" />
</div>
<Modal :show.sync="showModal" @save="saveUser()" @close="closeModal()">
<form submit.prevent="submitUser">
<div class="col-12 text-center d-flex justify-content-center" v-if="form.photo">
<img :src="form.photo" alt="image" class="img-fluid avatar-xl rounded" />
</div>
<div class="mb-3">
<label for="example-fileinput" class="form-label">{{ __("Fotoğraf") }}</label>
<input
@input:change="form.photo"
required
type="file"
id="example-fileinput"
class="form-control"
/>
</div>
<div class="mb-3">
<label for="name" class="form-label">{{ __("Adı") }}</label>
<input
class="form-control"
type="text"
v-model="form.name"
required
id="name"
/>
<InputError class="mt-2" :message="form.errors.name" />
</div>
<div class="mb-3">
<label for="emailaddress" class="form-label">{{ __("E Posta Adresi") }}</label>
<input
v-model="form.email"
class="form-control"
type="email"
id="emailaddress"
required
/>
<InputError class="mt-2" :message="form.errors.email" />
</div>
<div class="mb-3">
<label for="password" class="form-label">{{ __("Şifre") }}</label>
<input
class="form-control"
type="password"
v-model="form.password"
required
id="password"
/>
<InputError class="mt-2" :message="form.errors.password" />
</div>
<div class="mb-3">
<label for="example-select" class="form-label">{{ __("Rolü") }}</label>
<select
v-model="form.role"
class="form-select"
id="example-select"
required
name="role"
>
<option v-for="role in roles" :value="role.id">{{ role.name }}</option>
</select>
</div>
</form>
</Modal>
</AuthenticatedLayout>
</template>