|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | +import { useNavigate } from 'react-router-dom'; |
| 3 | +import { Form, Button, Input, Typography, message, Space } from 'antd'; |
| 4 | +import { ArrowLeftOutlined } from '@ant-design/icons'; |
| 5 | +import UploadModel from '../components/components/UploadModel'; |
| 6 | + |
| 7 | +const { Title } = Typography; |
| 8 | + |
| 9 | +interface FieldType { |
| 10 | + email: string; |
| 11 | + experiment: string; |
| 12 | +} |
| 13 | + |
| 14 | +const PublishExperimentPage: React.FC = () => { |
| 15 | + const navigate = useNavigate(); |
| 16 | + const [file, setFile] = useState<File | null>(null); |
| 17 | + const [currentTaskUid, setCurrentTaskUid] = useState<string | null>(null); |
| 18 | + const [isCancelling, setIsCancelling] = useState(false); |
| 19 | + const pollRef = useRef<ReturnType<typeof setInterval> | null>(null); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + if (!currentTaskUid) return; |
| 23 | + |
| 24 | + pollRef.current = setInterval(async () => { |
| 25 | + try { |
| 26 | + const res = await fetch(`http://localhost:8000/api/status/${currentTaskUid}`); |
| 27 | + if (!res.ok) return; |
| 28 | + const data = await res.json(); |
| 29 | + if (data.status === 'SUCCESS' || data.status === 'FAILURE') { |
| 30 | + setCurrentTaskUid(null); |
| 31 | + } |
| 32 | + } catch { |
| 33 | + // ignore transient errors |
| 34 | + } |
| 35 | + }, 5000); |
| 36 | + |
| 37 | + return () => { |
| 38 | + if (pollRef.current) clearInterval(pollRef.current); |
| 39 | + }; |
| 40 | + }, [currentTaskUid]); |
| 41 | + |
| 42 | + const onFinish = async (values: FieldType) => { |
| 43 | + if (!file) { |
| 44 | + message.error('Please upload a model file first.'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + setCurrentTaskUid(null); |
| 49 | + |
| 50 | + const formData = new FormData(); |
| 51 | + formData.append('file', file); |
| 52 | + formData.append('experiment', values.experiment); |
| 53 | + formData.append('email', values.email); |
| 54 | + |
| 55 | + try { |
| 56 | + const res = await fetch('http://localhost:8000/api/ranking-upload', { |
| 57 | + method: 'POST', |
| 58 | + body: formData, |
| 59 | + }); |
| 60 | + if (!res.ok) throw new Error('Upload failed'); |
| 61 | + const data = await res.json(); |
| 62 | + setCurrentTaskUid(data.task_uid); |
| 63 | + message.success(data.message); |
| 64 | + } catch (err) { |
| 65 | + console.error(err); |
| 66 | + message.error('Submission failed'); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + const onCancel = async () => { |
| 71 | + if (!currentTaskUid) return; |
| 72 | + setIsCancelling(true); |
| 73 | + try { |
| 74 | + const res = await fetch(`http://localhost:8000/api/cancel/${currentTaskUid}`, { |
| 75 | + method: 'POST', |
| 76 | + }); |
| 77 | + if (!res.ok) throw new Error('Cancel failed'); |
| 78 | + setCurrentTaskUid(null); |
| 79 | + message.success('Experiment cancelled'); |
| 80 | + } catch (err) { |
| 81 | + console.error(err); |
| 82 | + message.error('Failed to cancel experiment'); |
| 83 | + } finally { |
| 84 | + setIsCancelling(false); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className="min-h-screen bg-gray-50"> |
| 90 | + <div className="max-w-2xl mx-auto px-6 py-10"> |
| 91 | + <Button |
| 92 | + icon={<ArrowLeftOutlined />} |
| 93 | + type="text" |
| 94 | + onClick={() => navigate('/ranking')} |
| 95 | + style={{ marginBottom: 20, paddingLeft: 0, color: '#595959' }} |
| 96 | + > |
| 97 | + Back to Ranking |
| 98 | + </Button> |
| 99 | + |
| 100 | + <Form |
| 101 | + name="publish-experiment" |
| 102 | + layout="vertical" |
| 103 | + onFinish={onFinish} |
| 104 | + autoComplete="off" |
| 105 | + > |
| 106 | + <Title level={2} style={{ textAlign: 'center' }}> |
| 107 | + Publish Experiment |
| 108 | + </Title> |
| 109 | + |
| 110 | + <Form.Item |
| 111 | + label="Your experiment name" |
| 112 | + name="experiment" |
| 113 | + rules={[{ required: true, message: 'Enter experiment name' }]} |
| 114 | + > |
| 115 | + <Input /> |
| 116 | + </Form.Item> |
| 117 | + |
| 118 | + <Form.Item label={null} style={{ textAlign: 'center' }}> |
| 119 | + <UploadModel onFileChange={(f) => setFile(f?.originFileObj || null)} /> |
| 120 | + </Form.Item> |
| 121 | + |
| 122 | + <Form.Item |
| 123 | + label="Email" |
| 124 | + name="email" |
| 125 | + rules={[ |
| 126 | + { required: true, message: 'Enter email address' }, |
| 127 | + { type: 'email', message: 'Enter a valid email address' }, |
| 128 | + ]} |
| 129 | + > |
| 130 | + <Input /> |
| 131 | + </Form.Item> |
| 132 | + |
| 133 | + <Form.Item label={null}> |
| 134 | + <Space> |
| 135 | + <Button type="primary" htmlType="submit"> |
| 136 | + Submit |
| 137 | + </Button> |
| 138 | + {currentTaskUid && ( |
| 139 | + <Button danger onClick={onCancel} loading={isCancelling}> |
| 140 | + Cancel experiment |
| 141 | + </Button> |
| 142 | + )} |
| 143 | + </Space> |
| 144 | + </Form.Item> |
| 145 | + </Form> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ); |
| 149 | +}; |
| 150 | + |
| 151 | +export default PublishExperimentPage; |
0 commit comments