Desc:
- Perubahan pengambilan data dari API ke Function use server
No issue
This commit is contained in:
2023-10-18 16:09:32 +08:00
parent 5f430786b4
commit 2a97165d1f
46 changed files with 398 additions and 345 deletions

View File

@@ -10,6 +10,7 @@ import { myConsole } from "@/app/fun/my_console";
import toast from "react-simple-toasts";
import { ApiHipmi } from "@/app/lib/api";
import { useRouter } from "next/navigation";
import _ from "lodash";
export default function Register() {
const route = useRouter();
@@ -20,7 +21,7 @@ export default function Register() {
myConsole(value);
const body = {
username: value,
username: _.lowerCase(value),
nomor: nomor,
};

View File

@@ -45,6 +45,7 @@ import { myConsole } from "@/app/fun/my_console";
import { getFotoProfile } from "../katalog/profile/api/get-foto-profile";
import { funGetUserProfile } from "../fun/get_user_profile";
import { USER_PROFILE } from "../models/user_profile";
import AppNotif from "../notif";
const listHalaman = [
{
@@ -96,29 +97,8 @@ export default function HomeView({ user }: { user: USER_PROFILE }) {
const router = useRouter();
const [stateUser, setStateUser] = useState(user);
// const [token, setToken] = useAtom(gs_token);
// useShallowEffect(() => {
// getUserId();
// }, []);
// async function getUserId() {
// const get = await getToken();
// if (!get) return myConsole("Data Kosong");
// setToken(get);
// }
// const [profile, setProfile] = useAtom(gs_profile);
// useShallowEffect(() => {
// loadProfile();
// }, []);
// async function loadProfile() {
// const get = await getProfile();
// if (!get) return myConsole("Data Kosong");
// setProfile(get);
// }
return (
<>
{/* <Center><Image src={ApiHipmi.get_foto + foto ?? ""} alt="" height={100} width={100}/></Center> */}
<Box>
<Flex align={"center"} gap={"sm"}>
<ActionIcon
@@ -126,9 +106,9 @@ export default function HomeView({ user }: { user: USER_PROFILE }) {
variant="transparent"
onClick={() => {
if (stateUser.Profile === null) {
return router.push("/dev/profile/create");
return router.push(`/dev/profile/create/${stateUser.id}`);
} else {
return router.push("/dev/katalog/view");
return router.push(`/dev/katalog/${stateUser.Profile.id}`);
}
}}
>

View File

@@ -5,7 +5,7 @@ import { ActionIcon, AppShell, Group, Header, Text } from "@mantine/core";
import { IconUserSearch, IconAward, IconQrcode, IconArrowLeft, IconPencilPlus } from "@tabler/icons-react";
import { useRouter } from "next/navigation";
export default function KatalogLayout({ children }: { children: any }) {
export default function KatalogLayout({ children, profileId }: { children: any, profileId: any }) {
const router = useRouter()
return (
<>
@@ -25,7 +25,7 @@ export default function KatalogLayout({ children }: { children: any }) {
Katalog
</Text>
<Group spacing={"sm"}>
<ActionIcon variant="transparent" onClick={() => router.push("/dev/portofolio/create")}>
<ActionIcon variant="transparent" onClick={() => router.push(`/dev/portofolio/create/${profileId}`)}>
<IconPencilPlus />
</ActionIcon>
{/* <Logout /> */}

View File

@@ -4,7 +4,7 @@ import { ActionIcon, AppShell, Group, Header, Text } from "@mantine/core";
import { IconArrowLeft } from "@tabler/icons-react";
import { useRouter } from "next/navigation";
export default function CreatePortofolioLayout({ children }: { children: any }) {
export default function CreatePortofolioLayout({ children, profileId }: { children: any, profileId: any }) {
const router = useRouter();
return (
<>
@@ -14,7 +14,7 @@ export default function CreatePortofolioLayout({ children }: { children: any })
<Group position="apart" h={50}>
<ActionIcon
variant="transparent"
onClick={() => router.push("/dev/katalog/view")}
onClick={() => router.push(`/dev/katalog/${profileId}`)}
>
<IconArrowLeft />
</ActionIcon>

View File

@@ -9,6 +9,7 @@ import _ from "lodash";
import { useRouter } from "next/navigation";
import { useState } from "react";
import toast from "react-simple-toasts";
import funCreatePortofolio from "../fun/fun_create_portofolio";
export default function CreatePortofolio({
bidangBisnis,
@@ -23,7 +24,7 @@ export default function CreatePortofolio({
bidangBisnisId: "",
alamatKantor: "",
tlpn: "",
deskripssi: "",
deskripsi: "",
});
async function onSubmit() {
@@ -33,33 +34,24 @@ export default function CreatePortofolio({
masterBidangBisnisId: value.bidangBisnisId,
alamatKantor: value.alamatKantor,
tlpn: value.tlpn,
deskripssi: value.deskripssi,
deskripsi: value.deskripsi,
};
if (_.values(body).includes("")) return toast("Lengkapi Data");
await fetch(ApiHipmi.create_portofolio, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
})
.then((res) => res.json())
.then((val) => {
myConsole(val);
if (val.status == 201) {
toast("Berhasil disimpan");
return router.push("/dev/katalog/view");
} else {
return toast("Gagal disimpa");
}
});
await funCreatePortofolio(body as any).then((res) => {
if (res.status === 201) {
toast("Berhasil disimpan");
return setTimeout(() => router.push(`/dev/katalog/${profileId}`), 1000)
} else {
return toast("Gagal disimpan");
}
});
}
return (
<>
{/* {JSON.stringify(bidangBisnis)} */}
{/* {JSON.stringify(profileId)} */}
<Stack px={"sm"}>
<TextInput
@@ -73,7 +65,7 @@ export default function CreatePortofolio({
/>
<Select
label="Bidang Bisnis"
data={_.map(bidangBisnis as any).map((e : any) => ({
data={_.map(bidangBisnis as any).map((e: any) => ({
value: e.id,
label: e.name,
}))}
@@ -108,7 +100,7 @@ export default function CreatePortofolio({
onChange={(val) => {
setValue({
...value,
deskripssi: val.target.value,
deskripsi: val.target.value,
});
}}
/>

View File

@@ -0,0 +1,32 @@
"use server";
import prisma from "@/app/lib/prisma";
import { MODEL_PORTOFOLIO } from "@/app_modules/models/portofolio";
import { revalidatePath } from "next/cache";
export default async function funCreatePortofolio(data: MODEL_PORTOFOLIO) {
console.log(data);
const res = await prisma.katalog.create({
data: {
profileId: data.profileId,
namaBisnis: data.namaBisnis,
deskripsi: data.deskripsi,
tlpn: data.tlpn,
alamatKantor: data.alamatKantor,
masterBidangBisnisId: data.masterBidangBisnisId,
},
});
if (!res)
return {
status: 401,
success: false,
};
revalidatePath(`/dev/katalog/${data.profileId}`);
return {
status: 201,
success: true,
};
}

View File

@@ -12,9 +12,10 @@ export async function funGetListPortofolio(profileId: any) {
namaBisnis: true,
alamatKantor: true,
tlpn: true,
deskripssi: true,
deskripsi: true,
masterBidangBisnisId: true,
active: true,
profileId: true
},
});

View File

@@ -12,7 +12,7 @@ export async function getOnePortofolio(id: string) {
id: true,
namaBisnis: true,
alamatKantor: true,
deskripssi: true,
deskripsi: true,
tlpn: true,
active: true,
MasterBidangBisnis: {
@@ -22,6 +22,7 @@ export async function getOnePortofolio(id: string) {
active: true,
},
},
profileId: true,
},
});

View File

@@ -6,14 +6,14 @@ import HeaderTransparent from "../../component/header_transparent";
import { useRouter } from "next/navigation";
import { IconArrowLeft, IconEdit } from "@tabler/icons-react";
export default function PortofolioLayout({ children }: { children: any }) {
export default function PortofolioLayout({ children, profileId }: { children: any, profileId: any }) {
const router = useRouter();
return (
<>
<AppShell
header={
<HeaderTransparent
route={"/dev/katalog/view"}
route={`/dev/katalog/${profileId}`}
title="Portofolio"
icon2={
<ActionIcon

View File

@@ -1,7 +1,7 @@
"use client";
import { Warna } from "@/app/lib/warna";
import { GET_ONE_PORTOFOLIO } from "@/app_modules/models/portofolio";
import { MODEL_PORTOFOLIO } from "@/app_modules/models/portofolio";
import { Box, Button, Center, Text, Title } from "@mantine/core";
import { IconTrash } from "@tabler/icons-react";
import { useRouter } from "next/navigation";
@@ -10,29 +10,22 @@ import { useState } from "react";
export default function ViewPortofolio({
dataPorto,
}: {
dataPorto: GET_ONE_PORTOFOLIO;
dataPorto: MODEL_PORTOFOLIO;
}) {
const router = useRouter();
const [porto, setPorto] = useState(dataPorto);
return (
<>
<Box>
<Text>{porto.namaBisnis}</Text>
<Text>{porto.alamatKantor}</Text>
<Text>+{porto.tlpn}</Text>
<Text>{porto.deskripssi}</Text>
<Text>{porto.deskripsi}</Text>
<Text>{porto.MasterBidangBisnis.name}</Text>
</Box>
<Center mt={"md"}>
<Button bg={"red"} color="red" w={300}
onClick={() => {
}}
>
<Button bg={"red"} color="red" w={300} onClick={() => {}}>
<IconTrash />
</Button>{" "}
</Center>

View File

@@ -10,10 +10,10 @@ import _ from "lodash";
import { useRouter } from "next/navigation";
import { useState } from "react";
import toast from "react-simple-toasts";
import funCreateNewProfile from "../fun/fun_create_profile";
export default function CreateProfile() {
export default function CreateProfile({ userId }: { userId: any }) {
const router = useRouter();
const [token, setToken] = useAtom(gs_token);
const [value, setValue] = useState({
name: "",
@@ -24,7 +24,7 @@ export default function CreateProfile() {
async function onSubmit() {
const body = {
userId: token?.id,
userId: userId,
name: value.name,
email: value.email,
alamat: value.alamat,
@@ -36,22 +36,14 @@ export default function CreateProfile() {
if (_.values(value).includes("")) return toast("Lengkapi data");
// if(_.values(value.email).includes(`${/^\S+@\S+$/.test(value.email) ? null : "Invalid email"}`)){}
await fetch(ApiHipmi.create_profile, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
})
.then((res) => res.json())
.then((val) => {
if (val.status == 201) {
toast("Data tersimpan")
return router.push("/dev/katalog/view");
} else {
return toast("Server Error!!!")
}
});
await funCreateNewProfile(body).then((res) => {
if (res.status === 201) {
toast("Data tersimpan");
return router.push("/dev/katalog/view");
} else {
toast("Gagal")
}
});
}
return (

View File

@@ -11,7 +11,7 @@ import {
import { IconArrowLeft } from "@tabler/icons-react";
import { useRouter } from "next/navigation";
export default function EditProfileLayout({ children }: { children: any }) {
export default function EditProfileLayout({ children, profileId }: { children: any, profileId: any }) {
const router = useRouter()
return (
<>
@@ -20,7 +20,7 @@ export default function EditProfileLayout({ children }: { children: any }) {
header={
<Header height={50} px={"sm"}>
<Group position="apart" h={50}>
<ActionIcon variant="transparent" onClick={() => router.push("/dev/katalog/view")}>
<ActionIcon variant="transparent" onClick={() => router.push(`/dev/katalog/${profileId}`)}>
<IconArrowLeft />
</ActionIcon>
<Text>Edit Profile</Text>

View File

@@ -4,7 +4,7 @@ import { myConsole } from "@/app/fun/my_console";
import { ApiHipmi } from "@/app/lib/api";
import { Warna } from "@/app/lib/warna";
import { gs_token } from "@/app_modules/home/state/global_state";
import { Button, Select, Stack, TextInput } from "@mantine/core";
import { Button, Loader, Select, Stack, TextInput } from "@mantine/core";
import { useShallowEffect } from "@mantine/hooks";
import { useAtom } from "jotai";
import _ from "lodash";
@@ -13,57 +13,53 @@ import { useState } from "react";
import toast from "react-simple-toasts";
import { gs_profile } from "../state/global_state";
import { loadDataProfile } from "../fun/fun_get_profile";
import { USER_PROFILE } from "@/app_modules/models/user_profile";
import funEditProfile from "../fun/fun_edit_profile";
export default function EditProfile({ data }: { data: any }) {
export default function EditProfile({ data }: { data: USER_PROFILE }) {
const router = useRouter();
//Get data profile
const [profile, setProfile] = useAtom(gs_profile)
useShallowEffect(() => {
loadDataProfile(setProfile);
}, []);
const [dataProfile, setDataProfile] = useState(data);
async function onUpdate() {
const body = profile;
const body = dataProfile;
if (_.values(body).includes("")) return toast("Lengkapi data");
await fetch(ApiHipmi.edit_profile, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
})
.then((res) => res.json())
.then((val) => {
myConsole(val);
if (val.status == 200) {
toast("Data tersimpan");
loadDataProfile(setProfile)
return setTimeout(() => router.push("/dev/katalog/view"), 1000);
} else {
return toast("Gagal update !!!");
}
});
await funEditProfile(body).then((res) => {
if (res.status === 200) {
toast("Update berhasil");
setTimeout(() => router.push(`/dev/katalog/${data.Profile?.id}`), 1000);
} else {
toast("Gagal update");
}
});
}
if(!profile) return <></>
if (!dataProfile)
return (
<>
<Loader />
</>
);
return (
<>
{/* {JSON.stringify(profile)} */}
<Stack px={"sm"}>
<TextInput label="Username" disabled value={profile?.User.username} />
<TextInput label="Nomor" disabled value={profile?.User.nomor} />
<TextInput label="Username" disabled value={dataProfile.username} />
<TextInput label="Nomor" disabled value={dataProfile.nomor} />
<TextInput
label="Nama"
placeholder="username"
value={profile?.name}
value={dataProfile.Profile?.name}
onChange={(val) => {
setProfile({
...profile,
name: val.target.value,
setDataProfile({
...(dataProfile as any),
Profile: {
...dataProfile.Profile,
name: val.target.value,
},
});
}}
/>
@@ -71,12 +67,14 @@ export default function EditProfile({ data }: { data: any }) {
<TextInput
label="Email"
placeholder="email"
value={profile?.email}
value={dataProfile.Profile?.email}
onChange={(val) => {
myConsole(val.target.value);
setProfile({
...profile,
email: val.target.value,
setDataProfile({
...(dataProfile as any),
Profile: {
...dataProfile.Profile,
email: val.target.value,
},
});
}}
/>
@@ -84,27 +82,32 @@ export default function EditProfile({ data }: { data: any }) {
<TextInput
label="Alamat"
placeholder="alamat"
value={profile?.alamat}
value={dataProfile.Profile?.alamat}
onChange={(val) => {
myConsole(val.target.value);
setProfile({
...profile,
alamat: val.target.value,
setDataProfile({
...(dataProfile as any),
Profile: {
...dataProfile.Profile,
alamat: val.target.value,
},
});
}}
/>
<Select
label="Jenis Kelamin"
value={profile?.jenisKelamin}
value={dataProfile.Profile?.jenisKelamin}
data={[
{ value: "Laki-laki", label: "Laki-laki" },
{ value: "Perempuan", label: "Perempuan" },
]}
onChange={(val) => {
setProfile({
...profile,
jenisKelamin: val as string,
setDataProfile({
...(dataProfile as any),
Profile: {
...dataProfile.Profile,
jenisKelamin: val,
},
});
}}
/>
@@ -119,6 +122,8 @@ export default function EditProfile({ data }: { data: any }) {
Update
</Button>
</Stack>
{/* <pre>{JSON.stringify(dataProfile, null, 2)}</pre> */}
</>
);
}

View File

@@ -0,0 +1,25 @@
"use server";
import prisma from "@/app/lib/prisma";
export default async function funCreateNewProfile(data: any) {
console.log(data);
const body = data;
const res = await prisma.profile.create({
data: {
userId: body.userId,
name: body.name,
email: body.email,
alamat: body.alamat,
jenisKelamin: body.jenisKelamin,
},
});
if (!res) return { status: 400 };
return {
status: 201,
success: true,
};
}

View File

@@ -0,0 +1,25 @@
"use server";
import prisma from "@/app/lib/prisma";
import { USER_PROFILE } from "@/app_modules/models/user_profile";
export default async function funEditProfile(data: USER_PROFILE) {
const res = await prisma.profile.update({
where: {
id: data.Profile?.id,
},
data: {
name: data.Profile?.name,
email: data.Profile?.email,
alamat: data.Profile?.alamat,
jenisKelamin: data.Profile?.jenisKelamin,
},
});
if (!res) return { status: 400 };
return {
status: 200,
success: true,
};
}

View File

@@ -45,7 +45,6 @@ export default function ProfileView({ user }: { user: USER_PROFILE }) {
if (!stateUser) return <></>;
return (
<>
{/* {JSON.stringify(stateUser)} */}
{/* Background dan foto */}
<Box>
<Paper bg={"gray"} p={"md"}>
@@ -67,7 +66,6 @@ export default function ProfileView({ user }: { user: USER_PROFILE }) {
}}
>
<Center h={101}>
{/* {stateUser.Profile?.ImageProfile?.url} */}
{stateUser.Profile?.ImageProfile?.url && (
<Image
src={ApiHipmi.get_foto + stateUser.Profile?.ImageProfile?.url}
@@ -75,11 +73,6 @@ export default function ProfileView({ user }: { user: USER_PROFILE }) {
radius={100}
width={100}
height={100}
sx={
{
// position: "fixed",
}
}
/>
)}
</Center>
@@ -93,7 +86,7 @@ export default function ProfileView({ user }: { user: USER_PROFILE }) {
variant="transparent"
bg={"gray"}
radius={50}
onClick={() => router.push("/dev/profile/upload")}
onClick={() => router.push(`/dev/profile/upload/${stateUser.Profile?.id}`)}
sx={{ position: "relative" }}
>
<IconCamera color="black" size={20} />
@@ -112,7 +105,7 @@ export default function ProfileView({ user }: { user: USER_PROFILE }) {
<ActionIcon
variant="transparent"
onClick={() => {
router.push("/dev/profile/edit");
router.push(`/dev/profile/edit/${stateUser.id}`);
}}
>
<IconEditCircle color={Warna.hijau_muda} size={20} />

View File

@@ -18,27 +18,29 @@ import { useShallowEffect } from "@mantine/hooks";
import { loadDataProfile } from "../fun/fun_get_profile";
import { funUploadFoto } from "../fun/upload_foto";
import { useRouter } from "next/navigation";
import { useState } from "react";
export default function UploadFotoProfileLayout({
children,
profileId
}: {
children: any;
profileId: any
}) {
const router = useRouter()
const [profile, setProfile] = useAtom(gs_profile);
useShallowEffect(() => {
loadDataProfile(setProfile);
}, []);
const [profile, setProfile] = useState(profileId)
return (
<>
{JSON.stringify(profileId)}
<AppShell
header={
<Header height={50} px={"sm"}>
<Group position="apart" h={50}>
<ActionIcon
variant="transparent"
onClick={() => router.push("/dev/katalog/view")}
onClick={() => router.push(`/dev/katalog/${profile}`)}
>
<IconArrowLeft />
</ActionIcon>
@@ -54,7 +56,7 @@ export default function UploadFotoProfileLayout({
<Flex direction={"column"} align={"center"}>
<FileButton
onChange={async (files) => {
const id = profile?.id
const id = profile
if (!files) return toast("File Kosong");
const fd = new FormData();
@@ -63,7 +65,7 @@ export default function UploadFotoProfileLayout({
const upFoto = await funUploadFoto(fd, id);
if (upFoto.success) {
toast("Upload berhasil");
router.push("/dev/katalog/view")
router.push(`/dev/katalog/${profile}`)
// loadDataProfile(valUser.id, setUser, setProfile);
}
}}

View File

@@ -6,6 +6,7 @@ export interface LIST_PORTOFOLIO {
deskripssi: string;
masterBidangBisnisId: string;
active: boolean;
profileId: string
}
export interface BIDANG_BISNIS {
@@ -14,12 +15,14 @@ export interface BIDANG_BISNIS {
active: boolean;
}
export interface GET_ONE_PORTOFOLIO {
export interface MODEL_PORTOFOLIO {
id: string;
namaBisnis: string;
alamatKantor: string;
deskripssi: string;
deskripsi: string;
tlpn: string;
active: boolean;
MasterBidangBisnis: BIDANG_BISNIS;
masterBidangBisnisId: string
profileId: string,
}