|
| 1 | +import JobFormButtons from '@components/create-job/JobFormButtons'; |
| 2 | +import CostAndDuration from '@components/create-job/steps/CostAndDuration'; |
| 3 | +import Deployment from '@components/create-job/steps/Deployment'; |
| 4 | +import Specifications from '@components/create-job/steps/Specifications'; |
| 5 | +import { APPLICATION_TYPES } from '@data/applicationTypes'; |
| 6 | +import { BOOLEAN_TYPES } from '@data/booleanTypes'; |
| 7 | +import { DYNAMIC_ENV_TYPES } from '@data/dynamicEnvTypes'; |
| 8 | +import { PIPELINE_INPUT_TYPES } from '@data/pipelineInputTypes'; |
| 9 | +import { PLUGIN_SIGNATURE_TYPES } from '@data/pluginSignatureTypes'; |
| 10 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 11 | +import { DeploymentContextType, useDeploymentContext } from '@lib/contexts/deployment'; |
| 12 | +import { jobSchema } from '@schemas/index'; |
| 13 | +import JobFormHeaderInterface from '@shared/jobs/JobFormHeaderInterface'; |
| 14 | +import { DraftJob, GenericDraftJob, JobType, NativeDraftJob, ServiceDraftJob } from '@typedefs/deeploys'; |
| 15 | +import { FieldErrors, FormProvider, useForm } from 'react-hook-form'; |
| 16 | +import { useNavigate } from 'react-router-dom'; |
| 17 | +import z from 'zod'; |
| 18 | + |
| 19 | +const STEPS: { |
| 20 | + title: string; |
| 21 | + validationName?: string; |
| 22 | +}[] = [ |
| 23 | + { title: 'Specifications', validationName: 'specifications' }, |
| 24 | + { title: 'Cost & Duration', validationName: 'costAndDuration' }, |
| 25 | + { title: 'Deployment', validationName: 'deployment' }, |
| 26 | +]; |
| 27 | + |
| 28 | +export default function DraftEditFormWrapper({ |
| 29 | + job, |
| 30 | + onSubmit, |
| 31 | +}: { |
| 32 | + job: DraftJob; |
| 33 | + onSubmit: (data: z.infer<typeof jobSchema>) => Promise<void>; |
| 34 | +}) { |
| 35 | + const { step } = useDeploymentContext() as DeploymentContextType; |
| 36 | + |
| 37 | + const navigate = useNavigate(); |
| 38 | + |
| 39 | + const cloneKeyValueEntries = (entries?: Array<{ key: string; value: string }>) => |
| 40 | + entries ? entries.map((entry) => ({ key: entry.key, value: entry.value })) : []; |
| 41 | + |
| 42 | + const cloneDynamicEnvEntries = ( |
| 43 | + entries?: Array<{ |
| 44 | + key: string; |
| 45 | + values: Array<{ type: (typeof DYNAMIC_ENV_TYPES)[number]; value: string }>; |
| 46 | + }>, |
| 47 | + ) => |
| 48 | + entries |
| 49 | + ? entries.map((entry) => ({ |
| 50 | + key: entry.key, |
| 51 | + values: entry.values.map((value) => ({ type: value.type, value: value.value })), |
| 52 | + })) |
| 53 | + : []; |
| 54 | + |
| 55 | + const cloneDeploymentNodes = (nodes?: Array<{ address: string }>) => |
| 56 | + nodes && nodes.length ? nodes.map((node) => ({ address: node.address })) : [{ address: '' }]; |
| 57 | + |
| 58 | + const getBaseSchemaDefaults = () => ({ |
| 59 | + jobType: job.jobType, |
| 60 | + specifications: { |
| 61 | + applicationType: job.specifications.applicationType ?? APPLICATION_TYPES[0], |
| 62 | + targetNodesCount: job.specifications.targetNodesCount, |
| 63 | + jobTags: [...(job.specifications.jobTags ?? [])], |
| 64 | + nodesCountries: [...(job.specifications.nodesCountries ?? [])], |
| 65 | + }, |
| 66 | + costAndDuration: { |
| 67 | + duration: job.costAndDuration.duration, |
| 68 | + paymentMonthsCount: job.costAndDuration.paymentMonthsCount, |
| 69 | + }, |
| 70 | + deployment: { |
| 71 | + autoAssign: job.deployment.autoAssign ?? true, |
| 72 | + targetNodes: cloneDeploymentNodes(job.deployment.targetNodes), |
| 73 | + spareNodes: cloneDeploymentNodes(job.deployment.spareNodes), |
| 74 | + allowReplicationInTheWild: job.deployment.allowReplicationInTheWild ?? true, |
| 75 | + enableTunneling: job.deployment.enableTunneling ?? BOOLEAN_TYPES[0], |
| 76 | + tunnelingToken: job.deployment.tunnelingToken, |
| 77 | + tunnelingLabel: job.deployment.tunnelingLabel, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + const getGenericSchemaDefaults = () => { |
| 82 | + const baseDefaults = getBaseSchemaDefaults(); |
| 83 | + const genericJob = job as GenericDraftJob; |
| 84 | + const deployment = genericJob.deployment; |
| 85 | + |
| 86 | + return { |
| 87 | + ...baseDefaults, |
| 88 | + specifications: { |
| 89 | + ...baseDefaults.specifications, |
| 90 | + containerType: genericJob.specifications.containerType, |
| 91 | + gpuType: genericJob.specifications.gpuType, |
| 92 | + }, |
| 93 | + deployment: { |
| 94 | + ...baseDefaults.deployment, |
| 95 | + jobAlias: deployment.jobAlias, |
| 96 | + deploymentType: |
| 97 | + deployment.deploymentType.type === 'image' |
| 98 | + ? { |
| 99 | + ...deployment.deploymentType, |
| 100 | + crUsername: deployment.deploymentType.crUsername ?? '', |
| 101 | + crPassword: deployment.deploymentType.crPassword ?? '', |
| 102 | + } |
| 103 | + : { |
| 104 | + ...deployment.deploymentType, |
| 105 | + workerCommands: deployment.deploymentType.workerCommands.map((command) => ({ |
| 106 | + command: command.command, |
| 107 | + })), |
| 108 | + username: deployment.deploymentType.username ?? '', |
| 109 | + accessToken: deployment.deploymentType.accessToken ?? '', |
| 110 | + }, |
| 111 | + port: deployment.port ?? '', |
| 112 | + restartPolicy: deployment.restartPolicy, |
| 113 | + imagePullPolicy: deployment.imagePullPolicy, |
| 114 | + envVars: cloneKeyValueEntries(deployment.envVars), |
| 115 | + dynamicEnvVars: cloneDynamicEnvEntries(deployment.dynamicEnvVars), |
| 116 | + volumes: cloneKeyValueEntries(deployment.volumes), |
| 117 | + fileVolumes: deployment.fileVolumes |
| 118 | + ? deployment.fileVolumes.map((fileVolume) => ({ |
| 119 | + name: fileVolume.name, |
| 120 | + mountingPoint: fileVolume.mountingPoint, |
| 121 | + content: fileVolume.content, |
| 122 | + })) |
| 123 | + : [], |
| 124 | + }, |
| 125 | + } as z.infer<typeof jobSchema>; |
| 126 | + }; |
| 127 | + |
| 128 | + const getNativeSchemaDefaults = () => { |
| 129 | + const baseDefaults = getBaseSchemaDefaults(); |
| 130 | + const nativeJob = job as NativeDraftJob; |
| 131 | + const deployment = nativeJob.deployment; |
| 132 | + |
| 133 | + return { |
| 134 | + ...baseDefaults, |
| 135 | + specifications: { |
| 136 | + ...baseDefaults.specifications, |
| 137 | + workerType: nativeJob.specifications.workerType, |
| 138 | + gpuType: nativeJob.specifications.gpuType, |
| 139 | + }, |
| 140 | + deployment: { |
| 141 | + ...baseDefaults.deployment, |
| 142 | + jobAlias: deployment.jobAlias, |
| 143 | + port: deployment.port ?? '', |
| 144 | + pluginSignature: deployment.pluginSignature ?? PLUGIN_SIGNATURE_TYPES[0], |
| 145 | + customParams: cloneKeyValueEntries(deployment.customParams), |
| 146 | + pipelineParams: cloneKeyValueEntries(deployment.pipelineParams), |
| 147 | + pipelineInputType: deployment.pipelineInputType ?? PIPELINE_INPUT_TYPES[0], |
| 148 | + pipelineInputUri: deployment.pipelineInputUri, |
| 149 | + chainstoreResponse: deployment.chainstoreResponse ?? BOOLEAN_TYPES[1], |
| 150 | + }, |
| 151 | + } as z.infer<typeof jobSchema>; |
| 152 | + }; |
| 153 | + |
| 154 | + const getServiceSchemaDefaults = () => { |
| 155 | + const baseDefaults = getBaseSchemaDefaults(); |
| 156 | + const serviceJob = job as ServiceDraftJob; |
| 157 | + const deployment = serviceJob.deployment; |
| 158 | + |
| 159 | + return { |
| 160 | + ...baseDefaults, |
| 161 | + specifications: { |
| 162 | + ...baseDefaults.specifications, |
| 163 | + containerType: serviceJob.specifications.containerType, |
| 164 | + }, |
| 165 | + deployment: { |
| 166 | + ...baseDefaults.deployment, |
| 167 | + jobAlias: deployment.jobAlias, |
| 168 | + envVars: cloneKeyValueEntries(deployment.envVars), |
| 169 | + dynamicEnvVars: cloneDynamicEnvEntries(deployment.dynamicEnvVars), |
| 170 | + volumes: cloneKeyValueEntries(deployment.volumes), |
| 171 | + serviceReplica: deployment.serviceReplica ?? '', |
| 172 | + }, |
| 173 | + } as z.infer<typeof jobSchema>; |
| 174 | + }; |
| 175 | + |
| 176 | + const getDefaultSchemaValues = () => { |
| 177 | + switch (job.jobType) { |
| 178 | + case JobType.Generic: |
| 179 | + return getGenericSchemaDefaults(); |
| 180 | + |
| 181 | + case JobType.Native: |
| 182 | + return getNativeSchemaDefaults(); |
| 183 | + |
| 184 | + case JobType.Service: |
| 185 | + return getServiceSchemaDefaults(); |
| 186 | + |
| 187 | + default: |
| 188 | + return {}; |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + const form = useForm<z.infer<typeof jobSchema>>({ |
| 193 | + resolver: zodResolver(jobSchema), |
| 194 | + mode: 'onTouched', |
| 195 | + defaultValues: getDefaultSchemaValues(), |
| 196 | + }); |
| 197 | + |
| 198 | + const onError = (errors: FieldErrors<z.infer<typeof jobSchema>>) => { |
| 199 | + console.log(errors); |
| 200 | + }; |
| 201 | + |
| 202 | + return ( |
| 203 | + <FormProvider {...form}> |
| 204 | + <form onSubmit={form.handleSubmit(onSubmit, onError)} key={`${job.jobType}-draft-edit`}> |
| 205 | + <div className="w-full flex-1"> |
| 206 | + <div className="mx-auto max-w-[626px]"> |
| 207 | + <div className="col gap-6"> |
| 208 | + <JobFormHeaderInterface |
| 209 | + steps={STEPS.map((step) => step.title)} |
| 210 | + onCancel={() => { |
| 211 | + navigate(-1); |
| 212 | + }} |
| 213 | + > |
| 214 | + <div className="big-title">Edit Job Draft</div> |
| 215 | + </JobFormHeaderInterface> |
| 216 | + |
| 217 | + {step === 0 && <Specifications />} |
| 218 | + {step === 1 && <CostAndDuration />} |
| 219 | + {step === 2 && <Deployment />} |
| 220 | + |
| 221 | + <JobFormButtons steps={STEPS} cancelLabel="Project" /> |
| 222 | + </div> |
| 223 | + </div> |
| 224 | + </div> |
| 225 | + </form> |
| 226 | + </FormProvider> |
| 227 | + ); |
| 228 | +} |
0 commit comments