Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit fe68d05

Browse files
authored
Merge pull request #241 from daita-technologies/feat/create_project_with_existed_dataset
feat: create project with existed dataset
2 parents 6e8f60b + c36b874 commit fe68d05

6 files changed

Lines changed: 234 additions & 26 deletions

File tree

.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ REACT_APP_API_AMAZON_COGNITO=https://abc123.auth.your-region.amazoncognito.com/o
1515
REACT_APP_COGNITO_REDIRECT_URI=https://abc123.api.your-region.amazonaws.com/dev/auth/login_social
1616
REACT_APP_COGNITO_CLIENTID=your-cognito-client-id
1717
REACT_APP_API_LOGOUT_SOCIAL=https://auth.com/logout?client_id=123
18-
REACT_APP_GITHUB_IDENTITY_PROVIDER=ProviderName
18+
REACT_APP_GITHUB_IDENTITY_PROVIDER=ProviderName
19+
REACT_APP_FEEDBACK_SLACK=https://abc123.api.your-region.amazonaws.com/dev/webhook/client-feedback
20+
REACT_APP_PRESIGN_URL_UPLOAD_FEEDBACK_IMAGE=https://abc123.api.your-region.amazonaws.com/devdaitabeapp/feedback/presign_url_image
21+
REACT_APP_CREATE_PROJECT_SAMPLE=https://abc123.api.your-region.amazonaws.com

src/components/CreateProjectModal/index.tsx

Lines changed: 175 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
1-
import { useDispatch, useSelector } from "react-redux";
1+
import CancelIcon from "@mui/icons-material/Cancel";
22
import {
3-
Modal,
4-
Typography,
3+
Autocomplete,
54
Box,
6-
TextField,
7-
Input,
5+
FormControlLabel,
6+
FormLabel,
87
IconButton,
8+
Input,
9+
Modal,
10+
Radio,
11+
RadioGroup,
12+
Slider,
13+
SxProps,
14+
TextField,
15+
Theme,
16+
Typography,
917
} from "@mui/material";
10-
import CancelIcon from "@mui/icons-material/Cancel";
18+
import { useForm } from "react-hook-form";
19+
import { useDispatch, useSelector } from "react-redux";
1120
import { CREATE_PROJECT } from "reduxes/project/constants";
12-
import { CreateProjectFields, CreateProjectModalProps } from "./type";
1321
import { modalCloseStyle, modalStyle } from "styles/generalStyle";
14-
import { useForm } from "react-hook-form";
1522
import { getLocalStorage } from "utils/general";
23+
import {
24+
CreateProjectFields,
25+
CreateProjectModalProps,
26+
PrebuildDataset,
27+
} from "./type";
1628

17-
import { ID_TOKEN_NAME, TOKEN_NAME } from "constants/defaultValues";
18-
import { RootState } from "reduxes";
1929
import { MyButton } from "components";
30+
import {
31+
ID_TOKEN_NAME,
32+
MAX_ALLOW_UPLOAD_IMAGES,
33+
TOKEN_NAME,
34+
} from "constants/defaultValues";
35+
import { useEffect, useState } from "react";
36+
import { toast } from "react-toastify";
37+
import { RootState } from "reduxes";
38+
import { projectApi } from "services";
2039

40+
const radioCss: SxProps<Theme> = {
41+
flex: 1,
42+
justifyContent: "center",
43+
border: "1px dashed",
44+
margin: "2px",
45+
};
2146
const CreateProjectModal = function (props: CreateProjectModalProps) {
2247
const { isOpen, handleClose } = props;
2348
const dispatch = useDispatch();
@@ -32,22 +57,83 @@ const CreateProjectModal = function (props: CreateProjectModalProps) {
3257
idToken: getLocalStorage(ID_TOKEN_NAME) || "",
3358
},
3459
});
60+
const [listPrebuildDataset, setListPrebuildDataset] = useState<
61+
PrebuildDataset[]
62+
>([]);
63+
const [fromDatasets, setFromDatasets] = useState<"Empty" | "Existed">(
64+
"Empty"
65+
);
66+
const [prebuildDataset, setPrebuildDataset] =
67+
useState<PrebuildDataset | null>(null);
68+
const [isLoadingPrebuildDataset, setIsLoadingPrebuildDataset] =
69+
useState<boolean>(true);
70+
const [numberOfImages, setNumberOfImages] = useState<number>(0);
71+
72+
useEffect(() => {
73+
projectApi
74+
.getListPrebuildDataset({ idToken: getLocalStorage(ID_TOKEN_NAME) || "" })
75+
.then((resp: any) => {
76+
if (!resp.error) {
77+
console.log("resp", resp);
78+
const prebuildDatasets: PrebuildDataset[] = [];
79+
for (const prebuildDataset of resp.data) {
80+
prebuildDatasets.push({
81+
isActive: prebuildDataset.is_active,
82+
s3Key: prebuildDataset.s3_key,
83+
name: prebuildDataset.name,
84+
totalImage: prebuildDataset.total_images,
85+
visualName: prebuildDataset.visual_name,
86+
});
87+
}
88+
console.log("prebuildDatasets", prebuildDatasets);
89+
setListPrebuildDataset([...prebuildDatasets]);
90+
} else {
91+
toast.error(resp.message);
92+
}
93+
setIsLoadingPrebuildDataset(false);
94+
});
95+
}, []);
3596

3697
const isLoading = useSelector(
3798
(state: RootState) => state.projectReducer.isCreatingProject
3899
);
39100

40101
const onSubmitCreateProject = (fields: CreateProjectFields) => {
102+
if (fromDatasets === "Existed") {
103+
if (prebuildDataset) {
104+
fields.createProjectPreBuild = {
105+
nameIdPrebuild: prebuildDataset.name,
106+
numberRadom: numberOfImages,
107+
};
108+
} else {
109+
toast.error("Please select one prebuild dataset");
110+
return;
111+
}
112+
}
113+
41114
dispatch({ type: CREATE_PROJECT.REQUESTED, payload: fields });
42115
};
43116

117+
const handleChangeFromDatasets = (
118+
event: React.ChangeEvent<HTMLInputElement>
119+
) => {
120+
setFromDatasets(
121+
(event.target as HTMLInputElement).value as "Empty" | "Existed"
122+
);
123+
};
124+
const handleChangeNumberOfImages = (
125+
_event: Event,
126+
newValue: number | number[]
127+
) => {
128+
setNumberOfImages(newValue as number);
129+
};
44130
return (
45131
<Modal
46132
open={isOpen}
47133
onClose={!isLoading ? handleClose : undefined}
48134
disableEscapeKeyDown
49135
>
50-
<Box sx={{ ...modalStyle, width: 600 }}>
136+
<Box sx={{ ...modalStyle, width: 700 }}>
51137
<IconButton
52138
sx={modalCloseStyle}
53139
onClick={!isLoading ? handleClose : undefined}
@@ -98,6 +184,84 @@ const CreateProjectModal = function (props: CreateProjectModalProps) {
98184
fullWidth
99185
disabled={isLoading}
100186
/>
187+
<Box mt={1}>
188+
<FormLabel id="demo-radio-buttons-group-label">
189+
From Datasets
190+
</FormLabel>
191+
<RadioGroup
192+
aria-labelledby="demo-radio-buttons-group-label"
193+
defaultValue="female"
194+
name="radio-buttons-group"
195+
row
196+
sx={{ justifyContent: "space-around", height: 200 }}
197+
value={fromDatasets}
198+
onChange={handleChangeFromDatasets}
199+
>
200+
<Box display="flex" sx={{ ...radioCss }}>
201+
<FormControlLabel
202+
value="Empty"
203+
control={<Radio />}
204+
label="Empty"
205+
labelPlacement="start"
206+
sx={{ justifyContent: "center", margin: 0 }}
207+
/>
208+
</Box>
209+
<Box
210+
display="flex"
211+
sx={{ ...radioCss, alignItems: "center" }}
212+
flexDirection="column"
213+
>
214+
<FormControlLabel
215+
value="Existed"
216+
control={<Radio />}
217+
label="Existed"
218+
labelPlacement="start"
219+
sx={{ justifyContent: "center", margin: 0 }}
220+
/>
221+
{fromDatasets === "Existed" && (
222+
<>
223+
<Autocomplete
224+
id="prebuild-dataset"
225+
value={prebuildDataset}
226+
disablePortal
227+
loading={isLoadingPrebuildDataset}
228+
options={listPrebuildDataset}
229+
getOptionLabel={(option) => option.name}
230+
onChange={(_, value) => {
231+
if (value) {
232+
setPrebuildDataset(value);
233+
setNumberOfImages(value.totalImage);
234+
}
235+
}}
236+
renderOption={(props, option) => (
237+
<li {...props} key={option.name}>
238+
{option.name}
239+
</li>
240+
)}
241+
sx={{ width: 300 }}
242+
renderInput={(params) => (
243+
<TextField {...params} label="List prebuild dataset" />
244+
)}
245+
/>
246+
<Box mt={2} width="95%">
247+
<Typography id="input-slider" gutterBottom>
248+
Number of images
249+
</Typography>
250+
<Slider
251+
size="small"
252+
value={numberOfImages}
253+
min={0}
254+
max={MAX_ALLOW_UPLOAD_IMAGES}
255+
aria-label="Small"
256+
valueLabelDisplay="auto"
257+
onChange={handleChangeNumberOfImages}
258+
/>
259+
</Box>
260+
</>
261+
)}
262+
</Box>
263+
</RadioGroup>
264+
</Box>
101265
<Box display="flex" justifyContent="flex-end" marginTop={6}>
102266
<MyButton
103267
type="submit"

src/components/CreateProjectModal/type.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ export interface CreateProjectFields {
88
accessToken: string;
99
projectName: string;
1010
description: string;
11+
createProjectPreBuild?: CreateProjectPreBuildFields;
12+
}
13+
14+
export interface CreateProjectPreBuildFields {
15+
nameIdPrebuild: string;
16+
numberRadom: number;
17+
}
18+
export interface PrebuildDataset {
19+
name: string;
20+
s3Key: string;
21+
totalImage: number;
22+
visualName: string;
23+
isActive: boolean;
1124
}

src/constants/defaultValues.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { TaskProcessType, TaskStatusMergedType } from "./taskType";
44

55
export const authApiURL = process.env.REACT_APP_AUTH_API_URL;
66
export const projectApiUrl = process.env.REACT_APP_PROJECT_API_URL;
7+
export const createProjectSampleApiUrl =
8+
process.env.REACT_APP_CREATE_PROJECT_SAMPLE;
79
export const generateApiUrl = process.env.REACT_APP_GENERATE_API_URL;
810
export const downloadZipApiUrl = process.env.REACT_APP_DOWNLOAD_ZIP_API;
911
export const uploadZipApiUrl = process.env.REACT_APP_UPLOAD_ZIP_API;

src/services/projectApi.tsx

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
uploadZipApiUrl,
88
generateApiUrl,
99
ID_TOKEN_NAME,
10+
createProjectSampleApiUrl,
1011
} from "constants/defaultValues";
1112
import { ApiResponse } from "constants/type";
1213
import { FetchImagesParams, ImageSourceType } from "reduxes/album/type";
@@ -40,6 +41,11 @@ export interface CreateSampleRequestBody {
4041
accessToken: string;
4142
}
4243

44+
export interface CreateSampleProjectFromPrebuildRequestBody {
45+
idToken: string;
46+
name_id_prebuild: string;
47+
number_random: string;
48+
}
4349
export interface DeleteProjectRequestBody {
4450
idToken: string;
4551
projectId: string;
@@ -84,17 +90,33 @@ const projectApi = {
8490
accessToken,
8591
projectName,
8692
description,
87-
}: CreateProjectFields) =>
88-
axios.post(
89-
`${projectApiUrl}/projects/create`,
90-
{
91-
id_token: idToken,
92-
access_token: accessToken,
93-
project_name: projectName,
94-
project_info: description,
95-
},
96-
{ headers: getAuthHeader() }
97-
),
93+
createProjectPreBuild,
94+
}: CreateProjectFields) => {
95+
if (createProjectPreBuild) {
96+
return axios.post(
97+
`${createProjectSampleApiUrl}/projects/create_project_from_prebuild`,
98+
{
99+
id_token: idToken,
100+
project_name: projectName,
101+
project_info: description,
102+
name_id_prebuild: createProjectPreBuild.nameIdPrebuild,
103+
number_random: createProjectPreBuild.numberRadom,
104+
},
105+
{ headers: getAuthHeader() }
106+
);
107+
} else {
108+
return axios.post(
109+
`${projectApiUrl}/projects/create`,
110+
{
111+
id_token: idToken,
112+
access_token: accessToken,
113+
project_name: projectName,
114+
project_info: description,
115+
},
116+
{ headers: getAuthHeader() }
117+
);
118+
}
119+
},
98120
listProjects: ({ idToken }: { idToken: string }) =>
99121
axios.post(
100122
`${projectApiUrl}/projects/list_info`,
@@ -235,6 +257,10 @@ const projectApi = {
235257
task_id: taskId,
236258
},
237259
}),
260+
getListPrebuildDataset: ({ idToken }: { idToken: string }) =>
261+
axios.post(`${createProjectSampleApiUrl}/projects/list_prebuild_dataset`, {
262+
id_token: idToken,
263+
}),
238264
deleteProject: ({
239265
idToken,
240266
projectId,

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,9 +2905,9 @@
29052905
integrity sha512-Q15Clj3lZSLnhVA6yKw1G7SQz46DeL9gO1TEgfK1OQGvMdQ6TUWmCeWf1QBUNkw2BDfV52i2YuYd9OF3ZwGhjw==
29062906

29072907
"@types/node@^12.20.14":
2908-
version "12.20.46"
2909-
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.46.tgz#7e49dee4c54fd19584e6a9e0da5f3dc2e9136bc7"
2910-
integrity sha512-cPjLXj8d6anFPzFvOPxS3fvly3Shm5nTfl6g8X5smexixbuGUf7hfr21J5tX9JW+UPStp/5P5R8qrKL5IyVJ+A==
2908+
version "12.20.55"
2909+
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240"
2910+
integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==
29112911

29122912
"@types/normalize-package-data@^2.4.0":
29132913
version "2.4.1"

0 commit comments

Comments
 (0)