diff --git a/app/Http/Controllers/GeneralController.php b/app/Http/Controllers/GeneralController.php index 9cb1a4b..2610381 100644 --- a/app/Http/Controllers/GeneralController.php +++ b/app/Http/Controllers/GeneralController.php @@ -16,4 +16,9 @@ class GeneralController extends Controller { return Inertia::render('Jabatan/Index'); } + + public function karyawan() + { + return Inertia::render('Karyawan/Index'); + } } diff --git a/resources/js/Layouts/Authenticated.jsx b/resources/js/Layouts/Authenticated.jsx index c717e03..3b81550 100644 --- a/resources/js/Layouts/Authenticated.jsx +++ b/resources/js/Layouts/Authenticated.jsx @@ -121,7 +121,7 @@ export default function Authenticated({ auth, children }) {
  • - + Karyawan
  • diff --git a/resources/js/Pages/Auth/Login.jsx b/resources/js/Pages/Auth/Login.jsx index 06990b9..72c8408 100644 --- a/resources/js/Pages/Auth/Login.jsx +++ b/resources/js/Pages/Auth/Login.jsx @@ -1,14 +1,12 @@ -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import Button from '@/Components/Button'; -import Checkbox from '@/Components/Checkbox'; import Guest from '@/Layouts/Guest'; import Input from '@/Components/Input'; import Label from '@/Components/Label'; -import ValidationErrors from '@/Components/ValidationErrors'; import { Head, Link, useForm } from '@inertiajs/inertia-react'; import { getAllUsers } from '@/Services/User'; -import { forEach } from 'lodash'; import { Inertia } from '@inertiajs/inertia'; +import { create } from '@/Services/Karyawan'; export default function Login({ status, canResetPassword }) { const [isLoading, setLoading] = useState(false) @@ -46,6 +44,30 @@ export default function Login({ status, canResetPassword }) { .finally(() => setLoading(false)) }; + useEffect(() => { + // create default user + getAllUsers() + .then(items => { + if(items.length <= 0) { + create({ + nik: "1234567890", + name: "admin", + username: "admin", + password: "admin", + jabatan: { + id: "1", + nama: "admin", + tunjangan: "0", + }, + jenisKelamin: "Laki-Laki", + status: "Karyawan Tetap", + is_admin: "true", + }) + } + }) + + }, []) + return ( diff --git a/resources/js/Pages/Jabatan/Index.jsx b/resources/js/Pages/Jabatan/Index.jsx index 72e9bc0..cd2ed30 100644 --- a/resources/js/Pages/Jabatan/Index.jsx +++ b/resources/js/Pages/Jabatan/Index.jsx @@ -50,9 +50,6 @@ export default function Jabatan(props) { - @@ -65,9 +62,6 @@ export default function Jabatan(props) { {items.map(item => ( - diff --git a/resources/js/Pages/Karyawan/FormModal.jsx b/resources/js/Pages/Karyawan/FormModal.jsx new file mode 100644 index 0000000..5f9ee98 --- /dev/null +++ b/resources/js/Pages/Karyawan/FormModal.jsx @@ -0,0 +1,222 @@ +import React, { useEffect, useState } from 'react' +import { toast } from 'react-toastify' +import { Modal } from '@/Components/Modal' +import { useForm } from '@inertiajs/inertia-react' +import Button from '@/Components/Button' +import Input from '@/Components/Input' +import { create, update } from '@/Services/Karyawan' +import { getAll as GetAllJabatan } from '@/Services/Jabatan' + +export default function FormModal({ modalState, refresh }) { + const [loading, setLoading] = useState(false) + const { data, setData, reset } = useForm({ + nik: '', + name: '', + username: '', + password: '', + jenisKelamin: "Laki-Laki", + jabatan: '', + status: 'Karyawan Tetap', + is_admin: false + }) + + const [jabatans, setJabatans] = useState([]) + + useEffect(() => { + GetAllJabatan() + .then((items) => setJabatans(items)) + },[]) + + useEffect(() => { + if (modalState.isOpen === false) { + reset() + modalState.setData(null) + } + if (modalState.data !== null) { + setData(modalState.data.data) + } + }, [modalState]) + + const onHandleChange = (event) => { + setData( + event.target.name, + event.target.type === 'checkbox' + ? event.target.checked + : event.target.value + ) + } + + const onHandleJabatanChange = (event) => { + if (event.target.value == "") { + setData({ + ...data, + jabatan: "" + }) + return + } + const jabatan = jabatans.find(item => item.id == event.target.value) + setData({ + ...data, + jabatan: {...jabatan.data, id : jabatan.id} + }) + } + + const submit = (e) => { + e.preventDefault() + setLoading(true) + if (modalState.data !== null) { + update(data, modalState.data.id) + .finally(() => { + reset() + toast.success("berhasil update") + setLoading(false) + modalState.toggle() + refresh() + }) + } else { + create(data) + .then((id) => console.log(id)) + .finally(() => { + reset() + toast.success("berhasil simpan") + setLoading(false) + modalState.toggle() + refresh() + }) + } + } + + return ( + +
    Karyawan
    + + + + + + + + + +
    + ) +} \ No newline at end of file diff --git a/resources/js/Pages/Karyawan/Index.jsx b/resources/js/Pages/Karyawan/Index.jsx new file mode 100644 index 0000000..75237de --- /dev/null +++ b/resources/js/Pages/Karyawan/Index.jsx @@ -0,0 +1,91 @@ +import React, { useEffect, useState } from 'react'; +import Authenticated from '@/Layouts/Authenticated'; +import { Head } from '@inertiajs/inertia-react'; +import { useModalState } from '@/Hooks' +import Button from '@/Components/Button'; +import FormModal from './FormModal'; +import { getAll, deleteById } from '@/Services/Karyawan'; +import { toast } from 'react-toastify'; + +export default function Karyawan(props) { + const formModal = useModalState(false) + const [items, setItems] = useState([]) + + const hanldeDeleteClick = (item) => { + const con = confirm("delete item?") + if (con) { + deleteById(item.id) + .then(() => toast.success('berhasil hapus')) + .finally(() => fetchData()) + } + } + + const handleEditClick = (item) => { + formModal.setData(item) + formModal.toggle() + } + + const fetchData = () => { + getAll() + .then(items => setItems(items)) + } + + useEffect(() => { + fetchData() + }, []) + + return ( + + + +
    +
    +
    +
    + +
    +
    - ID - Nama
    - {item.id} - {item.data.nama}
    + + + + + + + + + {items.map(item => ( + + + + + + ))} + +
    + Nama + + Jabatan +
    + {item.data.name} + + {item.data.jabatan.nama} + +
    + + {item.data.jabatan.id != "1" && ( + + )} +
    +
    + + + + + + + + ); +} diff --git a/resources/js/Services/Karyawan.js b/resources/js/Services/Karyawan.js new file mode 100644 index 0000000..f63f274 --- /dev/null +++ b/resources/js/Services/Karyawan.js @@ -0,0 +1,41 @@ +import db from "@/firebase"; +import { collection, getDocs, doc, addDoc, deleteDoc, updateDoc } from "firebase/firestore"; + +const COLLECTION = "users" + +async function getAll() { + const collect = collection(db, COLLECTION) + const data = await getDocs(collect) + const lists = data.docs.map(doc => { + return { + data: doc.data(), + id: doc.id + } + }) + return lists +} + +async function create(payload) { + const docRef = await addDoc(collection(db, COLLECTION), payload) + return docRef.id +} + +async function update(payload, id){ + const docRef = doc(db, COLLECTION, id) + const result = await updateDoc(docRef, payload); + return result +} + +async function deleteById(id) { + console.log(id) + const docRef = doc(db, COLLECTION, id) + const result = await deleteDoc(docRef); + return result +} + +export { + getAll, + create, + update, + deleteById +} \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 98e10ad..fd7cfa4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -30,5 +30,6 @@ Route::middleware([IsGuest::class])->group(function () { Route::middleware([IsSessionAuth::class])->group(function () { Route::get('/dashboard', [GeneralController::class, 'dashboard'])->name('dashboard'); Route::get('/jabatan', [GeneralController::class, 'jabatan'])->name('jabatan'); + Route::get('/karyawan', [GeneralController::class, 'karyawan'])->name('karyawan'); Route::post('/logout', [AuthController::class, 'destroy'])->name('logout'); });