feat : update announcement
This commit is contained in:
31
api.http
31
api.http
@@ -232,18 +232,21 @@ POST http://localhost:3000/api/announcement HTTP/1.1
|
|||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{
|
{
|
||||||
"title": "cobaannouncement1 dsdsd",
|
"title": "test",
|
||||||
"desc": "coba announcement sdsdsd",
|
"desc": "test",
|
||||||
"groups": [
|
"groups":
|
||||||
{
|
[
|
||||||
"id": "group1",
|
{
|
||||||
"name": "Dinas",
|
"idAnnouncement": "1",
|
||||||
"Division": [
|
"idGroup": "group1",
|
||||||
{
|
"idDivision": "1",
|
||||||
"id": "1",
|
"isActive": true
|
||||||
"name": "sasasasa"
|
},
|
||||||
}
|
{
|
||||||
]
|
"idAnnouncement": "1",
|
||||||
}
|
"idGroup": "group1",
|
||||||
]
|
"idDivision": "clzknx2xy0001bt0wygcwjfph",
|
||||||
|
"isActive": true
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
@@ -49,14 +49,14 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
// const user = await funGetUserByCookies();
|
const user = await funGetUserByCookies();
|
||||||
// if (user.id == undefined) {
|
if (user.id == undefined) {
|
||||||
// return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||||
// }
|
}
|
||||||
|
|
||||||
const { title, desc, createBy, groups } = (await request.json());
|
const { title, desc, createBy, groups } = (await request.json());
|
||||||
const villaId = "desa1"
|
const villaId = user.idVillage
|
||||||
const roleId = "superAdminLukman"
|
const roleId = user.idUserRole
|
||||||
|
|
||||||
const data = await prisma.announcement.create({
|
const data = await prisma.announcement.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -74,21 +74,17 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const dataMember = groups.map((group: any) => ({
|
const dataMember = groups.map((group: any) => ({
|
||||||
idAnnoucement: data.id,
|
idAnnouncement: data.id,
|
||||||
idGroup: group.id,
|
idGroup: group.idGroup,
|
||||||
idDivision: group.Division.map((division: any) => ({
|
idDivision: group.idDivision,
|
||||||
id: division.id,
|
|
||||||
name: division.name,
|
|
||||||
})),
|
|
||||||
isActive: true,
|
isActive: true,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
console.log("test data", dataMember)
|
const announcementMember = await prisma.announcementMember.createMany({
|
||||||
// await prisma.announcementMember.createMany({
|
data: dataMember,
|
||||||
// data: dataMember
|
});
|
||||||
// })
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan pengumuman"}, { status: 200 });
|
return NextResponse.json({data, groups: announcementMember, success: true, message: "Berhasil mendapatkan pengumuman"}, { status: 200 });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { ICreateData } from "./type_announcement";
|
||||||
|
|
||||||
export const funGetAllAnnouncement = async (path?: string) => {
|
export const funGetAllAnnouncement = async (path?: string) => {
|
||||||
const response = await fetch(`/api/announcement${(path) ? path : ''}`, { next: { tags: ['announcement'] } });
|
const response = await fetch(`/api/announcement${(path) ? path : ''}`, { next: { tags: ['announcement'] } });
|
||||||
return await response.json().catch(() => null);
|
return await response.json().catch(() => null);
|
||||||
@@ -7,3 +9,14 @@ export const funGetAnnouncementById = async (path: string) => {
|
|||||||
const response = await fetch(`/api/announcement/${path}`);
|
const response = await fetch(`/api/announcement/${path}`);
|
||||||
return await response.json().catch(() => null);
|
return await response.json().catch(() => null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const funCreateAnnouncement = async (data: ICreateData) => {
|
||||||
|
const response = await fetch("/api/announcement", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
return await response.json().catch(() => null);
|
||||||
|
}
|
||||||
@@ -1,33 +1,46 @@
|
|||||||
export interface IListDataAnnouncement {
|
export interface IListDataAnnouncement {
|
||||||
id: string,
|
id: string,
|
||||||
title: string,
|
title: string,
|
||||||
desc: string,
|
desc: string,
|
||||||
createdAt: string
|
createdAt: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IRootAllAnnouncement {
|
export interface IRootAllAnnouncement {
|
||||||
announcement: IAnnouncement
|
announcement: IAnnouncement
|
||||||
allAnnouncementMember: IAllAnnouncementMember[]
|
allAnnouncementMember: IAllAnnouncementMember[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IAnnouncement {
|
export interface IAnnouncement {
|
||||||
id: string
|
id: string
|
||||||
title: string
|
title: string
|
||||||
desc: string
|
desc: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IAllAnnouncementMember {
|
export interface IAllAnnouncementMember {
|
||||||
idAnnouncement: string
|
idAnnouncement: string
|
||||||
idGroup: string
|
idGroup: string
|
||||||
idDivision: string
|
idDivision: string
|
||||||
group: string
|
group: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GroupData {
|
export interface GroupData {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
Division: {
|
Division: {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
}[];
|
}[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ICreateData {
|
||||||
|
title: string
|
||||||
|
desc: string
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGroupData {
|
||||||
|
idAnnouncement: string
|
||||||
|
idGroup: string
|
||||||
|
idDivision: string
|
||||||
|
isActive: boolean
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,20 +3,47 @@ import { LayoutNavbarNew, WARNA } from "@/module/_global";
|
|||||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||||
import { globalMemberDivision } from "@/module/division_new/lib/val_division";
|
import { globalMemberDivision } from "@/module/division_new/lib/val_division";
|
||||||
import { useHookstate } from "@hookstate/core";
|
import { useHookstate } from "@hookstate/core";
|
||||||
import { Box, Button, Group, Stack, Text, Textarea, TextInput } from "@mantine/core";
|
import { Avatar, Box, Button, Flex, Group, Stack, Text, Textarea, TextInput } from "@mantine/core";
|
||||||
import { useShallowEffect } from "@mantine/hooks";
|
import { useShallowEffect } from "@mantine/hooks";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { HiOutlineChevronRight } from "react-icons/hi2";
|
import { HiOutlineChevronRight } from "react-icons/hi2";
|
||||||
import { IoIosArrowForward } from "react-icons/io";
|
import { IoIosArrowForward } from "react-icons/io";
|
||||||
|
import CreateUsersAnnouncement from "./create_users_announcement";
|
||||||
|
import { globalMemberAnnouncement } from "../lib/val_announcement";
|
||||||
|
import { funCreateAnnouncement } from "../lib/api_announcement";
|
||||||
|
import { group } from "console";
|
||||||
|
import { GroupData, ICreateData, IGroupData } from "../lib/type_announcement";
|
||||||
|
|
||||||
|
export type DataMember = DataGroup[]
|
||||||
|
|
||||||
|
export interface DataGroup {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
Division: Division[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Division {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export default function CreateAnnouncement() {
|
export default function CreateAnnouncement() {
|
||||||
const [isOpen, setOpen] = useState(false)
|
const [isOpen, setOpen] = useState(false)
|
||||||
const member = useHookstate(globalMemberDivision)
|
const memberGroup = useHookstate(globalMemberAnnouncement)
|
||||||
const router = useRouter()
|
const memberValue = memberGroup.get() as GroupData[]
|
||||||
const [selectedFiles, setSelectedFiles] = useState<any>([]);
|
const [selectedFiles, setSelectedFiles] = useState<any>([]);
|
||||||
|
|
||||||
|
const [isChooseMember, setIsChooseMember] = useState(false)
|
||||||
|
const [isData, setisData] = useState({
|
||||||
|
title: "",
|
||||||
|
desc: "",
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function onTrue(val: boolean) {
|
function onTrue(val: boolean) {
|
||||||
if (val) {
|
if (val) {
|
||||||
toast.success("Sukses! Data tersimpan");
|
toast.success("Sukses! Data tersimpan");
|
||||||
@@ -24,20 +51,38 @@ export default function CreateAnnouncement() {
|
|||||||
setOpen(false)
|
setOpen(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function onSubmit(val: boolean) {
|
||||||
|
// const response = await funCreateAnnouncement(
|
||||||
|
// {
|
||||||
|
// title: isData.title,
|
||||||
|
// desc: isData.desc,
|
||||||
|
// groups: memberValue
|
||||||
|
// },
|
||||||
|
|
||||||
|
// )
|
||||||
|
setOpen(false)
|
||||||
|
console.log(isData)
|
||||||
|
console.log(memberValue)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
setSelectedFiles(JSON.parse(JSON.stringify(member.get())))
|
setSelectedFiles(JSON.parse(JSON.stringify(memberGroup.get())))
|
||||||
}
|
}
|
||||||
|
|
||||||
useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
loadData()
|
loadData()
|
||||||
},[])
|
}, [])
|
||||||
|
|
||||||
|
function onToChooseMember() {
|
||||||
|
setIsChooseMember(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isChooseMember) return <CreateUsersAnnouncement onClose={() => { setIsChooseMember(false) }} />
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<pre>
|
<LayoutNavbarNew back="" title="Tambah Pengumuman" menu={<></>} />
|
||||||
{JSON.stringify(selectedFiles)}
|
|
||||||
</pre>
|
|
||||||
<LayoutNavbarNew back="" title="Tambah Pengumuman" menu={<></>} />
|
|
||||||
<Stack
|
<Stack
|
||||||
p={20}
|
p={20}
|
||||||
>
|
>
|
||||||
@@ -50,6 +95,7 @@ export default function CreateAnnouncement() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
onChange={(e) => { setisData({ ...isData, title: e.target.value }) }}
|
||||||
/>
|
/>
|
||||||
<Textarea
|
<Textarea
|
||||||
size="md"
|
size="md"
|
||||||
@@ -65,6 +111,7 @@ export default function CreateAnnouncement() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
onChange={(e) => { setisData({ ...isData, desc: e.target.value }) }}
|
||||||
/>
|
/>
|
||||||
<Box pt={10}>
|
<Box pt={10}>
|
||||||
<Group justify="space-between" style={{
|
<Group justify="space-between" style={{
|
||||||
@@ -72,7 +119,7 @@ export default function CreateAnnouncement() {
|
|||||||
padding: 10,
|
padding: 10,
|
||||||
borderRadius: 10
|
borderRadius: 10
|
||||||
}}
|
}}
|
||||||
onClick={() => router.push("/announcement/create-user")}
|
onClick={() => { onToChooseMember() }}
|
||||||
>
|
>
|
||||||
<Text size="sm">
|
<Text size="sm">
|
||||||
Tambah Anggota
|
Tambah Anggota
|
||||||
@@ -80,6 +127,25 @@ export default function CreateAnnouncement() {
|
|||||||
<IoIosArrowForward />
|
<IoIosArrowForward />
|
||||||
</Group>
|
</Group>
|
||||||
</Box>
|
</Box>
|
||||||
|
<Box pt={20}>
|
||||||
|
<Text c={WARNA.biruTua} mb={10}>Anggota Terpilih</Text>
|
||||||
|
{(memberGroup.length === 0) ? (
|
||||||
|
<Text c="dimmed" ta={"center"} fs={"italic"}>Belum ada anggota</Text>
|
||||||
|
) : memberGroup.get().map((v: any, i: any) => {
|
||||||
|
return (
|
||||||
|
<Box key={i} mt={10}>
|
||||||
|
<Text fw={"bold"}>{v.name}</Text>
|
||||||
|
<Box pl={20}>
|
||||||
|
<Flex direction={"column"} gap={"md"}>
|
||||||
|
{v.Division.map((division: any) => (
|
||||||
|
<li key={division.id}>{division.name}</li>
|
||||||
|
))}
|
||||||
|
</Flex>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Box mt={30} mx={20}>
|
<Box mt={30} mx={20}>
|
||||||
<Button
|
<Button
|
||||||
@@ -95,7 +161,7 @@ export default function CreateAnnouncement() {
|
|||||||
</Box>
|
</Box>
|
||||||
<LayoutModal opened={isOpen} onClose={() => setOpen(false)}
|
<LayoutModal opened={isOpen} onClose={() => setOpen(false)}
|
||||||
description="Apakah Anda yakin ingin menambahkan data?"
|
description="Apakah Anda yakin ingin menambahkan data?"
|
||||||
onYes={(val) => { onTrue(val) }} />
|
onYes={(val) => { onSubmit(val) }} />
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ interface CheckedState {
|
|||||||
[key: string]: string[];
|
[key: string]: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CreateUsersAnnouncement() {
|
export default function CreateUsersAnnouncement({ onClose}: { onClose: (val: any) => void }) {
|
||||||
const [checked, setChecked] = useState<CheckedState>({});
|
const [checked, setChecked] = useState<CheckedState>({});
|
||||||
const [selectAll, setSelectAll] = useState(false);
|
const [selectAll, setSelectAll] = useState(false);
|
||||||
const [isData, setIsData] = useState<GroupData[]>([])
|
const [isData, setIsData] = useState<GroupData[]>([])
|
||||||
@@ -69,11 +69,17 @@ export default function CreateUsersAnnouncement() {
|
|||||||
const handleSubmit = () => {
|
const handleSubmit = () => {
|
||||||
const selectedGroups: GroupData[] = [];
|
const selectedGroups: GroupData[] = [];
|
||||||
Object.keys(checked).forEach((groupId) => {
|
Object.keys(checked).forEach((groupId) => {
|
||||||
if (checked[groupId]) {
|
const group = isData.find((item) => item.id === groupId);
|
||||||
selectedGroups.push();
|
if (group) {
|
||||||
|
selectedGroups.push({
|
||||||
|
id: group.id,
|
||||||
|
name: group.name,
|
||||||
|
Division: group.Division.filter((division) => checked[groupId].includes(division.id)),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
memberGroup.set(selectedGroups);
|
memberGroup.set(selectedGroups);
|
||||||
|
onClose(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user