diff --git a/src/app/(dashboard)/assign_zone_project/AssignProject.jsx b/src/app/(dashboard)/assign_zone_project/AssignProject.jsx
new file mode 100644
index 0000000000000000000000000000000000000000..60fd26de09e100d0897da893b01156694f9546a5
--- /dev/null
+++ b/src/app/(dashboard)/assign_zone_project/AssignProject.jsx
@@ -0,0 +1,358 @@
+"use client"
+import React, { useState, useEffect, useRef } from 'react'
+import Loader from '@/components/Loader/Loader'
+import { useNotification } from '@/context/NotificationContext'
+import CancelIcon from "@/static/image/svg/cancel.svg"
+import UserIcon from "@/static/image/svg/user.svg"
+import DeskIcon from "@/static/image/svg/study-desk.svg"
+import AddIcon from "@/static/image/svg/add.svg"
+import fetchRequest from "@/app/lib/fetchRequest";
+
+
+const AssignProject = ({ setIsOpen, listProjects }) => {
+ const [loading, setLoading] = useState(false)
+ const [projects, setProjects] = useState([])
+ const [zones, setZones] = useState([])
+ const [ selectedDay, setSelectedDay ] = useState('')
+ const [ selectedWeek, setSelectedWeek ] = useState('')
+ const [ selectedZone, setSelectedZone ] = useState(null)
+ const [ selectedProject, setSelectedProject ] = useState(null)
+ const [ places , setPlaces ] = useState([])
+ const [ nbrCollabs, setNbrCollabs ] = useState(0)
+ const [ collabsAttributed, setCollabsAttributed ] = useState(0)
+ const [ selectedOtherZone, setSelectedOtherZone ] = useState(null)
+ const [ otherPlaces, setOtherPlaces ] = useState([])
+
+ const { toggleNotification } = useNotification()
+
+ const attributedCollabsRef = useRef()
+
+ useEffect(() => {
+ const fetchProjectsandZones = async () => {
+ setLoading(true)
+ try {
+ const {isSuccess, errors, data} = await fetchRequest(`/zoaning/affectingProject/${selectedWeek}/${selectedDay}`, {method: 'GET'})
+ if(isSuccess){
+ setCollabsAttributed(0)
+ setNbrCollabs(0)
+ setPlaces([])
+ if ( (data.projects && data.projects.length === 0) && (data.zones && data.zones.length === 0)){
+ toggleNotification({
+ visible: true,
+ message: "Il y'a pas de projets et de zones pour cette semaine et ce jour.",
+ type: "warning"
+ })
+ setProjects([])
+ setZones([])
+ }else{
+ if(data.projects && data.projects.length === 0){
+ toggleNotification({
+ visible: true,
+ message: "Il y'a pas de projets pour cette semaine et ce jour.",
+ type: "warning"
+ })
+ setProjects([])
+ }else{
+ setProjects(data.projects)
+ }
+ if(data.zones && data.zones.length === 0){
+ toggleNotification({
+ visible: true,
+ message: "Il y'a pas de zones pour cette semaine et ce jour.",
+ type: "warning"
+ })
+ setZones([])
+ }else{
+ setZones(data.zones)
+ }
+ }
+ }else{
+ // handle error
+ setLoading(false)
+ }
+ } catch (error) {
+ console.log(error)
+ toggleNotification({ title: 'Erreur', content: error.message, type: 'error' })
+ } finally {
+ setLoading(false)
+ }
+ }
+ if(selectedDay && selectedWeek) fetchProjectsandZones()
+ }, [selectedDay, selectedWeek])
+
+ const handleZoneSelection = async (e) => {
+ const zone_id = e.target.value
+ try{
+ const { isSuccess, errors, data } = await fetchRequest(`/zoaning/countingPlaces/${zone_id}`, {method: 'GET'})
+ if(isSuccess){
+ setSelectedZone(zone_id)
+ setPlaces(data.places)
+ setOtherPlaces([])
+ setSelectedOtherZone(null)
+ }else{
+ // handle error
+ setPlaces([])
+ }
+ }catch(error){
+ console.log(error)
+ }
+ }
+
+ const handleProjectSelection = async (e) => {
+ const project_id = e.target.value
+ try{
+ const { isSuccess, errors, data } = await fetchRequest(`/zoaning/countingCollabs/${project_id}`, {method: 'GET'})
+ if(isSuccess){
+ setSelectedProject(project_id)
+ setNbrCollabs(data.user_count)
+ setOtherPlaces([])
+ setSelectedOtherZone(null)
+ }else{
+ // handle error
+ setNbrCollabs(0)
+ }
+ }catch(error){
+ console.log(error)
+ }
+ }
+
+ useEffect( () => {
+ if(nbrCollabs > 0 && places.length > 0){
+ if( nbrCollabs <= places.length){
+ setCollabsAttributed(nbrCollabs)
+ attributedCollabsRef.current = nbrCollabs
+ }else{
+ setCollabsAttributed(places.length)
+ attributedCollabsRef.current = places.length
+ }
+ }
+ }, [nbrCollabs, places])
+
+
+ const handleAddCollab = () =>{
+ setCollabsAttributed(collabsAttributed + 1)
+ attributedCollabsRef.current = collabsAttributed + 1
+ }
+
+ const handleMinusCollab = () =>{
+ setCollabsAttributed(collabsAttributed - 1)
+ attributedCollabsRef.current = collabsAttributed - 1
+ }
+
+
+
+
+ const handleOtherZoneSelection = async (e) => {
+ const zone_id = e.target.value
+ setSelectedOtherZone(zone_id)
+ try{
+ const { isSuccess, errors, data } = await fetchRequest(`/zoaning/countingPlaces/${zone_id}`, {method: 'GET'})
+ if(isSuccess){
+ setOtherPlaces(data.places)
+ }else{
+ // handle error
+ }
+ }catch(error){
+ console.log(error)
+ toggleNotification({
+ visible: true,
+ message: "Internal Server Error",
+ type: "error"
+ })
+ }
+ }
+
+ useEffect( () => {
+ if( (collabsAttributed - places.length) <= 0 && selectedOtherZone ) {
+ setSelectedOtherZone(null)
+ setOtherPlaces([])
+ }
+ }, [collabsAttributed])
+
+
+ const handleAssignProject = async () => {
+ const finalData = {
+ id_zone: selectedZone,
+ id_project: selectedProject,
+ jour: selectedDay,
+ semaine: selectedWeek,
+ nombre_personnes: collabsAttributed,
+ places_disponibles: (collabsAttributed > places.length) ? 0 : places.length - collabsAttributed,
+ places_occuper: (collabsAttributed > places.length) ? places.length : collabsAttributed,
+ places: (collabsAttributed > places.length) ? places.map( (element) => element.id) : places.map( (element, index) => index < collabsAttributed && element.id).filter(id => id !== false)
+ }
+ if( selectedOtherZone && otherPlaces.length > 0){
+ finalData.otherZone = {
+ id_zone: selectedOtherZone,
+ places_disponibles: ( (collabsAttributed - places.length ) > otherPlaces.length ) ? 0 : otherPlaces.length - (collabsAttributed - places.length),
+ places_occuper: ( (collabsAttributed - places.length ) > otherPlaces.length ) ? otherPlaces.length : collabsAttributed - places.length,
+ places: ( (collabsAttributed - places.length ) > otherPlaces.length ) ? otherPlaces.map( (element) => element.id) : otherPlaces.map( (element, index) => (index < (collabsAttributed - places.length)) && element.id).filter(id => id !== false)
+ }
+ }
+ try{
+ const { isSuccess, errors, data } = await fetchRequest(`/zoaning/affectingProject`, {method: 'POST', body: JSON.stringify(finalData)})
+ if(isSuccess){
+ listProjects( (prevListProjects) => [...prevListProjects, data.main_zone, ...(data.second_zone ? [data.second_zone] : [])])
+ toggleNotification({
+ visible: true,
+ message: "Projet affecté avec succès.",
+ type: "success"
+ })
+ closePopup(false)
+ }else{
+ console.log(errors)
+ toggleNotification({
+ visible: true,
+ message: "Erreur lors de l'affectation du projet",
+ type: "error"
+ })
+ }
+ }catch(error){
+ console.log(error)
+ toggleNotification({
+ visible: true,
+ message: "Internal Server Error",
+ type: "error"
+ })
+ }
+
+ }
+
+ const closePopup = () => {
+ setIsOpen()
+ setSelectedWeek('')
+ setSelectedDay('')
+ setSelectedProject(null)
+ setSelectedZone(null)
+ setCollabsAttributed(0)
+ setPlaces([])
+ setNbrCollabs(0)
+ setSelectedOtherZone(null)
+ setOtherPlaces([])
+ setProjects([])
+ setZones([])
+ setLoading(false)
+
+
+ }
+
+
+ console.log("other zone ::::" , selectedOtherZone)
+ return (
+
|
setNumPlace(event.target.value)} defaultValue={numero} type='text' className='disabled:bg-white border-0 rounded-md px-2 enabled:drop-shadow border-none enabled:bg-gray-100 duration-100 h-10 outline-none' />
|
|