Skip to content

Commit 468ccd6

Browse files
Merge pull request #482 from CedricProfessionnel/ReformatingExportAws
Reformat and add asset export (Feature Aws Part)
2 parents df7dc2c + a1c289e commit 468ccd6

File tree

6 files changed

+196
-23
lines changed

6 files changed

+196
-23
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fetchAFile from "./fetch-a-file"
2+
const createAssets = async (dataset, configExport, dm) => {
3+
await Promise.all(
4+
dataset.samples.map(async (sample, index, samples) => {
5+
var sampleName = await dm.getSampleName(sample)
6+
var blob = await fetchAFile(sample, configExport, dm)
7+
if (!blob) return
8+
await dm.addAsset(sampleName, blob, dataset.name)
9+
})
10+
)
11+
}
12+
export default createAssets
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fetchAFile = async (element, configExport, dm) => {
2+
var response
3+
var url
4+
if (dm.getSampleUrl(element) !== undefined) {
5+
url = configExport.proxyUrl + dm.getSampleUrl(element)
6+
response = await fetch(url, {
7+
method: "GET",
8+
headers: {
9+
"X-Requested-With": "xmlhttprequest",
10+
},
11+
}).catch((error) => {
12+
console.log("Looks like there was a problem: \n", error)
13+
})
14+
if (response) {
15+
const blob = await response.blob()
16+
return blob
17+
}
18+
} else {
19+
var text = await dm.getAssetText(element)
20+
if (text) return text
21+
var time = await dm.getAssetTime(element)
22+
if (time) return time
23+
}
24+
}
25+
export default fetchAFile

src/components/ExportToCognitoS3Dialog/index.js

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ import useActiveDatasetManager from "../../hooks/use-active-dataset-manager"
55
import isEmpty from "lodash/isEmpty"
66
import datasetManagerCognito from "udt-dataset-managers/dist/CognitoDatasetManager"
77
import useAuth from "../../utils/auth-handlers/use-auth"
8-
import { Grid, TextField } from "@material-ui/core"
98
import { useTranslation } from "react-i18next"
10-
const orangeText = { color: "orange" }
9+
import { TextField, Grid, IconButton } from "@material-ui/core"
10+
import WarningHeader from "./warning-header"
11+
import initConfigExport from "./init-config-export"
12+
import SettingDialog from "./interface-setting-export.js"
13+
import createAssets from "./create-assets"
14+
import { setIn } from "seamless-immutable"
15+
import {
16+
Settings as SettingsIcon,
17+
Storage as StorageIcon,
18+
} from "@material-ui/icons/"
1119

1220
const customStyles = {
1321
headCells: {
@@ -62,6 +70,7 @@ export default ({ open, onClose }) => {
6270
)
6371
}
6472
const [refreshInterface, setRefreshInterface] = useState(false)
73+
const [configExport, setConfigExport] = useState(initConfigExport)
6574

6675
const getCurrentDataset = async () => {
6776
if (currentDataset) return currentDataset
@@ -73,6 +82,10 @@ export default ({ open, onClose }) => {
7382
const getProjectName = () => {
7483
if (!currentDataset) return
7584
if (!currentDataset.name) return
85+
if (currentDataset.name === "New undefined Dataset") {
86+
setNameProjectToCreate("")
87+
return ""
88+
}
7689
setNameProjectToCreate(currentDataset.name)
7790
return currentDataset.name
7891
}
@@ -135,14 +148,30 @@ export default ({ open, onClose }) => {
135148
const handleCreateProject = async () => {
136149
if (!currentDataset) return
137150
var dataset = currentDataset
138-
dataset = dataset.setIn(["name"], nameProjectToCreate)
151+
152+
dataset = await dataset.setIn(["name"], nameProjectToCreate)
153+
139154
if (nameProjectExist) await dm.removeSamplesFolder(nameProjectToCreate)
155+
if (nameProjectExist && configExport.typeAssetExport === "withProxy")
156+
await dm.removeAssetsFolder(nameProjectToCreate)
157+
if (configExport.typeAssetExport === "withProxy") {
158+
dataset = await renameAllSamples(dataset)
159+
await createAssets(dataset, configExport, dm)
160+
}
140161
await dm.setDataset(dataset)
141162
await activeDatasetManager.setDataset(dataset)
142-
await getProjects()
143163
onClose()
144164
}
145165

166+
const renameAllSamples = async (dataset) => {
167+
var samples = await Promise.all(
168+
await dataset.samples.map(async (sample, index, samples) => {
169+
return await dm.addNamesToSample(sample, index, samples)
170+
})
171+
)
172+
return (dataset = await setIn(dataset, ["samples"], samples))
173+
}
174+
146175
return (
147176
<SimpleDialog
148177
title={t("export-project")}
@@ -158,13 +187,12 @@ export default ({ open, onClose }) => {
158187
]}
159188
>
160189
{
161-
<Grid container spacing={0}>
190+
<Grid container spacing={1}>
162191
<Grid container item xs={12} spacing={0} justify="center">
163-
{nameProjectExist ? (
164-
<p style={orangeText}>{t("warning-project-exist")}</p>
165-
) : (
166-
<p></p>
167-
)}
192+
<WarningHeader
193+
nameProjectToCreate={nameProjectToCreate}
194+
nameProjectExist={nameProjectExist}
195+
/>
168196
</Grid>
169197
<Grid container item xs={12} spacing={0} justify="center">
170198
<TextField
@@ -176,21 +204,42 @@ export default ({ open, onClose }) => {
176204
}}
177205
value={nameProjectToCreate}
178206
/>
207+
<IconButton
208+
onClick={() => {
209+
setConfigExport({
210+
...configExport,
211+
contentDialogBoxIsSetting: !configExport.contentDialogBoxIsSetting,
212+
})
213+
}}
214+
>
215+
{configExport.contentDialogBoxIsSetting ? (
216+
<StorageIcon id="StorageIcon" />
217+
) : (
218+
<SettingsIcon id="SettingIcon" />
219+
)}
220+
</IconButton>
179221
</Grid>
180222
<Grid container item xs={12} spacing={0} justify="center">
181-
{!isEmpty(projects) && (
182-
<DataTable
183-
expandableRows
184-
expandableRowsComponent={<ExpandedRow />}
185-
dense
186-
noHeader
187-
noTableHead
188-
columns={columns}
189-
selectableRowSelected={(row) => row.isSelected}
190-
data={projects}
191-
pagination={projects.length > 10}
192-
paginationPerPage={10}
193-
paginationRowsPerPageOptions={[10, 20, 25, 50, 100, 200]}
223+
{!configExport.contentDialogBoxIsSetting ? (
224+
!isEmpty(projects) && (
225+
<DataTable
226+
expandableRows
227+
expandableRowsComponent={<ExpandedRow />}
228+
dense
229+
noHeader
230+
noTableHead
231+
columns={columns}
232+
selectableRowSelected={(row) => row.isSelected}
233+
data={projects}
234+
pagination={projects.length > 10}
235+
paginationPerPage={10}
236+
paginationRowsPerPageOptions={[10, 20, 25, 50, 100, 200]}
237+
/>
238+
)
239+
) : (
240+
<SettingDialog
241+
configExport={configExport}
242+
setConfigExport={setConfigExport}
194243
/>
195244
)}
196245
</Grid>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const basicConfig = {
2+
contentDialogBoxIsSetting: false,
3+
typeAssetExport: "none",
4+
proxyUrl: "https://cors-anywhere.herokuapp.com/",
5+
}
6+
export default basicConfig
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from "react"
2+
import {
3+
Grid,
4+
FormControl,
5+
FormLabel,
6+
RadioGroup,
7+
FormControlLabel,
8+
Radio,
9+
TextField,
10+
} from "@material-ui/core"
11+
const settingDialog = ({ configExport, setConfigExport }) => {
12+
return (
13+
<Grid container xs={12} spacing={0}>
14+
<Grid container item xs={12} spacing={1} justify="center" direction="row">
15+
<Grid xs={6}>
16+
<FormControl component="fieldset">
17+
<FormLabel component="legend">Assets processing</FormLabel>
18+
<RadioGroup
19+
aria-label="option1"
20+
onChange={(event) => {
21+
setConfigExport({
22+
...configExport,
23+
typeAssetExport: event.target.value,
24+
})
25+
}}
26+
>
27+
<FormControlLabel
28+
value="none"
29+
control={<Radio />}
30+
label="Don't upload assets"
31+
checked={configExport.typeAssetExport === "none"}
32+
/>
33+
<FormControlLabel
34+
value="withProxy"
35+
control={<Radio />}
36+
label="Use a proxy"
37+
checked={configExport.typeAssetExport === "withProxy"}
38+
/>
39+
</RadioGroup>
40+
</FormControl>
41+
</Grid>
42+
<Grid container item xs={6} spacing={1} direction="column">
43+
<FormLabel>Proxy url</FormLabel>
44+
<TextField
45+
id="proxy"
46+
onChange={(event) => {
47+
setConfigExport({
48+
...configExport,
49+
proxyUrl: event.target.value,
50+
})
51+
}}
52+
onFocus={(event) => {
53+
event.target.select()
54+
}}
55+
value={configExport.proxyUrl}
56+
/>
57+
</Grid>
58+
</Grid>
59+
</Grid>
60+
)
61+
}
62+
63+
export default settingDialog
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react"
2+
const WarningHeader = ({ nameProjectExist, nameProjectToCreate }) => {
3+
const orangeText = { color: "orange", textAlign: "center" }
4+
var text = `Warning :`
5+
if (nameProjectExist)
6+
text += ` This project name already exist. If you continue the existing project with the same name will be replaced.\n`
7+
if (nameProjectToCreate === "") text += ` Please enter a project name.\n`
8+
return text === `Warning :` ? (
9+
<div></div>
10+
) : (
11+
<div style={orangeText}>
12+
{text.split("\n").map((obj, i) => {
13+
return <div key={i}>{obj}</div>
14+
})}
15+
</div>
16+
)
17+
}
18+
export default WarningHeader

0 commit comments

Comments
 (0)