|
| 1 | +import { makeStyles } from "@material-ui/core/styles" |
| 2 | +import TextField from "@material-ui/core/TextField" |
| 3 | +import Autocomplete from "@material-ui/lab/Autocomplete" |
| 4 | +import { Template, TemplateVersion, Workspace } from "api/typesGenerated" |
| 5 | +import { FormFooter } from "components/FormFooter/FormFooter" |
| 6 | +import { Pill } from "components/Pill/Pill" |
| 7 | +import { Stack } from "components/Stack/Stack" |
| 8 | +import { useFormik } from "formik" |
| 9 | +import { FC } from "react" |
| 10 | +import { useTranslation } from "react-i18next" |
| 11 | +import { createDayString } from "util/createDayString" |
| 12 | +import * as Yup from "yup" |
| 13 | + |
| 14 | +const validationSchema = Yup.object({ |
| 15 | + versionId: Yup.string().required(), |
| 16 | +}) |
| 17 | + |
| 18 | +export const WorkspaceChangeVersionForm: FC<{ |
| 19 | + isLoading: boolean |
| 20 | + workspace: Workspace |
| 21 | + template: Template |
| 22 | + versions: TemplateVersion[] |
| 23 | + onSubmit: (versionId: string) => void |
| 24 | + onCancel: () => void |
| 25 | +}> = ({ isLoading, workspace, template, versions, onSubmit, onCancel }) => { |
| 26 | + const styles = useStyles() |
| 27 | + const { t } = useTranslation("workspaceChangeVersionPage") |
| 28 | + const formik = useFormik({ |
| 29 | + initialValues: { |
| 30 | + versionId: workspace.latest_build.template_version_id, |
| 31 | + }, |
| 32 | + validationSchema, |
| 33 | + onSubmit: ({ versionId }) => onSubmit(versionId), |
| 34 | + }) |
| 35 | + const autocompleteValue = versions.find( |
| 36 | + (version) => version.id === formik.values.versionId, |
| 37 | + ) |
| 38 | + |
| 39 | + return ( |
| 40 | + <form onSubmit={formik.handleSubmit}> |
| 41 | + <Stack direction="column" spacing={3}> |
| 42 | + <Stack |
| 43 | + direction="row" |
| 44 | + spacing={2} |
| 45 | + className={styles.workspace} |
| 46 | + alignItems="center" |
| 47 | + > |
| 48 | + <div className={styles.workspaceIcon}> |
| 49 | + <img src={workspace.template_icon} alt="" /> |
| 50 | + </div> |
| 51 | + <Stack direction="column" spacing={0.5}> |
| 52 | + <span className={styles.workspaceName}>{workspace.name}</span> |
| 53 | + |
| 54 | + <span className={styles.workspaceDescription}> |
| 55 | + {workspace.template_display_name.length > 0 |
| 56 | + ? workspace.template_display_name |
| 57 | + : workspace.template_name} |
| 58 | + </span> |
| 59 | + </Stack> |
| 60 | + </Stack> |
| 61 | + |
| 62 | + <Autocomplete |
| 63 | + id="workspaceVersion" |
| 64 | + disableClearable |
| 65 | + options={versions.slice().reverse()} |
| 66 | + value={autocompleteValue} |
| 67 | + onChange={async (_event, value) => { |
| 68 | + if (value) { |
| 69 | + await formik.setFieldValue("versionId", value.id) |
| 70 | + } |
| 71 | + }} |
| 72 | + renderInput={(params) => ( |
| 73 | + <TextField |
| 74 | + {...params} |
| 75 | + label={t("labels.workspaceVersion")} |
| 76 | + variant="outlined" |
| 77 | + fullWidth |
| 78 | + /> |
| 79 | + )} |
| 80 | + getOptionLabel={(version: TemplateVersion) => version.name} |
| 81 | + renderOption={(version: TemplateVersion) => ( |
| 82 | + <div className={styles.menuItem}> |
| 83 | + <div> |
| 84 | + <div>{version.name}</div> |
| 85 | + <div className={styles.versionDescription}> |
| 86 | + {t("labels.createdBy")} {version.created_by.username}{" "} |
| 87 | + {createDayString(version.created_at)} |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + |
| 91 | + {template.active_version_id === version.id && ( |
| 92 | + <Pill |
| 93 | + type="success" |
| 94 | + text={t("labels.active")} |
| 95 | + className={styles.activePill} |
| 96 | + /> |
| 97 | + )} |
| 98 | + </div> |
| 99 | + )} |
| 100 | + /> |
| 101 | + </Stack> |
| 102 | + |
| 103 | + <FormFooter |
| 104 | + onCancel={onCancel} |
| 105 | + isLoading={isLoading} |
| 106 | + submitLabel={t("labels.submit")} |
| 107 | + /> |
| 108 | + </form> |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +const useStyles = makeStyles((theme) => ({ |
| 113 | + workspace: { |
| 114 | + padding: theme.spacing(2.5, 3), |
| 115 | + borderRadius: theme.shape.borderRadius, |
| 116 | + backgroundColor: theme.palette.background.paper, |
| 117 | + border: `1px solid ${theme.palette.divider}`, |
| 118 | + }, |
| 119 | + |
| 120 | + workspaceName: { |
| 121 | + fontSize: theme.spacing(2), |
| 122 | + }, |
| 123 | + |
| 124 | + workspaceDescription: { |
| 125 | + fontSize: theme.spacing(1.75), |
| 126 | + color: theme.palette.text.secondary, |
| 127 | + }, |
| 128 | + |
| 129 | + workspaceIcon: { |
| 130 | + width: theme.spacing(5), |
| 131 | + lineHeight: 1, |
| 132 | + |
| 133 | + "& img": { |
| 134 | + width: "100%", |
| 135 | + }, |
| 136 | + }, |
| 137 | + |
| 138 | + menuItem: { |
| 139 | + paddingTop: theme.spacing(1), |
| 140 | + paddingBottom: theme.spacing(1), |
| 141 | + position: "relative", |
| 142 | + width: "100%", |
| 143 | + }, |
| 144 | + |
| 145 | + versionDescription: { |
| 146 | + fontSize: theme.spacing(1.5), |
| 147 | + color: theme.palette.text.secondary, |
| 148 | + }, |
| 149 | + |
| 150 | + activePill: { |
| 151 | + position: "absolute", |
| 152 | + top: theme.spacing(2), |
| 153 | + right: theme.spacing(2), |
| 154 | + }, |
| 155 | +})) |
0 commit comments