1414// You should have received a copy of the GNU Affero General Public License
1515// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17- import React , { useEffect , useState } from "react" ;
17+ import React , { useEffect , useState , Fragment } from "react" ;
1818import get from "lodash/get" ;
1919import { connect } from "react-redux" ;
2020import { createStyles , Theme , withStyles } from "@material-ui/core/styles" ;
@@ -34,6 +34,7 @@ import api from "../../../../../../common/api";
3434import ModalWrapper from "../../../../Common/ModalWrapper/ModalWrapper" ;
3535import PredefinedList from "../../../../Common/FormComponents/PredefinedList/PredefinedList" ;
3636import DaysSelector from "../../../../Common/FormComponents/DaysSelector/DaysSelector" ;
37+ import { LinearProgress } from "@material-ui/core" ;
3738
3839const styles = ( theme : Theme ) =>
3940 createStyles ( {
@@ -68,9 +69,11 @@ const ShareFile = ({
6869 setModalErrorSnackMessage,
6970} : IShareFileProps ) => {
7071 const [ shareURL , setShareURL ] = useState < string > ( "" ) ;
72+ const [ isLoadingVersion , setIsLoadingVersion ] = useState < boolean > ( true ) ;
7173 const [ isLoadingFile , setIsLoadingFile ] = useState < boolean > ( false ) ;
7274 const [ selectedDate , setSelectedDate ] = useState < string > ( "" ) ;
7375 const [ dateValid , setDateValid ] = useState < boolean > ( true ) ;
76+ const [ versionID , setVersionID ] = useState < string > ( "null" ) ;
7477
7578 const initialDate = new Date ( ) ;
7679
@@ -85,7 +88,49 @@ const ShareFile = ({
8588 } ;
8689
8790 useEffect ( ( ) => {
88- if ( dateValid ) {
91+ // In case version is undefined, we get the latest version of the object
92+ if ( dataObject . version_id === undefined ) {
93+ // In case it is not distributed setup, then we default to "null";
94+ if ( distributedSetup ) {
95+ api
96+ . invoke (
97+ "GET" ,
98+ `/api/v1/buckets/${ bucketName } /objects?prefix=${ btoa (
99+ dataObject . name
100+ ) } ${ distributedSetup ? "&with_versions=true" : "" } `
101+ )
102+ . then ( ( res : IFileInfo [ ] ) => {
103+ const result = get ( res , "objects" , [ ] ) ;
104+
105+ const latestVersion = result . find (
106+ ( elem : IFileInfo ) => elem . is_latest
107+ ) ;
108+
109+ if ( latestVersion ) {
110+ setVersionID ( latestVersion . version_id ) ;
111+ return ;
112+ }
113+
114+ // Version couldn't ve retrieved, we default
115+ setVersionID ( "null" ) ;
116+ } )
117+ . catch ( ( error : ErrorResponseHandler ) => {
118+ setModalErrorSnackMessage ( error ) ;
119+ } ) ;
120+
121+ setIsLoadingVersion ( false ) ;
122+ return ;
123+ }
124+ setVersionID ( "null" ) ;
125+ setIsLoadingVersion ( false ) ;
126+ return ;
127+ }
128+ setVersionID ( dataObject . version_id || "null" ) ;
129+ setIsLoadingVersion ( false ) ;
130+ } , [ bucketName , dataObject , distributedSetup , setModalErrorSnackMessage ] ) ;
131+
132+ useEffect ( ( ) => {
133+ if ( dateValid && ! isLoadingVersion ) {
89134 setIsLoadingFile ( true ) ;
90135 setShareURL ( "" ) ;
91136
@@ -95,14 +140,12 @@ const ShareFile = ({
95140 const diffDate = slDate . getTime ( ) - currDate . getTime ( ) ;
96141
97142 if ( diffDate > 0 ) {
98- const versID = distributedSetup ? dataObject . version_id : "null" ;
99-
100143 api
101144 . invoke (
102145 "GET" ,
103146 `/api/v1/buckets/${ bucketName } /objects/share?prefix=${ btoa (
104147 dataObject . name
105- ) } &version_id=${ versID || "null" } ${
148+ ) } &version_id=${ versionID } ${
106149 selectedDate !== "" ? `&expires=${ diffDate } ms` : ""
107150 } `
108151 )
@@ -125,6 +168,8 @@ const ShareFile = ({
125168 setShareURL ,
126169 setModalErrorSnackMessage ,
127170 distributedSetup ,
171+ isLoadingVersion ,
172+ versionID ,
128173 ] ) ;
129174
130175 return (
@@ -137,42 +182,51 @@ const ShareFile = ({
137182 } }
138183 >
139184 < Grid container className = { classes . modalContent } >
140- < Grid item xs = { 12 } className = { classes . moduleDescription } >
141- This module generates a temporary URL with integrated access
142- credentials for sharing objects for up to 7 days.
143- < br />
144- The temporary URL expires after the configured time limit.
145- </ Grid >
146- < Grid item xs = { 12 } className = { classes . dateContainer } >
147- < DaysSelector
148- initialDate = { initialDate }
149- id = "date"
150- label = "Active for"
151- maxDays = { 7 }
152- onChange = { dateChanged }
153- entity = "Link"
154- />
155- </ Grid >
156- < Grid container item xs = { 12 } >
157- < Grid item xs = { 10 } >
158- < PredefinedList content = { shareURL } />
159- </ Grid >
160- < Grid item xs = { 2 } className = { classes . copyButtonContainer } >
161- < CopyToClipboard text = { shareURL } >
162- < Button
163- variant = "contained"
164- color = "primary"
165- startIcon = { < CopyIcon /> }
166- onClick = { ( ) => {
167- setModalSnackMessage ( "Share URL Copied to clipboard" ) ;
168- } }
169- disabled = { shareURL === "" || isLoadingFile }
170- >
171- Copy
172- </ Button >
173- </ CopyToClipboard >
185+ { isLoadingVersion && (
186+ < Grid item xs = { 12 } >
187+ < LinearProgress />
174188 </ Grid >
175- </ Grid >
189+ ) }
190+ { ! isLoadingVersion && (
191+ < Fragment >
192+ < Grid item xs = { 12 } className = { classes . moduleDescription } >
193+ This module generates a temporary URL with integrated access
194+ credentials for sharing objects for up to 7 days.
195+ < br />
196+ The temporary URL expires after the configured time limit.
197+ </ Grid >
198+ < Grid item xs = { 12 } className = { classes . dateContainer } >
199+ < DaysSelector
200+ initialDate = { initialDate }
201+ id = "date"
202+ label = "Active for"
203+ maxDays = { 7 }
204+ onChange = { dateChanged }
205+ entity = "Link"
206+ />
207+ </ Grid >
208+ < Grid container item xs = { 12 } >
209+ < Grid item xs = { 10 } >
210+ < PredefinedList content = { shareURL } />
211+ </ Grid >
212+ < Grid item xs = { 2 } className = { classes . copyButtonContainer } >
213+ < CopyToClipboard text = { shareURL } >
214+ < Button
215+ variant = "contained"
216+ color = "primary"
217+ startIcon = { < CopyIcon /> }
218+ onClick = { ( ) => {
219+ setModalSnackMessage ( "Share URL Copied to clipboard" ) ;
220+ } }
221+ disabled = { shareURL === "" || isLoadingFile }
222+ >
223+ Copy
224+ </ Button >
225+ </ CopyToClipboard >
226+ </ Grid >
227+ </ Grid >
228+ </ Fragment >
229+ ) }
176230 </ Grid >
177231 </ ModalWrapper >
178232 </ React . Fragment >
0 commit comments