-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneStepJobPage.tsx
220 lines (187 loc) · 8.41 KB
/
OneStepJobPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* ----------------------------------------------------------
*
* A new page that combines all previous pipline steps into one page
* to simplify and remove abiguity from the upload process. The other pages
* that did exists in the pipeline should be broken into compoenents instead.
*
*
*
* ----------------------------------------------------------
*/
//package imports
import React, { useEffect, useRef } from "react"
import { useState } from "react"
import { Container, Row } from "react-bootstrap"
//local imports
import { BasicJobInfoInputs } from "../components/UploadPipeLine/majorSteps/BasicJobInfoInputs"
import ToolFormUploadForm from "../components/UploadPipeLine/majorSteps/ToolFormUploadForm"
import { ProtienReferenceUploadForm } from "../components/UploadPipeLine/majorSteps/ProtienReferenceUploadForm"
import FileUploadDisplay from "../components/fileUpload/FileUploadDisplay"
import { useNavigate, useParams } from "react-router-dom"
import { ChromFile, ToolData, fileSet } from "../components/tempTypes/Types"
import { jobSetupFormData } from "../components/tempTypes/Types"
import { apiPaths } from "../api/apiConfig"
import { paths } from "../config.mjs"
import axios from "axios"
import { HiglassUploadForm } from "../components/UploadPipeLine/higlassUploadForm"
import { HiglassToggle } from "../components/UploadPipeLine/higlassToggle"
type OneStepJobUploadPageProps = {
}
export const OneStepJobUploadPage = (props:OneStepJobUploadPageProps)=>{
//use params to get id from url
const params = useParams();
const navigate = useNavigate();
const formRef = useRef(null);
//------------------------
//The commmented out sections below are most of an implementation of storing
//the user's choices in cookies to make sure they dont lose their work if they
//are uploading a big job
//---------------------
// //stored data to make form persist
let storedProtienRefArr:string[]=[];
let storedProtienRefFileNames:string[]=[];
let storedToolData:ToolData[]=[];
let storedUseHiGlass:boolean=true;
let storedChromFile=new ChromFile();
let storedFileSets:fileSet[]=[];
let storedBasicInfo:any={};
// //these are all wrapped with try catches since we are just checking if the data
// //exists and loading if so, hence we dont need any more than a simple try catch
// try {storedProtienRefArr=JSON.parse(localStorage.getItem('storedProtienRefArr'))} catch {}
// try {storedProtienRefFileNames=JSON.parse(localStorage.getItem('storedProtienRefFileNames'))} catch {}
// try {storedToolData=JSON.parse(localStorage.getItem('storedToolData'))} catch {}
// try {storedUseHiGlass=JSON.parse(localStorage.getItem('storedUseHiGlass'))} catch {}
// try {storedChromFile=JSON.parse(localStorage.getItem('storedChromFile'))} catch {}
// try {storedFileSets=JSON.parse(localStorage.getItem('storedFileSets'))} catch {}
// try {storedBasicInfo=JSON.parse(localStorage.getItem('storedBasicInfo'))} catch {}
// console.log(localStorage.getItem('storedProtienRefArr'));
// console.log(localStorage.getItem('storedProtienRefFileNames'));
//form states
const [protienRefFiles, setProtienRefFiles] = useState<File[]>(storedProtienRefArr?storedProtienRefArr:[]);
const [protienRefFileNames, setProtienRefFileNames] = useState<string[]>(storedProtienRefFileNames?storedProtienRefFileNames:[]);
const [toolData, setToolData] = useState<ToolData[]>(storedToolData?storedToolData:[]);
const [useHiglass, setUseHiglass] = useState<boolean>(storedUseHiGlass);
const [chromSizesFile, setChromSizesFile] = useState<ChromFile>(storedChromFile?storedChromFile:new ChromFile());
const [fileSets, setFilesets] = useState<fileSet[]>(storedFileSets?storedFileSets:[]);
const [basicInfo, setBasicInfo] = useState<any>(storedBasicInfo?storedBasicInfo:{});
// /** @description a simple wrapper to update local storage with the result of the state */
// const localStorageWrapper = (localStoreString,originalSetter) => ((value)=>{originalSetter(value);localStorage.setItem(localStoreString,JSON.stringify(value))});
// //we wrap the form states setters to store their values to the local storage
// const setProtienRefFiles = localStorageWrapper('storedProtienRefArr', _setProtienRefFiles);
// const setProtienRefFileNames = localStorageWrapper('storedProtienRefFileNames', _setProtienRefFileNames);
useEffect(()=>{
axios.get(apiPaths.getNextID).then(response=>{
console.log(response)
if(response.status==200){
navigate(`${paths.setup}/${response.data.id}/`)
}
})
},[])
//update files and fileNames whenever any of these change
useEffect(()=>{
let i = 0;
let protienRefFilesets = protienRefFiles.map(file=>{
return {
file:file,
name:protienRefFileNames[i++]
}
})
let arr = [toolData, protienRefFilesets];
if(chromSizesFile.isValid()) arr.push([{file:chromSizesFile.file, name:chromSizesFile.fileName}]);
console.log({file:chromSizesFile.file, name:chromSizesFile.fileName});
console.log(chromSizesFile);
setFilesets(arr)
},[protienRefFiles,protienRefFileNames,toolData,chromSizesFile])
//get job id from url
let jobId = params.id ? parseInt(params.id) : undefined;
let once = false;
function canSubmit(e){
if(toolData.some(tool=>tool.resolutions.some(res=>!res.file))) return false;
const formElement = e.target;
const isValid = formElement.checkValidity() && formRef.current.checkValidity();
if(!isValid) return;
let data: jobSetupFormData = {
email: basicInfo.email,
description: basicInfo.description,
title: basicInfo.jobTitle,
higlassToggle:useHiglass === true ? 1 : 0,
}
//add job info
if(!once){
axios.post(apiPaths.jobInfo, data).then((res: any) =>{
if(res.data.status === 200){
console.log(res);
jobId=res.data.id;
} else {
alert("An Unexpected error occurred.")
}
})
once=true;
}
console.log(basicInfo);
return true;
}
/**
* @description the function to handle submitting the form itself
* @param e
*/
function submitJob(e:React.FormEvent<HTMLFormElement>){
e.preventDefault();
if(!canSubmit(e)){
return;
}
return "test";
}
/**
* @description the callback when the form fully submitted
*/
function onSubmitted(){
//submit job
axios.post(apiPaths.jobSubmit, {id:params.id}).then((response) => {
if(response.status===200)
navigate(paths.queue+"/"+params.id);
else {
alert("something went wrong." + response.data.err);
}
});
}
return (
<form onSubmit={submitJob} id="mainForm" ref={formRef}>
<Container fluid className="w-75" style={{ padding: ".5% 0 .5% 0" }}>
<Row>
<BasicJobInfoInputs setData={setBasicInfo}/>
</Row>
<hr/>
<Row>
<ToolFormUploadForm
setToolDataCb={setToolData}
/>
</Row>
<hr/>
<Row>
<ProtienReferenceUploadForm
setRefFileNames={setProtienRefFileNames}
setRefFiles={setProtienRefFiles}
refFiles={protienRefFiles}
refNames={protienRefFileNames}
/>
</Row>
<hr/>
<Row>
<HiglassUploadForm
checked={useHiglass}
setChecked={setUseHiglass}
chromSizesFile={chromSizesFile}
setChromSizesFile={setChromSizesFile}/>
</Row>
</Container>
<FileUploadDisplay
fileSets={fileSets}
id={jobId}
cb={onSubmitted}
conditionalCb={canSubmit}
/>
</form>
)
}