Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Ceph Webapp Poc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Jacopo Gasparetto
Ceph Webapp Poc
Commits
3727df70
Commit
3727df70
authored
1 year ago
by
Jacopo Gasparetto
Browse files
Options
Downloads
Patches
Plain Diff
Refactor InputFile folder to use only one single button to upload a file
parent
9e85d868
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
frontend/src/components/InputFile.tsx
+19
-6
19 additions, 6 deletions
frontend/src/components/InputFile.tsx
frontend/src/routes/BucketBrowser/index.tsx
+63
-41
63 additions, 41 deletions
frontend/src/routes/BucketBrowser/index.tsx
with
82 additions
and
47 deletions
frontend/src/components/InputFile.tsx
+
19
−
6
View file @
3727df70
import
{
ChangeEvent
}
from
"
react
"
;
import
{
ChangeEvent
,
ReactNode
}
from
"
react
"
;
import
{
Button
}
from
"
./Button
"
;
interface
InputFileProps
{
onChange
:
(
e
:
ChangeEvent
<
HTMLInputElement
>
)
=>
void
;
icon
?:
ReactNode
}
export
const
InputFile
=
({
onChange
}:
InputFileProps
)
=>
{
export
const
InputFile
=
({
onChange
,
icon
}:
InputFileProps
)
=>
{
const
openFileSelector
=
()
=>
{
const
fileSelector
=
document
.
getElementById
(
"
fileSelector
"
);
if
(
fileSelector
)
{
fileSelector
.
click
();
}
}
return
(
<
div
className
=
""
>
<
input
onChange
=
{
onChange
}
className
=
"
p-4
min-w-0
flex-auto
rounded
border
\
hover
:
bg-neutral-200
\
file
:
rounded-none
file
:
border-0
file
:
border-solid
\
file
:
bg-neutral-200
"
className
=
"hidden"
type
=
"file"
id
=
"fileSelector"
/>
<
Button
title
=
"Upload File"
icon
=
{
icon
}
onClick
=
{
openFileSelector
}
/>
</
div
>
);
...
...
This diff is collapsed.
Click to expand it.
frontend/src/routes/BucketBrowser/index.tsx
+
63
−
41
View file @
3727df70
import
{
ChangeEvent
,
useEffect
,
useRef
,
useState
}
from
'
react
'
;
import
{
ChangeEvent
,
useCallback
,
useEffect
,
useRef
,
useState
}
from
'
react
'
;
import
{
Page
}
from
'
../../components/Page
'
;
import
{
BucketObject
}
from
'
../../models/bucket
'
;
import
{
Column
,
Table
}
from
'
../../components/Table
'
;
...
...
@@ -21,6 +21,7 @@ import {
DeleteObjectCommand
}
from
'
@aws-sdk/client-s3
'
;
type
PropsType
=
{
bucketName
:
string
}
...
...
@@ -28,10 +29,10 @@ type PropsType = {
export
const
BucketBrowser
=
({
bucketName
}:
PropsType
)
=>
{
const
[
bucketObjects
,
setBucketObjects
]
=
useState
<
BucketObject
[]
>
([]);
const
[
selectedRows
,
setSelectedRows
]
=
useState
<
Set
<
number
>>
(
new
Set
());
const
[
file
,
setFile
]
=
useState
<
File
>
();
const
s3
=
useS3Service
();
const
navigate
=
useNavigate
();
const
inputRef
=
useRef
<
HTMLInputElement
>
();
const
lockRef
=
useRef
<
boolean
>
(
false
);
const
columns
:
Column
[]
=
[
{
id
:
"
icon
"
},
...
...
@@ -81,48 +82,72 @@ export const BucketBrowser = ({ bucketName }: PropsType) => {
]
});
const
refreshBucketObjects
=
useCallback
(()
=>
{
const
f
=
async
()
=>
{
if
(
!
s3
.
isAuthenticated
())
{
return
;
}
console
.
log
(
"
List Bucket objects...
"
)
const
listObjCmd
=
new
ListObjectsV2Command
({
Bucket
:
bucketName
});
const
response
=
await
s3
.
client
.
send
(
listObjCmd
);
const
{
Contents
}
=
response
;
if
(
Contents
)
{
setBucketObjects
(
Contents
);
}
else
{
console
.
warn
(
"
Warning: bucket looks empty.
"
);
}
};
f
().
catch
(
err
=>
console
.
error
(
err
));
},
[
s3
,
bucketName
]);
useEffect
(()
=>
{
if
(
!
s3
.
isAuthenticated
()
||
selectedRows
.
size
>
0
)
{
re
turn
;
if
(
bucketObjects
.
length
===
0
&&
!
lockRef
.
current
)
{
re
freshBucketObjects
()
}
const
listObjCmd
=
new
ListObjectsV2Command
({
Bucket
:
bucketName
});
s3
.
client
.
send
(
listObjCmd
)
.
then
(
response
=>
{
const
{
Contents
}
=
response
;
if
(
Contents
)
{
setBucketObjects
(
Contents
);
}
});
},
[
s3
,
file
,
selectedRows
])
return
()
=>
{
lockRef
.
current
=
true
;
}
},
[
bucketObjects
,
s3
,
refreshBucketObjects
])
const
handleFileChange
=
(
e
:
ChangeEvent
<
HTMLInputElement
>
)
=>
{
inputRef
.
current
=
e
.
target
;
if
(
e
.
target
.
files
)
{
setFile
(
e
.
target
.
files
[
0
]);
if
(
!
e
.
target
.
files
)
{
return
;
}
};
const
upload
=
()
=>
{
if
(
!
(
file
&&
s3
.
isAuthenticated
()))
{
if
(
!
s3
.
isAuthenticated
())
{
console
.
warn
(
"
Warning: cannot upload file because S3 service not authenticated
"
);
return
;
}
const
putObjCmd
=
new
PutObjectCommand
({
Bucket
:
bucketName
,
Body
:
file
,
Key
:
file
.
name
});
inputRef
.
current
=
e
.
target
;
const
{
files
}
=
e
.
target
;
// Upload all files FIXME: use different approach for multiple files
Array
.
from
(
files
).
forEach
(
file
=>
{
const
putObjCmd
=
new
PutObjectCommand
({
Bucket
:
bucketName
,
Body
:
file
,
Key
:
file
.
name
});
s3
.
client
.
send
(
putObjCmd
)
.
then
(()
=>
{
console
.
log
(
"
File uploaded
"
);
if
(
inputRef
.
current
)
{
inputRef
.
current
.
files
=
null
;
inputRef
.
current
.
value
=
""
;
}
setFile
(
undefined
);
})
.
catch
(
err
=>
console
.
error
(
err
));
s3
.
client
.
send
(
putObjCmd
)
.
then
(()
=>
{
console
.
log
(
"
File uploaded
"
);
if
(
inputRef
.
current
)
{
inputRef
.
current
.
files
=
null
;
inputRef
.
current
.
value
=
""
;
}
})
.
then
(
refreshBucketObjects
)
.
catch
(
err
=>
console
.
error
(
err
));
});
}
const
deleteObject
=
async
(
bucketName
:
string
,
key
:
string
)
=>
{
...
...
@@ -157,8 +182,8 @@ export const BucketBrowser = ({ bucketName }: PropsType) => {
// Wait untill all delete are done then refresh the UI.
Promise
.
all
(
promises
)
.
then
(()
=>
{
console
.
log
(
"
refresh
"
)
setSelectedRows
(
new
Set
())
setSelectedRows
(
new
Set
());
refreshBucketObjects
();
});
}
...
...
@@ -169,15 +194,12 @@ export const BucketBrowser = ({ bucketName }: PropsType) => {
icon
=
{
<
ArrowLeftIcon
/>
}
onClick
=
{
()
=>
navigate
(
-
1
)
}
/>
<
div
className
=
'container w-2/3'
>
<
div
className
=
"flex mt-8 place-content-between"
>
<
div
className
=
'flex space-x-4'
>
<
InputFile
onChange
=
{
handleFileChange
}
/>
<
Button
title
=
"Upload file"
<
InputFile
icon
=
{
<
ArrowUpOnSquareIcon
/>
}
onC
lick
=
{
upload
}
onC
hange
=
{
handleFileChange
}
/>
</
div
>
<
Button
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment