1010 *
1111 * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
1212 ********************************************************************************/
13- import React , { useContext , useState } from "react" ;
14- import { Trash2 } from "react-feather" ;
13+ import React , { useContext , useRef } from "react" ;
1514import ediTDorContext from "../../../context/ediTDorContext" ;
1615import {
1716 buildAttributeListObject ,
@@ -22,102 +21,103 @@ import InfoIconWrapper from "../../base/InfoIconWrapper";
2221import { getFormsTooltipContent } from "../../../utils/TooltipMapper" ;
2322import Form from "./Form" ;
2423import AddFormElement from "../base/AddFormElement" ;
25-
24+ import AffordanceButtons from "./AffordanceButtons" ;
25+ import type { IInteractionAffordance } from "../../../types/form" ;
26+ import { useCopiedAffordanceFocus } from "../../../hooks/useCopiedAffordanceFocus" ;
2627const alreadyRenderedKeys = [ "title" , "forms" , "description" ] ;
2728
28- const Action : React . FC < any > = ( props ) => {
29+ interface IAction {
30+ action : IInteractionAffordance ;
31+ actionName : string ;
32+ copiedToken ?: number ;
33+ onCopy : ( ) => void ;
34+ }
35+ const Action : React . FC < IAction > = ( {
36+ action,
37+ actionName,
38+ copiedToken,
39+ onCopy,
40+ } ) => {
2941 const context = useContext ( ediTDorContext ) ;
30-
31- const [ isExpanded , setIsExpanded ] = useState ( false ) ;
32-
33- const addFormDialog = React . useRef ( ) ;
34- const handleOpenAddFormDialog = ( ) => {
35- addFormDialog . current . openModal ( ) ;
36- } ;
37-
38- if (
39- Object . keys ( props . action ) . length === 0 &&
40- props . action . constructor !== Object
41- ) {
42- return (
43- < div className = "text-3xl text-white" >
44- Action could not be rendered because mandatory fields are missing.
45- </ div >
46- ) ;
47- }
48-
49- const action = props . action ;
50- const forms = separateForms ( props . action . forms ) ;
51-
42+ const addFormDialog = useRef < { openModal : ( ) => void ; close : ( ) => void } > (
43+ null
44+ ) ;
45+ const { containerRef, isExpanded, isHighlighted, setIsExpanded } =
46+ useCopiedAffordanceFocus ( { copiedToken } ) ;
47+ const forms = separateForms ( action . forms ) ;
5248 const attributeListObject = buildAttributeListObject (
53- { name : props . actionName } ,
54- props . action ,
49+ { name : actionName } ,
50+ action ,
5551 alreadyRenderedKeys
5652 ) ;
57- const attributes = Object . keys ( attributeListObject ) . map ( ( x ) => {
58- return (
59- < li key = { x } >
60- { x } : { JSON . stringify ( attributeListObject [ x ] ) }
61- </ li >
62- ) ;
63- } ) ;
64-
65- const handleDeleteAction = ( ) => {
66- context . removeOneOfAKindReducer ( "actions" , props . actionName ) ;
53+ const handleDelete = ( ) => {
54+ context . removeOneOfAKindReducer ( "actions" , actionName ) ;
6755 } ;
6856
6957 return (
7058 < details
71- className = "mb-1"
59+ ref = { containerRef }
60+ id = { `action-${ actionName } ` }
61+ className = { `mb-2 rounded-lg transition-all ${ isExpanded ? "overflow-hidden bg-gray-500" : "" } ${ isHighlighted ? "border-2 border-green-400 ring-2 ring-green-300/70" : "" } ` }
7262 open = { isExpanded }
73- onToggle = { ( ) => setIsExpanded ( ! isExpanded ) }
63+ onToggle = { ( e ) => setIsExpanded ( e . currentTarget . open ) }
7464 >
75- < summary
76- className = { `flex cursor-pointer items-center rounded-t-lg pl-2 text-xl font-bold text-white ${ isExpanded ? "bg-gray-500" : "" } ` }
77- >
78- < h3 className = "flex-grow px-2" > { action . title ?? props . actionName } </ h3 >
65+ < summary className = "flex cursor-pointer items-center py-1 pl-2 text-xl font-bold text-white" >
66+ < h3 className = "flex-grow px-2" > { action . title ?? actionName } </ h3 >
67+
7968 { isExpanded && (
80- < button
81- className = "flex h-10 w-10 items-center justify-center self-stretch rounded-bl-md rounded-tr-md bg-gray-400 text-base"
82- onClick = { handleDeleteAction }
83- >
84- < Trash2 size = { 16 } color = "white" />
85- </ button >
69+ < AffordanceButtons
70+ copyTitle = "Copy action"
71+ deleteTitle = "Delete action"
72+ onCopy = { ( e ) => {
73+ e . preventDefault ( ) ;
74+ e . stopPropagation ( ) ;
75+ onCopy ( ) ;
76+ } }
77+ onDelete = { ( e ) => {
78+ e . preventDefault ( ) ;
79+ e . stopPropagation ( ) ;
80+ handleDelete ( ) ;
81+ } }
82+ />
8683 ) }
8784 </ summary >
8885
89- < div className = "mb-4 rounded-b-lg bg-gray-500 px-2 pb-4" >
86+ < div className = "px-2 pb-4" >
9087 { action . description && (
9188 < div className = "px-2 pb-2 text-lg text-gray-400" >
9289 { action . description }
9390 </ div >
9491 ) }
95- < ul className = "list-disc pl-6 text-base text-gray-300" > { attributes } </ ul >
9692
97- < div className = "flex items-center justify-start pb-2 pt-2" >
98- < InfoIconWrapper
99- className = "flex-grow"
100- tooltip = { getFormsTooltipContent ( ) }
101- id = "actions"
102- >
103- < h4 className = "pr-1 text-lg font-bold text-white" > Forms</ h4 >
104- </ InfoIconWrapper >
105- </ div >
93+ < ul className = "list-disc pl-6 text-base text-gray-300" >
94+ { Object . entries ( attributeListObject ) . map ( ( [ k , v ] ) => (
95+ < li key = { k } >
96+ { k } : { JSON . stringify ( v ) }
97+ </ li >
98+ ) ) }
99+ </ ul >
100+
101+ < InfoIconWrapper tooltip = { getFormsTooltipContent ( ) } id = "actions" >
102+ < h4 className = "text-lg font-bold text-white" > Forms</ h4 >
103+ </ InfoIconWrapper >
104+
105+ < AddFormElement onClick = { ( ) => addFormDialog . current ?. openModal ( ) } />
106106
107- < AddFormElement onClick = { handleOpenAddFormDialog } />
108107 < AddFormDialog
109- type = { "action" }
108+ type = "action"
110109 interaction = { action }
111- interactionName = { props . actionName }
110+ interactionName = { actionName }
112111 ref = { addFormDialog }
113112 />
113+
114114 { forms . map ( ( form , i ) => (
115115 < Form
116- key = { `${ i } -${ form . href } ` }
116+ key = { `${ i } -${ form ? .href } ` }
117117 form = { form }
118- propName = { props . actionName }
119- interactionType = { "action" }
120- > </ Form >
118+ propName = { actionName }
119+ interactionType = "action"
120+ / >
121121 ) ) }
122122 </ div >
123123 </ details >
0 commit comments