Skip to content
Prev Previous commit
Next Next commit
get form to behave
  • Loading branch information
f0ssel committed Feb 20, 2024
commit 2e10ecc9d2aebcdba4767784a277103de256bcab
30 changes: 22 additions & 8 deletions site/src/modules/resources/PortForwardButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { useDashboard } from "modules/dashboard/useDashboard";
import * as Yup from "yup";
import { FormikContextType, useFormik } from "formik";
import { getFormHelpers } from "utils/formUtils";
import { LoadingButton } from "@mui/lab";

export interface PortForwardButtonProps {
host: string;
Expand Down Expand Up @@ -109,7 +110,7 @@ export const PortForwardButton: FC<PortForwardButtonProps> = (props) => {
const getValidationSchema = (): Yup.AnyObjectSchema =>
Yup.object({
port: Yup.number().required().min(0).max(65535),
sharing_level: Yup.string().required().oneOf(WorkspaceAppSharingLevels),
share_level: Yup.string().required().oneOf(WorkspaceAppSharingLevels),
});

interface PortForwardPopoverViewProps extends PortForwardButtonProps {
Expand All @@ -118,6 +119,8 @@ interface PortForwardPopoverViewProps extends PortForwardButtonProps {
portSharingControlsEnabled: boolean;
}

type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;

export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
host,
workspaceName,
Expand Down Expand Up @@ -153,16 +156,25 @@ export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
} = useMutation(upsertWorkspacePortShare(workspaceID));
const validationSchema = getValidationSchema();
// TODO: do partial here
const form: FormikContextType<UpsertWorkspaceAgentPortShareRequest> =
useFormik<UpsertWorkspaceAgentPortShareRequest>({
const form: FormikContextType<Optional<UpsertWorkspaceAgentPortShareRequest, 'port'>> =
useFormik<Optional<UpsertWorkspaceAgentPortShareRequest, 'port'>>({
initialValues: {
agent_name: agent.name,
port: 0,
port: undefined,
share_level: "authenticated",
},
validationSchema,
onSubmit: async (values) => {
await upsertWorkspacePortShareForm(values);
// we need port to be optional in the initialValues so it appears empty instead of 0.
// because of this we need to reset the form to clear the port field manually.
form.resetForm();
await form.setFieldValue("port", "");

const port = Number(values.port);
await upsertWorkspacePortShareForm({
...values,
port,
});
await sharedPortsQuery.refetch();
},
});
Expand Down Expand Up @@ -437,6 +449,7 @@ export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
size="small"
variant="outlined"
type="number"
value={form.values.port}
/>
<TextField
{...getFieldHelpers("share_level")}
Expand All @@ -451,13 +464,14 @@ export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
Public
</MenuItem>
</TextField>
<Button
<LoadingButton
variant="contained"
type="submit"
disabled={isSubmitting}
loading={isSubmitting}
disabled={!form.isValid}
>
Share Port
</Button>
</LoadingButton>
</Stack>
</form>
</div>
Expand Down