Skip to content

Commit

Permalink
fix(ui): initialize the form with the latest values (#1679)
Browse files Browse the repository at this point in the history
* refactor: form skeleton

* update

* update

* rename

* update

* update

* [autofix.ci] apply automated fixes

* cmts

* update

* cache-and-network

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
liangfung and autofix-ci[bot] authored Mar 20, 2024
1 parent 6c5f4f8 commit da76c16
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,12 @@
import React from 'react'

import { Separator } from '@/components/ui/separator'
import { ListSkeleton } from '@/components/skeleton'

import { GeneralFormSection } from './form-section'
import { GeneralNetworkForm } from './network-form'
import { GeneralSecurityForm } from './security-form'

export default function General() {
const [initialized, setInitialized] = React.useState(false)

React.useEffect(() => {
setTimeout(() => {
setInitialized(true)
}, 500)
}, [])

if (!initialized) return <ListSkeleton />

return (
<div className="flex flex-col gap-4">
<GeneralFormSection title="Network">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
FormMessage
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { FormSkeleton } from '@/components/skeleton'

const updateNetworkSettingMutation = graphql(/* GraphQL */ `
mutation updateNetworkSettingMutation($input: NetworkSettingInput!) {
Expand All @@ -41,14 +42,8 @@ interface NetworkFormProps {

const NetworkForm: React.FC<NetworkFormProps> = ({
onSuccess,
defaultValues: propsDefaultValues
defaultValues
}) => {
const defaultValues = React.useMemo(() => {
return {
...(propsDefaultValues || {})
}
}, [propsDefaultValues])

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues
Expand Down Expand Up @@ -112,15 +107,16 @@ const NetworkForm: React.FC<NetworkFormProps> = ({
}

export const GeneralNetworkForm = () => {
const [{ data }, reexecuteQuery] = useNetworkSetting()
const [{ data, stale }, reexecuteQuery] = useNetworkSetting()

const onSuccess = () => {
toast.success('Network configuration is updated')
reexecuteQuery()
}

return (
data && (
<NetworkForm defaultValues={data.networkSetting} onSuccess={onSuccess} />
)
return data && !stale ? (
<NetworkForm defaultValues={data.networkSetting} onSuccess={onSuccess} />
) : (
<FormSkeleton />
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { IconTrash } from '@/components/ui/icons'
import { Input } from '@/components/ui/input'
import { LicenseGuard } from '@/components/license-guard'
import { FormSkeleton } from '@/components/skeleton'

const updateSecuritySettingMutation = graphql(/* GraphQL */ `
mutation updateSecuritySetting($input: SecuritySettingInput!) {
Expand Down Expand Up @@ -226,7 +227,9 @@ function buildListValuesFromField(fieldListValue?: Array<{ value: string }>) {
}

export const GeneralSecurityForm = () => {
const [{ data }, reexecuteQuery] = useQuery({ query: securitySetting })
const [{ data, stale }, reexecuteQuery] = useQuery({
query: securitySetting
})
const onSuccess = () => {
toast.success('Security configuration is updated')
reexecuteQuery()
Expand All @@ -237,7 +240,10 @@ export const GeneralSecurityForm = () => {
data.securitySetting.allowedRegisterDomainList
)
}
return (
data && <SecurityForm defaultValues={defaultValues} onSuccess={onSuccess} />

return data && !stale ? (
<SecurityForm defaultValues={defaultValues} onSuccess={onSuccess} />
) : (
<FormSkeleton />
)
}
11 changes: 11 additions & 0 deletions ee/tabby-ui/components/skeleton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ export const ListSkeleton = () => {
</div>
)
}

export const FormSkeleton = () => {
return (
<div className="flex flex-col gap-3">
<Skeleton className="h-4 w-[20%]" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-[20%]" />
<Skeleton className="h-4 w-full" />
</div>
)
}

0 comments on commit da76c16

Please sign in to comment.