DataApiEnableSwitchForm.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import type { UseFormReturn } from 'react-hook-form'
  2. import { CardContent, CardFooter, Form, FormControl, FormField, FormItem, Switch } from 'ui'
  3. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  4. import { DataApiDisabledAlert } from './DataApiDisabledAlert'
  5. import type { DataApiFormValues } from './DataApiEnableSwitch.types'
  6. import { FormActions } from '@/components/ui/Forms/FormActions'
  7. export const DataApiEnableSwitchForm = ({
  8. form,
  9. formId,
  10. disabled,
  11. isBusy,
  12. permissionsHelper,
  13. onSubmit,
  14. handleReset,
  15. }: {
  16. form: UseFormReturn<DataApiFormValues>
  17. formId: string
  18. disabled: boolean
  19. isBusy: boolean
  20. permissionsHelper: string | undefined
  21. onSubmit: (values: DataApiFormValues) => void
  22. handleReset: () => void
  23. }) => {
  24. const watchedEnabled = form.watch('enableDataApi')
  25. return (
  26. <Form {...form}>
  27. <form id={formId} onSubmit={form.handleSubmit(onSubmit)}>
  28. <CardContent>
  29. <FormField
  30. control={form.control}
  31. name="enableDataApi"
  32. render={({ field }) => (
  33. <FormItem className="space-y-4">
  34. <FormItemLayout
  35. layout="flex-row-reverse"
  36. label="Enable Data API"
  37. description="When enabled you will be able to use any Briven client library and PostgREST endpoints with any schema configured in the Settings tab."
  38. >
  39. <FormControl>
  40. <Switch
  41. size="large"
  42. disabled={disabled}
  43. checked={field.value}
  44. onCheckedChange={field.onChange}
  45. />
  46. </FormControl>
  47. </FormItemLayout>
  48. {!watchedEnabled && <DataApiDisabledAlert />}
  49. </FormItem>
  50. )}
  51. />
  52. </CardContent>
  53. <CardFooter>
  54. <FormActions
  55. form={formId}
  56. isSubmitting={isBusy}
  57. hasChanges={form.formState.isDirty}
  58. handleReset={handleReset}
  59. disabled={disabled}
  60. helper={permissionsHelper}
  61. />
  62. </CardFooter>
  63. </form>
  64. </Form>
  65. )
  66. }