11import React , { useState , useEffect , useRef } from "react"
22import PropTypes from "prop-types"
3+ import _ from "lodash"
34
4- const TransferButton = ( { label, handleClick } ) => {
5- const [ btnText , setBtnText ] = useState ( label )
5+ const TransferButton = ( { label, localId, group, target, handleTransfer } ) => {
6+ const [ requesting , setRequesting ] = useState ( false )
7+ const [ providedLocalId , setProvidedLocalId ] = useState ( "" )
68 const timerRef = useRef ( null )
79
810 useEffect (
@@ -12,27 +14,120 @@ const TransferButton = ({ label, handleClick }) => {
1214 [ ]
1315 )
1416
15- const handleBtnClick = ( event ) => {
16- setBtnText ( < em > Requesting</ em > )
17- timerRef . current = setTimeout ( ( ) => setBtnText ( label ) , 3000 )
18- handleClick ( event )
17+ const handleExistingLocalIdClick = ( event ) => {
18+ handleTransfer ( localId )
19+ notify ( )
1920 event . preventDefault ( )
2021 }
2122
23+ const handleProvidedLocalIdClick = ( event ) => {
24+ handleTransfer ( providedLocalId )
25+ notify ( )
26+ event . preventDefault ( )
27+ }
28+
29+ const handleNoLocalIdClick = ( event ) => {
30+ handleTransfer ( null )
31+ notify ( )
32+ event . preventDefault ( )
33+ }
34+
35+ const notify = ( ) => {
36+ setRequesting ( true )
37+ timerRef . current = setTimeout ( ( ) => setRequesting ( false ) , 3000 )
38+ }
39+
40+ const handleChangeProvidedLocalId = ( event ) => {
41+ setProvidedLocalId ( event . target . value )
42+ event . preventDefault ( )
43+ }
44+
45+ if ( requesting ) {
46+ return (
47+ < button type = "button" className = "btn btn-secondary btn-no-outline" >
48+ < em > Requesting</ em >
49+ </ button >
50+ )
51+ }
52+
53+ const btnId = `transferBtn-${ group } -${ target } `
54+ const btnClasses = [ "btn" , "dropdown-toggle" , "btn-no-outline" ]
55+ const dropDownItemBtnClasses = [ "btn" , "btn-secondary" , "dropdown-item" ]
56+
2257 return (
23- < button
24- type = "button"
25- className = "btn btn-secondary btn-no-outline"
26- onClick = { handleBtnClick }
27- >
28- { btnText }
29- </ button >
58+ < div className = "btn-group dropdown" >
59+ < button
60+ type = "button"
61+ id = { btnId }
62+ className = { btnClasses . join ( " " ) }
63+ aria-label = "Transfer to catalog"
64+ title = "Transfer to catalog"
65+ data-bs-toggle = "dropdown"
66+ aria-expanded = "false"
67+ >
68+ { label }
69+ </ button >
70+ < div className = "dropdown-menu dropdown-menu-end" aria-labelledby = { btnId } >
71+ { localId ? (
72+ < React . Fragment >
73+ < button
74+ className = { dropDownItemBtnClasses . join ( " " ) }
75+ onClick = { handleExistingLocalIdClick }
76+ >
77+ Export with local system ID: { localId }
78+ </ button >
79+ </ React . Fragment >
80+ ) : (
81+ < React . Fragment >
82+ < div className = "dropdown-item" >
83+ Overlay record with local system ID:
84+ < div className = "input-group mb-3" >
85+ < input
86+ type = "text"
87+ className = "form-control"
88+ aria-label = "Enter local system id"
89+ onChange = { handleChangeProvidedLocalId }
90+ value = { providedLocalId }
91+ />
92+ < button
93+ className = "btn btn-primary"
94+ type = "button"
95+ disabled = { _ . isEmpty ( providedLocalId ) }
96+ onClick = { handleProvidedLocalIdClick }
97+ >
98+ Go
99+ </ button >
100+ </ div >
101+ </ div >
102+ < button
103+ className = { dropDownItemBtnClasses . join ( " " ) }
104+ onClick = { handleNoLocalIdClick }
105+ >
106+ Create a new record in catalog.
107+ </ button >
108+ </ React . Fragment >
109+ ) }
110+ </ div >
111+ </ div >
30112 )
113+
114+ // return (
115+ // <button
116+ // type="button"
117+ // className="btn btn-secondary btn-no-outline"
118+ // onClick={handleBtnClick}
119+ // >
120+ // {label}
121+ // </button>
122+ // )
31123}
32124
33125TransferButton . propTypes = {
34126 label : PropTypes . string . isRequired ,
35- handleClick : PropTypes . func . isRequired ,
127+ group : PropTypes . string . isRequired ,
128+ target : PropTypes . string . isRequired ,
129+ localId : PropTypes . string ,
130+ handleTransfer : PropTypes . func . isRequired ,
36131}
37132
38133export default TransferButton
0 commit comments