1- import { useDispatch , useSelector } from "react-redux " ;
1+ import CancelIcon from "@mui/icons-material/Cancel " ;
22import {
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" ;
1120import { CREATE_PROJECT } from "reduxes/project/constants" ;
12- import { CreateProjectFields , CreateProjectModalProps } from "./type" ;
1321import { modalCloseStyle , modalStyle } from "styles/generalStyle" ;
14- import { useForm } from "react-hook-form" ;
1522import { 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" ;
1929import { 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+ } ;
2146const 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"
0 commit comments