Commit e3d81eb9 authored by Dhevan's avatar Dhevan

crud use done

parent 058f162e
...@@ -5,7 +5,17 @@ ...@@ -5,7 +5,17 @@
<h6>{{ title }}</h6> <h6>{{ title }}</h6>
<hr /> <hr />
</div> </div>
<div
v-if="isFetching"
class="card-body p-5 text-center d-flex align-items-center justify-content-center"
style="min-height: 300px"
>
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
<DynamicForm <DynamicForm
v-if="!isFetching"
ref="createForm" ref="createForm"
:schemas="schemas" :schemas="schemas"
@formSubmit="handleSubmit" @formSubmit="handleSubmit"
...@@ -20,9 +30,14 @@ ...@@ -20,9 +30,14 @@
</template> </template>
<script setup> <script setup>
import { ref, onBeforeMount } from "vue"; import { ref, onBeforeMount } from "vue";
import { useRouter, useRoute } from "vue-router";
import BasePage from "./BasePage.vue"; import BasePage from "./BasePage.vue";
import DynamicForm from "./../Form/DynamicForm.vue"; import DynamicForm from "./../Form/DynamicForm.vue";
import { Swal, showLoading, hideLoading } from "@/utils/alert.js"; import { Swal, showLoading, hideLoading } from "@/utils/alert.js";
const emit = defineEmits(["saved", "dataLoaded"]);
const route = useRoute();
const props = defineProps({ const props = defineProps({
schemas: { schemas: {
type: Array, type: Array,
...@@ -33,20 +48,29 @@ const props = defineProps({ ...@@ -33,20 +48,29 @@ const props = defineProps({
default: "", default: "",
}, },
api: {}, api: {},
isEdit: {
default: false,
},
}); });
const createForm = ref(null); const createForm = ref(null);
const isFetching = ref(false);
const formValue = ref({});
const handleSubmit = async (payload, { resetForm }) => { const handleSubmit = async (payload, { resetForm }) => {
// console.log(payload) // console.log(payload)
// return // return
try { try {
showLoading(); showLoading();
const response = await props.api.store(payload); const response = props.isEdit
? await props.api.update(route.params.id, payload)
: await props.api.store(payload);
hideLoading(); hideLoading();
Swal.fire("Sukses", "", "success"); Swal.fire("Sukses", "", "success");
resetForm(); if (!props.isEdit) {
emit("created", response); resetForm();
}
emit("saved", response);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
if (err.response && err.response.status == 422) { if (err.response && err.response.status == 422) {
...@@ -59,8 +83,6 @@ const handleSubmit = async (payload, { resetForm }) => { ...@@ -59,8 +83,6 @@ const handleSubmit = async (payload, { resetForm }) => {
} }
}; };
const formValue = ref({});
onBeforeMount(async () => { onBeforeMount(async () => {
const initialValue = {}; const initialValue = {};
props.schemas.forEach((schema) => { props.schemas.forEach((schema) => {
...@@ -69,5 +91,26 @@ onBeforeMount(async () => { ...@@ -69,5 +91,26 @@ onBeforeMount(async () => {
} }
}); });
formValue.value = initialValue; formValue.value = initialValue;
if (props.isEdit) {
isFetching.value = true;
let response = await props.api.detail(route.params.id);
const data = response.result;
emit("dataLoaded", data);
props.schemas.forEach((schema) => {
if (schema.type == "password") {
data[schema.name] = null;
}
if (schema.type == "checkbox") {
if (data[schema.name] == 1) {
data[schema.name] = true;
} else {
data[schema.name] = false;
}
}
});
formValue.value = data;
isFetching.value = false;
}
}); });
</script> </script>
import UserProfile from "./../views/user/UserProfile.vue"; import UserProfile from "./../views/user/UserProfile.vue";
import UserIndex from "./../views/user/UserIndex.vue"; import UserIndex from "./../views/user/UserIndex.vue";
import UserCreate from "@/views/user/UserCreate.vue"; import UserCreate from "@/views/user/UserCreate.vue";
import UserEdit from "@/views/user/UserEdit.vue";
export const userRoutes = [ export const userRoutes = [
{ {
path: "/user", path: "/user",
...@@ -18,7 +19,7 @@ export const userRoutes = [ ...@@ -18,7 +19,7 @@ export const userRoutes = [
{ {
path: "/user/:id/edit", path: "/user/:id/edit",
name: "edit", name: "edit",
component: UserIndex, component: UserEdit,
}, },
{ {
path: "/user/profile", path: "/user/profile",
......
<template> <template>
<form-page title="Tambah User" :schemas="schemas"></form-page> <form-page title="Tambah User" :schemas="schemas" :api="UserApi"></form-page>
</template> </template>
<script setup> <script setup>
import * as Yup from "yup"; import * as Yup from "yup";
import FormPage from "@/components/Page/FormPage.vue"; import FormPage from "@/components/Page/FormPage.vue";
import { UserApi } from "@/api/user.js";
const schemas = [ const schemas = [
{ {
label: "Nama User", label: "Nama User",
...@@ -29,7 +29,7 @@ const schemas = [ ...@@ -29,7 +29,7 @@ const schemas = [
name: "password", name: "password",
validation: Yup.string().required().label("Password User"), validation: Yup.string().required().label("Password User"),
cols: 6, cols: 6,
type: "password" type: "password",
}, },
]; ];
</script> </script>
<template>
<form-page
:isEdit="true"
title="Edit User"
:schemas="schemas"
:api="UserApi"
></form-page>
</template>
<script setup>
import * as Yup from "yup";
import FormPage from "@/components/Page/FormPage.vue";
import { UserApi } from "@/api/user.js";
const schemas = [
{
label: "Nama User",
name: "name",
validation: Yup.string().required().label("Nama User"),
cols: 6,
},
{
label: "Email",
name: "email",
validation: Yup.string().required().label("Email User"),
cols: 6,
},
{
label: "username",
name: "username",
validation: Yup.string().required().label("Username"),
cols: 6,
},
{
label: "Password User",
name: "password",
validation: Yup.string().required().label("Password User"),
cols: 6,
type: "password",
},
];
</script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment