upd: tambah anggota task
Deskripsi: -tambah anggota task NO ISsues
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { AddMemberDetailTask } from "@/module/task"
|
||||
|
||||
function Page() {
|
||||
return (
|
||||
<AddMemberDetailTask />
|
||||
)
|
||||
}
|
||||
|
||||
export default Page
|
||||
61
src/app/api/task/[id]/member/route.ts
Normal file
61
src/app/api/task/[id]/member/route.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
// ADD MEMBER TASK DIVISI
|
||||
export async function POST(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { id } = context.params;
|
||||
const { member, idDivision } = (await request.json());
|
||||
|
||||
const data = await prisma.divisionProject.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
console.log(member, idDivision)
|
||||
|
||||
if (data == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Tambah anggota tugas gagal, data tugas tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (member.length > 0) {
|
||||
const dataMember = member.map((v: any) => ({
|
||||
..._.omit(v, ["idUser", "name"]),
|
||||
idDivision: idDivision,
|
||||
idProject: id,
|
||||
idUser: v.idUser,
|
||||
}))
|
||||
|
||||
const insertMember = await prisma.divisionProjectMember.createMany({
|
||||
data: dataMember
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil menambahkan anggota tugas",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal menambah anggota tugas, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -108,6 +108,7 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
idUser: true,
|
||||
User: {
|
||||
select: {
|
||||
name: true,
|
||||
@@ -185,4 +186,5 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
console.log(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mengedit detail tugas, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AddDetailTask from "./ui/add_detail_task";
|
||||
import AddMemberDetailTask from "./ui/add_member_detail_task";
|
||||
import ViewDateEndTask from "./ui/create_date_end_task";
|
||||
import CreateTask from "./ui/create_task";
|
||||
import CreateUsersProject from "./ui/create_users_project";
|
||||
@@ -24,4 +25,5 @@ export { ProgressDetailTask }
|
||||
export { ListFileDetailTask }
|
||||
export { ListAnggotaDetailTask }
|
||||
export { EditDetailTask }
|
||||
export { AddDetailTask }
|
||||
export { AddDetailTask }
|
||||
export { AddMemberDetailTask }
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IFormAddDetailTask, IFormDateTask, IFormTaskDivision } from "./type_task";
|
||||
import { IFormAddDetailTask, IFormAddMemberTask, IFormDateTask, IFormTaskDivision } from "./type_task";
|
||||
|
||||
export const funGetAllTask = async (path?: string) => {
|
||||
const response = await fetch(`/api/task${(path) ? path : ''}`, { next: { tags: ['task'] } });
|
||||
@@ -76,4 +76,15 @@ export const funCreateDetailTask = async (path: string, data: IFormAddDetailTask
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await response.json().catch(() => null);
|
||||
};
|
||||
|
||||
export const funAddMemberTask = async (path: string, data: IFormAddMemberTask) => {
|
||||
const response = await fetch(`/api/task/${path}/member`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await response.json().catch(() => null);
|
||||
};
|
||||
@@ -27,6 +27,12 @@ export interface IFormAddDetailTask {
|
||||
}
|
||||
|
||||
|
||||
export interface IFormAddMemberTask {
|
||||
idDivision: string
|
||||
member: IFormMemberTask[] | []
|
||||
}
|
||||
|
||||
|
||||
export interface IFormTaskDivision {
|
||||
idDivision: string
|
||||
title: string
|
||||
@@ -51,6 +57,7 @@ export interface IDataListTaskDivision {
|
||||
|
||||
export interface IDataMemberTaskDivision {
|
||||
id: string
|
||||
idUser: string
|
||||
name: string
|
||||
email: string
|
||||
}
|
||||
|
||||
203
src/module/task/ui/add_member_detail_task.tsx
Normal file
203
src/module/task/ui/add_member_detail_task.tsx
Normal file
@@ -0,0 +1,203 @@
|
||||
"use client"
|
||||
import { LayoutNavbarNew, WARNA } from "@/module/_global";
|
||||
import { funGetDivisionById, IDataMemberDivision } from "@/module/division_new";
|
||||
import {
|
||||
Anchor,
|
||||
Avatar,
|
||||
Box,
|
||||
Button,
|
||||
Checkbox,
|
||||
Divider,
|
||||
Flex,
|
||||
Group,
|
||||
Stack,
|
||||
Text,
|
||||
TextInput,
|
||||
} from "@mantine/core";
|
||||
import { useShallowEffect } from "@mantine/hooks";
|
||||
import { useParams, useRouter } from "next/navigation";
|
||||
import React, { useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { FaCheck } from "react-icons/fa6";
|
||||
import { funAddMemberTask, funGetTaskDivisionById } from "../lib/api_task";
|
||||
import { IDataMemberTaskDivision } from "../lib/type_task";
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||
|
||||
export default function AddMemberDetailTask() {
|
||||
const router = useRouter()
|
||||
const param = useParams<{ id: string, detail: string }>()
|
||||
const [selectedFiles, setSelectedFiles] = useState<any>([])
|
||||
const [isData, setData] = useState<IDataMemberDivision[]>([])
|
||||
const [isDataMember, setDataMember] = useState<IDataMemberTaskDivision[]>([])
|
||||
const [selectAll, setSelectAll] = useState(false)
|
||||
const [openModal, setOpenModal] = useState(false)
|
||||
|
||||
|
||||
async function getData() {
|
||||
try {
|
||||
const response = await funGetDivisionById(param.id)
|
||||
if (response.success) {
|
||||
setData(response.data.member)
|
||||
} else {
|
||||
toast.error(response.message)
|
||||
}
|
||||
|
||||
const res = await funGetTaskDivisionById(param.detail, 'member');
|
||||
if (res.success) {
|
||||
setDataMember(res.data)
|
||||
} else {
|
||||
toast.error(res.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
toast.error("Gagal mendapatkan anggota, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
useShallowEffect(() => {
|
||||
getData()
|
||||
}, []);
|
||||
|
||||
const handleFileClick = (index: number) => {
|
||||
if (selectedFiles.some((i: any) => i.idUser == isData[index].idUser)) {
|
||||
setSelectedFiles(selectedFiles.filter((i: any) => i.idUser != isData[index].idUser))
|
||||
} else {
|
||||
setSelectedFiles([...selectedFiles, { idUser: isData[index].idUser, name: isData[index].name }])
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
const handleSelectAll = () => {
|
||||
setSelectAll(!selectAll);
|
||||
if (!selectAll) {
|
||||
for (let index = 0; index < isData.length; index++) {
|
||||
if (!isDataMember.some((i: any) => i.idUser == isData[index].idUser)) {
|
||||
if (!selectedFiles.some((i: any) => i.idUser == isData[index].idUser)) {
|
||||
const newArr = {
|
||||
idUser: isData[index].idUser, name: isData[index].name
|
||||
}
|
||||
setSelectedFiles((selectedFiles: any) => [...selectedFiles, newArr])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
setSelectedFiles([]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function onVerifikasi() {
|
||||
if (selectedFiles.length == 0) {
|
||||
return toast.error("Error! silahkan pilih anggota")
|
||||
}
|
||||
|
||||
setOpenModal(true)
|
||||
}
|
||||
|
||||
async function onSubmit() {
|
||||
try {
|
||||
const res = await funAddMemberTask(param.detail, { idDivision: param.id, member: selectedFiles });
|
||||
if (res.success) {
|
||||
toast.success(res.message)
|
||||
router.back()
|
||||
} else {
|
||||
toast.error(res.message)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
toast.error("Gagal menambahkan anggota, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<LayoutNavbarNew
|
||||
back=""
|
||||
title="Pilih Anggota"
|
||||
menu
|
||||
/>
|
||||
<Box p={20}>
|
||||
{/* <TextInput
|
||||
styles={{
|
||||
input: {
|
||||
color: WARNA.biruTua,
|
||||
borderRadius: WARNA.biruTua,
|
||||
borderColor: WARNA.biruTua,
|
||||
},
|
||||
}}
|
||||
size="md"
|
||||
radius={30}
|
||||
leftSection={<HiMagnifyingGlass size={20} />}
|
||||
placeholder="Pencarian"
|
||||
/> */}
|
||||
<Group justify="space-between" mt={20} onClick={handleSelectAll}>
|
||||
<Text c={WARNA.biruTua} fw={"bold"}>
|
||||
Pilih Semua Anggota
|
||||
</Text>
|
||||
{selectAll ? <FaCheck style={{ marginRight: 10 }} /> : ""}
|
||||
</Group>
|
||||
<Box mt={15}>
|
||||
{isData.map((v, i) => {
|
||||
const isSelected = selectedFiles.some((i: any) => i?.idUser == v.idUser);
|
||||
const found = isDataMember.some((i: any) => i.idUser == v.idUser)
|
||||
return (
|
||||
<Box mb={15} key={i} onClick={() => (!found) ? handleFileClick(i) : null}>
|
||||
<Flex justify={"space-between"} align={"center"}>
|
||||
<Group>
|
||||
<Avatar src={"v.image"} alt="it's me" size="lg" />
|
||||
<Stack align="flex-start" justify="flex-start">
|
||||
<Text style={{
|
||||
cursor: 'pointer',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
}}>
|
||||
{v.name}
|
||||
</Text>
|
||||
<Text c={"dimmed"}>{(found) ? "sudah menjadi anggota" : ""}</Text>
|
||||
</Stack>
|
||||
</Group>
|
||||
<Text
|
||||
style={{
|
||||
cursor: 'pointer',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
paddingLeft: 20,
|
||||
}}
|
||||
>
|
||||
{isSelected ? <FaCheck style={{ marginRight: 10 }} /> : ""}
|
||||
</Text>
|
||||
</Flex>
|
||||
<Divider my={"md"} />
|
||||
</Box>
|
||||
);
|
||||
})}
|
||||
</Box>
|
||||
<Box mt={"xl"}>
|
||||
<Button
|
||||
c={"white"}
|
||||
bg={WARNA.biruTua}
|
||||
size="lg"
|
||||
radius={30}
|
||||
fullWidth
|
||||
onClick={() => { onVerifikasi() }}
|
||||
>
|
||||
Simpan
|
||||
</Button>
|
||||
</Box>
|
||||
</Box>
|
||||
|
||||
<LayoutModal opened={openModal} onClose={() => setOpenModal(false)}
|
||||
description="Apakah Anda yakin ingin menambahkan anggota?"
|
||||
onYes={(val) => {
|
||||
if (val) {
|
||||
onSubmit()
|
||||
}
|
||||
setOpenModal(false)
|
||||
}} />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -52,7 +52,6 @@ export default function CreateTask() {
|
||||
|
||||
async function onSubmit() {
|
||||
try {
|
||||
console.log("kirim",fileForm)
|
||||
const response = await funCreateTask({ idDivision: param.id, title, task: dataTask, file: fileForm, member: memberValue })
|
||||
|
||||
if (response.success) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { useShallowEffect } from "@mantine/hooks";
|
||||
import { HiMenu } from "react-icons/hi";
|
||||
import { IoAddCircle } from "react-icons/io5";
|
||||
import { RiFilter2Line } from "react-icons/ri";
|
||||
import { FaUsers } from "react-icons/fa6";
|
||||
|
||||
export default function NavbarDetailDivisionTask() {
|
||||
const router = useRouter()
|
||||
@@ -63,7 +64,7 @@ export default function NavbarDetailDivisionTask() {
|
||||
cursor: 'pointer'
|
||||
}}
|
||||
onClick={() => {
|
||||
router.push(param.detail + '/create-task')
|
||||
router.push(param.detail + '/add-task')
|
||||
}}
|
||||
>
|
||||
<Box>
|
||||
@@ -79,14 +80,14 @@ export default function NavbarDetailDivisionTask() {
|
||||
cursor: 'pointer'
|
||||
}}
|
||||
onClick={() => {
|
||||
router.push('/announcement?page=filter')
|
||||
router.push(param.detail + '/add-member')
|
||||
}}
|
||||
>
|
||||
<Box>
|
||||
<RiFilter2Line size={30} color={WARNA.biruTua} />
|
||||
<FaUsers size={30} color={WARNA.biruTua} />
|
||||
</Box>
|
||||
<Box>
|
||||
<Text c={WARNA.biruTua} ta='center'>Filter</Text>
|
||||
<Text c={WARNA.biruTua} ta='center'>Tambah anggota</Text>
|
||||
</Box>
|
||||
</Flex>
|
||||
</SimpleGrid>
|
||||
|
||||
Reference in New Issue
Block a user