Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 29 additions & 38 deletions app/components/AttachEphemeralIpModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import {
type IpVersion,
} from '~/api'
import { ListboxField } from '~/components/form/fields/ListboxField'
import { ModalForm } from '~/components/form/ModalForm'
import { HL } from '~/components/HL'
import { toPoolItem } from '~/components/PoolListboxItem'
import { useInstanceSelector } from '~/hooks/use-params'
import { addToast } from '~/stores/toast'
import { Message } from '~/ui/lib/Message'
import { Modal } from '~/ui/lib/Modal'
import { ALL_ISH } from '~/util/consts'

type AttachEphemeralIpModalProps = {
Expand Down Expand Up @@ -70,49 +70,40 @@ export const AttachEphemeralIpModal = ({
const form = useForm({ defaultValues: { pool: defaultPool } })
const pool = form.watch('pool')

const disabledReason =
const submitDisabled =
compatibleUnicastPools.length === 0
? 'No compatible unicast pools available for this instance'
: !pool
? 'Select a pool to continue'
: undefined

return (
<Modal isOpen title="Attach ephemeral IP" onDismiss={onDismiss}>
<Modal.Body>
<Modal.Section>
{infoMessage && <Message variant="info" content={infoMessage} />}
<form>
<ListboxField
name="pool"
label="Pool"
control={form.control}
items={sortPools(compatibleUnicastPools).map(toPoolItem)}
disabled={compatibleUnicastPools.length === 0}
placeholder="Select a pool"
noItemsPlaceholder="No pools available"
/>
</form>
{instanceEphemeralIpAttach.error && (
<p className="text-error mt-4">{instanceEphemeralIpAttach.error.message}</p>
)}
</Modal.Section>
</Modal.Body>
<Modal.Footer
actionText="Attach"
disabled={!!disabledReason}
disabledReason={disabledReason}
onAction={() => {
instanceEphemeralIpAttach.mutate({
path: { instance },
query: { project },
body: {
poolSelector: { type: 'explicit', pool },
},
})
}}
onDismiss={onDismiss}
></Modal.Footer>
</Modal>
<ModalForm
form={form}
title="Attach ephemeral IP"
onDismiss={onDismiss}
submitLabel="Attach"
submitError={instanceEphemeralIpAttach.error}
loading={instanceEphemeralIpAttach.isPending}
submitDisabled={submitDisabled}
onSubmit={({ pool }) =>
instanceEphemeralIpAttach.mutate({
path: { instance },
query: { project },
body: { poolSelector: { type: 'explicit', pool } },
})
}
>
{infoMessage && <Message variant="info" content={infoMessage} />}
<ListboxField
name="pool"
label="Pool"
control={form.control}
items={sortPools(compatibleUnicastPools).map(toPoolItem)}
disabled={compatibleUnicastPools.length === 0}
placeholder="Select a pool"
noItemsPlaceholder="No pools available"
/>
</ModalForm>
)
}
2 changes: 1 addition & 1 deletion app/components/AttachFloatingIpModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const AttachFloatingIpModal = ({
body: { kind: 'instance', parent: instance.id },
})
}
submitDisabled={!floatingIp}
submitDisabled={!floatingIp ? 'Select a floating IP' : undefined}
>
<Message
variant="info"
Expand Down
9 changes: 5 additions & 4 deletions app/components/form/ModalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type ModalFormProps<TFieldValues extends FieldValues> = {
form: UseFormReturn<TFieldValues>
children: ReactNode

/** Must be provided with a reason describing why it's disabled */
submitDisabled?: boolean
/** When set, disables the submit button and shows this string as the disabled reason */
submitDisabled?: string
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have this change in mine but I think it might be the right idea. Disabling the button without a message feels kinda junky.

onSubmit: (values: TFieldValues) => void
submitLabel: string

Expand All @@ -35,7 +35,7 @@ export function ModalForm<TFieldValues extends FieldValues>({
form,
children,
onDismiss,
submitDisabled = false,
submitDisabled,
submitError,
title,
onSubmit,
Expand Down Expand Up @@ -77,7 +77,8 @@ export function ModalForm<TFieldValues extends FieldValues>({
onDismiss={onDismiss}
formId={id}
actionText={submitLabel}
disabled={submitDisabled}
disabled={!!submitDisabled}
disabledReason={submitDisabled}
actionLoading={loading || isSubmitting}
/>
</Modal>
Expand Down
Loading