feat : update profile
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import { TypeUser } from './lib/type_user';
|
||||
import createLogUser from "./log/fun/createLogUser";
|
||||
import ViewEditProfile from "./profile/view/view_edit_profile";
|
||||
import ViewProfile from "./profile/view/view_profile";
|
||||
import { funGetAllmember } from './member/lib/api_member';
|
||||
import Profile from './profile/ui/profile';
|
||||
import EditProfile from './profile/ui/edit_profile';
|
||||
|
||||
export { ViewProfile };
|
||||
export { ViewEditProfile };
|
||||
export { createLogUser };
|
||||
export type { TypeUser }
|
||||
export { funGetAllmember }
|
||||
export { Profile }
|
||||
export { EditProfile }
|
||||
|
||||
@@ -273,8 +273,8 @@ export default function CreateMember() {
|
||||
onBlur={() => setTouched({ ...touched, nik: true })}
|
||||
error={
|
||||
touched.nik && (
|
||||
listData.nik == "" ? "NIK Tidak Boleh Kosong" :
|
||||
listData.nik.length < 16 ? "NIK Harus 16 Karakter" : null
|
||||
listData.nik === "" ? "NIK Tidak Boleh Kosong" :
|
||||
listData.nik.length !== 16 ? "NIK Harus 16 Karakter" : null
|
||||
)
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -250,10 +250,10 @@ export default function EditMember({ id }: { id: string }) {
|
||||
onBlur={() => setTouched({ ...touched, nik: true })}
|
||||
error={
|
||||
touched.nik && (
|
||||
data.nik == "" ? "NIK Tidak Boleh Kosong" :
|
||||
data.nik.length < 16 ? "NIK Harus 16 Karakter" : null
|
||||
data.nik === "" ? "NIK Tidak Boleh Kosong" :
|
||||
data.nik.length !== 16 ? "NIK Harus 16 Karakter" : null
|
||||
)
|
||||
}
|
||||
}
|
||||
/>
|
||||
<TextInput
|
||||
size="md" type="text" radius={30} placeholder="Nama" withAsterisk label="Nama" w={"100%"}
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
"use client"
|
||||
import { LayoutNavbarNew, WARNA } from "@/module/_global";
|
||||
import { Box, Button, Flex, Modal, Select, Stack, Text, TextInput } from "@mantine/core";
|
||||
import { HiUser } from "react-icons/hi2";
|
||||
import toast from "react-hot-toast";
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||
import { useState } from "react";
|
||||
|
||||
export default function EditProfile() {
|
||||
const [isValModal, setValModal] = useState(false)
|
||||
|
||||
function onTrue(val: boolean) {
|
||||
if (val) {
|
||||
toast.success("Sukses! Data tersimpan");
|
||||
}
|
||||
setValModal(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<LayoutNavbarNew back='' title='Edit Profil' menu='' />
|
||||
<Stack
|
||||
align="center"
|
||||
justify="center"
|
||||
gap="xs"
|
||||
pt={30}
|
||||
px={20}
|
||||
>
|
||||
<Box bg={WARNA.biruTua} py={30} px={50}
|
||||
style={{
|
||||
borderRadius: 10,
|
||||
}}>
|
||||
<HiUser size={100} color={WARNA.bgWhite} />
|
||||
</Box>
|
||||
<TextInput
|
||||
size="md" type="number" radius={30} placeholder="NIK" withAsterisk label="NIK" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
<TextInput
|
||||
size="md" type="text" radius={30} placeholder="Nama" withAsterisk label="Nama" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
<TextInput
|
||||
size="md" type="email" radius={30} placeholder="Email" withAsterisk label="Email" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
<TextInput
|
||||
size="md" type="number" radius={30} placeholder="+62...." withAsterisk label="Nomor Telepon" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
<Select
|
||||
placeholder="Pilih Gender" label="Gender" w={"100%"} size="md" required withAsterisk radius={30}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
data={['Laki-laki', 'Perempuan']}
|
||||
/>
|
||||
</Stack>
|
||||
<Box mt={30} mx={20} pb={20}>
|
||||
<Button
|
||||
c={"white"}
|
||||
bg={WARNA.biruTua}
|
||||
size="md"
|
||||
radius={30}
|
||||
fullWidth
|
||||
onClick={() => setValModal(true)}
|
||||
>
|
||||
Simpan
|
||||
</Button>
|
||||
</Box>
|
||||
<LayoutModal opened={isValModal} onClose={() => setValModal(false)}
|
||||
description="Apakah Anda yakin ingin
|
||||
melakukan perubahan data?"
|
||||
onYes={(val) => { onTrue(val) }} />
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
17
src/module/user/profile/lib/api_profile.ts
Normal file
17
src/module/user/profile/lib/api_profile.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { IEditDataProfile } from "./type_profile";
|
||||
|
||||
export const funGetProfileByCookies = async (path?: string) => {
|
||||
const response = await fetch(`/api/user/profile${(path) ? path : ''}`, { next: { tags: ['profile'] } });
|
||||
return await response.json().catch(() => null);
|
||||
}
|
||||
|
||||
export const funEditProfileByCookies = async ( data: IEditDataProfile) => {
|
||||
const response = await fetch(`/api/user/profile/`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await response.json().catch(() => null);
|
||||
}
|
||||
21
src/module/user/profile/lib/type_profile.ts
Normal file
21
src/module/user/profile/lib/type_profile.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export interface IProfileById {
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
phone: string
|
||||
nik: string
|
||||
gender: string
|
||||
idGroup: string
|
||||
idPosition: string
|
||||
group: string
|
||||
position: string
|
||||
}
|
||||
|
||||
export interface IEditDataProfile {
|
||||
id: string;
|
||||
nik: string;
|
||||
name: string;
|
||||
phone: string;
|
||||
email: string;
|
||||
gender: string;
|
||||
}
|
||||
237
src/module/user/profile/ui/edit_profile.tsx
Normal file
237
src/module/user/profile/ui/edit_profile.tsx
Normal file
@@ -0,0 +1,237 @@
|
||||
"use client"
|
||||
import { LayoutNavbarNew, WARNA } from "@/module/_global";
|
||||
import { Box, Button, Flex, Modal, Select, Stack, Text, TextInput } from "@mantine/core";
|
||||
import { HiUser } from "react-icons/hi2";
|
||||
import toast from "react-hot-toast";
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||
import { useState } from "react";
|
||||
import { IEditDataProfile, IProfileById } from "../lib/type_profile";
|
||||
import { funEditProfileByCookies, funGetProfileByCookies } from "../lib/api_profile";
|
||||
import { useShallowEffect } from "@mantine/hooks";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
|
||||
export default function EditProfile() {
|
||||
const [isValModal, setValModal] = useState(false)
|
||||
const [isDataEdit, setDataEdit] = useState<IProfileById[]>([])
|
||||
const [touched, setTouched] = useState({
|
||||
nik: false,
|
||||
name: false,
|
||||
phone: false,
|
||||
email: false,
|
||||
gender: false,
|
||||
});
|
||||
const [data, setData] = useState<IEditDataProfile>({
|
||||
id: "",
|
||||
nik: "",
|
||||
name: "",
|
||||
phone: "",
|
||||
email: "",
|
||||
gender: "",
|
||||
})
|
||||
|
||||
async function getAllProfile() {
|
||||
try {
|
||||
const res = await funGetProfileByCookies()
|
||||
setData(res.data)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
useShallowEffect(() => {
|
||||
getAllProfile()
|
||||
}, [])
|
||||
|
||||
async function onEditProfile(val: boolean) {
|
||||
try {
|
||||
if (val) {
|
||||
const res = await funEditProfileByCookies({
|
||||
id: data.id,
|
||||
nik: data.nik,
|
||||
name: data.name,
|
||||
phone: data.phone,
|
||||
email: data.email,
|
||||
gender: data.gender,
|
||||
})
|
||||
if (res.success) {
|
||||
setValModal(false)
|
||||
toast.success(res.message)
|
||||
} else {
|
||||
toast.error(res.message)
|
||||
}
|
||||
}
|
||||
setValModal(false)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
toast.error("Gagal edit profil, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<LayoutNavbarNew back='' title='Edit Profil' menu='' />
|
||||
<Stack
|
||||
align="center"
|
||||
justify="center"
|
||||
gap="xs"
|
||||
pt={30}
|
||||
px={20}
|
||||
>
|
||||
<Box bg={WARNA.biruTua} py={30} px={50}
|
||||
style={{
|
||||
borderRadius: 10,
|
||||
}}>
|
||||
<HiUser size={100} color={WARNA.bgWhite} />
|
||||
</Box>
|
||||
<TextInput
|
||||
size="md" type="number" radius={30} placeholder="NIK" withAsterisk label="NIK" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
onChange={(e) => {
|
||||
setData({ ...data, nik: e.target.value })
|
||||
setTouched({ ...touched, nik: false })
|
||||
}}
|
||||
value={data.nik}
|
||||
onBlur={() => setTouched({ ...touched, nik: true })}
|
||||
error={
|
||||
touched.nik && (
|
||||
data.nik === "" ? "NIK Tidak Boleh Kosong" :
|
||||
data.nik.length !== 16 ? "NIK Harus 16 Karakter" : null
|
||||
)
|
||||
}
|
||||
/>
|
||||
<TextInput
|
||||
size="md" type="text" radius={30} placeholder="Nama" withAsterisk label="Nama" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
onChange={(e) => {
|
||||
setData({ ...data, name: e.target.value })
|
||||
setTouched({ ...touched, name: false })
|
||||
}}
|
||||
value={data.name}
|
||||
onBlur={() => setTouched({ ...touched, name: true })}
|
||||
error={
|
||||
touched.name && (
|
||||
data.name == "" ? "Nama Tidak Boleh Kosong" : null
|
||||
)
|
||||
}
|
||||
/>
|
||||
<TextInput
|
||||
size="md" type="email" radius={30} placeholder="Email" withAsterisk label="Email" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
onChange={(e) => {
|
||||
setData({ ...data, email: e.target.value })
|
||||
setTouched({ ...touched, email: false })
|
||||
}}
|
||||
value={data.email}
|
||||
onBlur={() => setTouched({ ...touched, email: true })}
|
||||
error={
|
||||
touched.email && (
|
||||
data.email == "" ? "Email Tidak Boleh Kosong" :
|
||||
!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(data.email) ? "Email tidak valid" : null
|
||||
)
|
||||
}
|
||||
/>
|
||||
<TextInput
|
||||
size="md" type="number" radius={30} placeholder="+62...." withAsterisk label="Nomor Telepon" w={"100%"}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
onChange={(e) => {
|
||||
setData({ ...data, phone: e.target.value })
|
||||
setTouched({ ...touched, phone: false })
|
||||
}}
|
||||
value={data.phone}
|
||||
onBlur={() => setTouched({ ...touched, phone: true })}
|
||||
error={
|
||||
touched.phone && (
|
||||
data.phone == "" ? "Nomor Telepon Tidak Boleh Kosong" : null
|
||||
)
|
||||
}
|
||||
/>
|
||||
<Select
|
||||
placeholder="Jenis Kelamin" label="Jenis Kelamin" w={"100%"} size="md" required withAsterisk radius={30}
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
data={
|
||||
[
|
||||
{
|
||||
value: "M",
|
||||
label: "Laki-laki"
|
||||
},
|
||||
{
|
||||
value: "F",
|
||||
label: "Perempuan"
|
||||
}
|
||||
]
|
||||
}
|
||||
onChange={(val: any) => {
|
||||
setData({ ...data, gender: val })
|
||||
setTouched({ ...touched, gender: false })
|
||||
}}
|
||||
value={data.gender}
|
||||
onBlur={() => setTouched({ ...touched, gender: true })}
|
||||
error={
|
||||
touched.gender && (
|
||||
data.gender == "" ? "Gender Tidak Boleh Kosong" : null
|
||||
)
|
||||
}
|
||||
/>
|
||||
</Stack>
|
||||
<Box mt={30} mx={20} pb={20}>
|
||||
<Button
|
||||
c={"white"}
|
||||
bg={WARNA.biruTua}
|
||||
size="md"
|
||||
radius={30}
|
||||
fullWidth
|
||||
onClick={() => {
|
||||
if (
|
||||
data.nik !== "" &&
|
||||
data.name !== "" &&
|
||||
data.email !== "" &&
|
||||
data.phone !== "" &&
|
||||
data.gender !== ""
|
||||
) {
|
||||
setValModal(true)
|
||||
} else {
|
||||
toast.error("Mohon lengkapi semua form");
|
||||
}
|
||||
}}
|
||||
>
|
||||
Simpan
|
||||
</Button>
|
||||
</Box>
|
||||
<LayoutModal opened={isValModal} onClose={() => setValModal(false)}
|
||||
description="Apakah Anda yakin ingin
|
||||
melakukan perubahan data?"
|
||||
onYes={(val) => { onEditProfile(val) }} />
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
135
src/module/user/profile/ui/profile.tsx
Normal file
135
src/module/user/profile/ui/profile.tsx
Normal file
@@ -0,0 +1,135 @@
|
||||
"use client"
|
||||
import { LayoutIconBack, LayoutNavbarHome, SkeletonDetailProfile, WARNA } from "@/module/_global";
|
||||
import { ActionIcon, Anchor, Box, Button, Flex, Group, Skeleton, Stack, Text } from "@mantine/core";
|
||||
import { BsInfo } from "react-icons/bs";
|
||||
import { HiUser } from "react-icons/hi2";
|
||||
import { RiIdCardFill } from "react-icons/ri";
|
||||
import { FaSquarePhone } from "react-icons/fa6";
|
||||
import { MdEmail } from "react-icons/md";
|
||||
import { InfoTitleProfile } from "../component/ui/ui_profile";
|
||||
import { IoMaleFemale } from "react-icons/io5";
|
||||
import toast from "react-hot-toast";
|
||||
import { LuLogOut } from "react-icons/lu";
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||
import { useState } from "react";
|
||||
import { funGetProfileByCookies } from "../lib/api_profile";
|
||||
import { useShallowEffect } from "@mantine/hooks";
|
||||
import { IProfileById } from "../lib/type_profile";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export default function Profile() {
|
||||
const [openModal, setOpenModal] = useState(false);
|
||||
const [isData, setData] = useState<IProfileById>()
|
||||
const router = useRouter()
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
async function getData() {
|
||||
try {
|
||||
setLoading(true)
|
||||
const res = await funGetProfileByCookies()
|
||||
setData(res.data)
|
||||
setLoading(false)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useShallowEffect(() => {
|
||||
getData()
|
||||
}, [])
|
||||
|
||||
async function onLogout(val: boolean) {
|
||||
try {
|
||||
if (val) {
|
||||
await fetch('/api/auth/logout', {
|
||||
method: 'DELETE',
|
||||
});
|
||||
toast.success('Logout Success')
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
setOpenModal(false)
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Box >
|
||||
<LayoutNavbarHome>
|
||||
<Group justify="space-between">
|
||||
<LayoutIconBack />
|
||||
|
||||
<ActionIcon onClick={() => { setOpenModal(true) }} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info">
|
||||
<LuLogOut size={20} color='white' />
|
||||
</ActionIcon>
|
||||
</Group>
|
||||
<Stack
|
||||
align="center"
|
||||
justify="center"
|
||||
gap="xs"
|
||||
>
|
||||
<HiUser size={100} color='white' />
|
||||
{loading ?
|
||||
<Skeleton height={62} mt={10} width={"40%"} />
|
||||
:
|
||||
<>
|
||||
<Text c={'white'} fw={'bold'} fz={25}>{isData?.name}</Text>
|
||||
<Text c={'white'} fw={'lighter'} fz={15}>{isData?.group} - {isData?.position}</Text>
|
||||
</>
|
||||
}
|
||||
</Stack>
|
||||
</LayoutNavbarHome>
|
||||
{loading
|
||||
?
|
||||
<SkeletonDetailProfile />
|
||||
:
|
||||
<Box p={20}>
|
||||
<Group justify="space-between" grow py={5}>
|
||||
<Text fw={'bold'} fz={20}>Informasi</Text>
|
||||
<Text style={{ cursor: 'pointer' }} ta={"right"} c={"blue"} onClick={() => router.push(`/profile/edit/`)}>Edit</Text>
|
||||
</Group>
|
||||
<Group justify="space-between" grow py={5}>
|
||||
<Group>
|
||||
<RiIdCardFill size={28} />
|
||||
<Text fz={18}>NIK</Text>
|
||||
</Group>
|
||||
<Text fz={18} fw={'bold'} ta={"right"}>{isData?.nik}</Text>
|
||||
</Group>
|
||||
<Group justify="space-between" grow py={5}>
|
||||
<Group>
|
||||
<FaSquarePhone size={28} />
|
||||
<Text fz={18}>No Telepon</Text>
|
||||
</Group>
|
||||
<Text fz={18} fw={'bold'} ta={"right"}>{isData?.phone}</Text>
|
||||
</Group>
|
||||
<Group justify="space-between" grow py={5}>
|
||||
<Group>
|
||||
<MdEmail size={28} />
|
||||
<Text fz={18}>Email</Text>
|
||||
</Group>
|
||||
<Text fz={18} fw={'bold'} ta={"right"}>{isData?.email}</Text>
|
||||
</Group>
|
||||
<Group justify="space-between" grow py={5}>
|
||||
<Group>
|
||||
<IoMaleFemale size={28} />
|
||||
<Text fz={18}>Gender</Text>
|
||||
</Group>
|
||||
<Text fz={18} fw={'bold'} ta={"right"}>
|
||||
{isData?.gender === 'M' ? 'Laki-laki' : isData?.gender === 'F' ? 'Perempuan' : ''}
|
||||
</Text>
|
||||
</Group>
|
||||
|
||||
</Box>
|
||||
}
|
||||
</Box>
|
||||
<LayoutModal opened={openModal} onClose={() => setOpenModal(false)}
|
||||
description="Apakah Anda yakin ingin Keluar?"
|
||||
onYes={(val) => onLogout(val)} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
import EditProfile from "../component/edit_profile";
|
||||
|
||||
export default function ViewEditProfile() {
|
||||
return (
|
||||
<EditProfile />
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user