@ -5,9 +5,10 @@ import { BASE } from "../Constants"
import Draggable from "react-draggable" ;
import Draggable from "react-draggable" ;
import { NULL_POS } from "../components/arrows/Pos" ;
import { NULL_POS } from "../components/arrows/Pos" ;
import { contains } from "../components/arrows/Box" ;
import { contains } from "../components/arrows/Box" ;
import { useRef , useState } from "react" ;
import React , { useRef , useState } from "react" ;
import { fetchAPI } from "../Fetcher"
import { fetchAPI } from "../Fetcher"
import { User } from "../model/User" ;
import { User } from "../model/User" ;
import { FaShare } from "react-icons/fa" ;
import { SaveStates } from "../components/editor/SavingState" ;
import { SaveStates } from "../components/editor/SavingState" ;
interface Tactic {
interface Tactic {
@ -83,7 +84,7 @@ function SideMenu({
teams : Team [ ]
teams : Team [ ]
} ) {
} ) {
return (
return (
< div id = "side M enu" style = { {
< div id = "side -m enu" style = { {
width : width + "%" ,
width : width + "%" ,
} } >
} } >
< div id = "side-menu-content" >
< div id = "side-menu-content" >
@ -153,7 +154,7 @@ function TableData({
i = 0
i = 0
while ( i < nbRow ) {
while ( i < nbRow ) {
listTactic [ i ] = listTactic [ i ] . map ( ( tactic : Tactic , i ) = > (
listTactic [ i ] = listTactic [ i ] . map ( ( tactic : Tactic , i ) = > (
< DraggableTableDataElement key = { i } tactic = { tactic } teams = { teams } user = { user } / >
< DraggableTableDataElement key = { i } tactic = { tactic } teams = { teams } / >
) )
) )
i ++
i ++
}
}
@ -175,23 +176,35 @@ function TableData({
function DraggableTableDataElement ( {
function DraggableTableDataElement ( {
tactic ,
tactic ,
teams ,
teams ,
user ,
} : {
} : {
tactic : Tactic
tactic : Tactic
teams : Team [ ]
teams : Team [ ]
user : User
} ) {
} ) {
const ref = useRef < HTMLDivElement > ( null )
const ref = useRef < HTMLDivElement > ( null )
const [ dragging , setDragging ] = useState ( false )
const [ dragging , setDragging ] = useState ( false )
const [ hovered , setHovered ] = useState ( false ) ;
const handleButtonClick = ( event : React.MouseEvent < HTMLButtonElement > ) = > {
event . stopPropagation ( ) ;
if ( ! dragging ) {
const userEmail = window . prompt ( "Entrez l'email à qui partager la tactique :" ) ;
if ( userEmail != null ) {
onShareTactic ( userEmail , tactic ) ;
}
} else {
setDragging ( false ) ;
}
} ;
return (
return (
< Draggable position = { NULL_POS }
< Draggable position = { NULL_POS }
nodeRef = { ref }
nodeRef = { ref }
onDrag = { ( ) = > setDragging ( true ) }
onDrag = { ( ) = > setDragging ( true ) }
onStop = { ( ) = > { if ( dragging ) {
onStop = { ( ) = > { if ( dragging ) {
onDropTactic ( ref . current . getBoundingClientRect ( ) , tactic , teams , user )
onDropTactic ( ref . current . getBoundingClientRect ( ) , tactic , teams )
}
}
} }
} }
>
>
< td key = { tactic . id }
< td key = { tactic . id }
ref = { ref }
ref = { ref }
className = "data"
className = "data"
@ -202,8 +215,18 @@ function DraggableTableDataElement({
setDragging ( false )
setDragging ( false )
}
}
} }
} }
>
onMouseEnter = { ( ) = > setHovered ( true ) }
onMouseLeave = { ( ) = > setHovered ( false ) }
>
{ truncateString ( tactic . name , 25 ) }
{ truncateString ( tactic . name , 25 ) }
{ hovered && (
< div className = "share-icon-container" >
< button className = "share-button share-icon-button" onClick = { handleButtonClick } >
< FaShare className = "share-icon" / >
< / button >
< / div >
) }
< / td >
< / td >
< / Draggable >
< / Draggable >
)
)
@ -316,22 +339,35 @@ function truncateString(name: string, limit: number): string {
return name
return name
}
}
function onDropTactic ( ref : DOMRect , tactic : Tactic , teams : Team [ ] , user : User ) {
function onDropTactic ( ref : DOMRect , tactic : Tactic , teams : Team [ ] ) {
let shared = false ;
let shared = false ;
for ( const team of teams ) {
for ( const team of teams ) {
if ( contains ( ref , document . getElementById ( ` button-team- ${ team . id } ` ) ! . getBoundingClientRect ( ) ) ) {
if ( contains ( ref , document . getElementById ( ` button-team- ${ team . id } ` ) ! . getBoundingClientRect ( ) ) ) {
if ( ! shared ) {
if ( ! shared ) {
shareTacticToTeam ( tactic , team , user );
shareTacticToTeam ( tactic , team );
shared = true ;
shared = true ;
}
}
}
}
}
}
}
}
async function shareTacticToTeam ( tactic : Tactic , team : Team , user : User ) {
async function onShareTactic ( email : string , tactic : Tactic ) {
const canShare = await fetchAPI ( ` tactic/ ${ tactic . id } /can-share-to-team ` , team ) . then ( ( r ) = > r . ok ) ;
const canShareResponse = await fetchAPI ( ` tactic/ ${ tactic . id } /can-share ` , tactic ) ;
if ( canShareResponse . ok ) {
const shareToAccountResponse = await fetchAPI ( ` tactic/ ${ tactic . id } /share-to-account ` , email ) ;
if ( ! shareToAccountResponse . ok ) {
alert ( "Une erreur s'est produite lors du partage de la tactique avec ce compte" ) ;
}
} else {
alert ( "Vous ne pouvez pas partager cette tactique" ) ;
}
}
async function shareTacticToTeam ( tactic : Tactic , team : Team ) {
const canShare = await fetchAPI ( ` tactic/ ${ tactic . id } /can-share-to-team ` , team ) . then ( ( r ) = > r . ok )
if ( canShare && confirm ( "Etes-vous sûr de vouloir partager la tactique " + tactic . name + " avec l'équipe " + team . name ) ) {
if ( canShare && confirm ( "Etes-vous sûr de vouloir partager la tactique " + tactic . name + " avec l'équipe " + team . name ) ) {
fetchAPI ( ` tactic/ ${ tactic . id } /share ` , team )
fetchAPI ( ` tactic/ ${ tactic . id } /share -to-team ` , team )
}
}
if ( ! canShare ) {
if ( ! canShare ) {
alert ( "Vous ne pouvez pas partager cette tactique à cette équipe" )
alert ( "Vous ne pouvez pas partager cette tactique à cette équipe" )