Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch on mount #8139

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion packages/client/src/components/form/FormFieldGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@
values: IFormSectionData
setFieldValue: (name: string, value: IFormFieldValue) => void
onClick?: () => void
onChange: (e: React.ChangeEvent<any>) => void

Check warning on line 175 in packages/client/src/components/form/FormFieldGenerator.tsx

View workflow job for this annotation

GitHub Actions / test (packages/client)

Unexpected any. Specify a different type
onBlur: (e: React.FocusEvent<any>) => void

Check warning on line 176 in packages/client/src/components/form/FormFieldGenerator.tsx

View workflow job for this annotation

GitHub Actions / test (packages/client)

Unexpected any. Specify a different type
resetDependentSelectValues: (name: string) => void
resetNestedInputValues?: (field: Ii18nFormField) => void
nestedFields?: { [key: string]: JSX.Element[] }
Expand Down Expand Up @@ -490,7 +490,7 @@

const message = intl.formatMessage(label, {
...values,
[fieldDefinition.name]: value as any

Check warning on line 493 in packages/client/src/components/form/FormFieldGenerator.tsx

View workflow job for this annotation

GitHub Actions / test (packages/client)

Unexpected any. Specify a different type
})

return (
Expand Down Expand Up @@ -612,9 +612,12 @@
if (fieldDefinition.type === REDIRECT) {
return (
<RedirectField
to={fieldDefinition.options.url}
form={values}
draft={draftData}
fieldDefinition={fieldDefinition}
fields={fields}
setFieldValue={setFieldValue}
isDisabled={disabled}
/>
)
}
Expand Down
82 changes: 67 additions & 15 deletions packages/client/src/components/form/Redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,86 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { IFormData, IFormSectionData } from '@client/forms'
import {
IFormData,
IFormField,
IFormFieldValue,
IFormSectionData,
IHttpFormField,
Ii18nRedirectFormField
} from '@client/forms'
import { evalExpressionInFieldDefinition } from '@client/forms/utils'
import { getOfflineData } from '@client/offline/selectors'
import { getUserDetails } from '@client/profile/profileSelectors'
import React from 'react'
import React, { useEffect } from 'react'
import { useSelector } from 'react-redux'
import { Redirect } from 'react-router-dom'
import { Link } from '@opencrvs/components'
import { useHttp } from '@client/components/form/http'

export const RedirectField = ({
to,
fields,
form,
draft
draft,
fieldDefinition,
setFieldValue,
isDisabled
}: {
to: string
fields: IFormField[]
form: IFormSectionData
draft: IFormData
fieldDefinition: Ii18nRedirectFormField
setFieldValue: (name: string, value: IFormFieldValue) => void
isDisabled?: boolean
}) => {
const config = useSelector(getOfflineData)
const user = useSelector(getUserDetails)
const {
options: {
url: to,
callback: { params }
}
} = fieldDefinition
const evalPath = evalExpressionInFieldDefinition(
'`' + to + '`',
form,
config,
draft,
user
)
const trigger = fields.find(
(f) => f.name === fieldDefinition.options.callback.trigger
)!
const onChange: Parameters<typeof useHttp>[1] = ({ data, error, loading }) =>
setFieldValue(trigger.name, { loading, data, error } as IFormFieldValue)

const { call } = useHttp<string>(
trigger as IHttpFormField,
onChange,
form,
config,
draft,
user
)

useEffect(() => {
const hasRequestBeenMade = Boolean(form[trigger.name])
function checkParamsPresentInURL() {
const urlParams = new URLSearchParams(window.location.search)
for (const [key, value] of Object.entries(params)) {
if (urlParams.get(key) !== value) {
return false
}
}
return true
}
if (checkParamsPresentInURL() && !hasRequestBeenMade) {
call()
}
}, [call, params, form, trigger])

return (
<Redirect
to={evalExpressionInFieldDefinition(
'`' + to + '`',
form,
config,
draft,
user
)}
/>
<Link disabled={isDisabled}>
<a href={evalPath}>{fieldDefinition.label}</a>
</Link>
)
}
10 changes: 9 additions & 1 deletion packages/client/src/forms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ export interface IRedirectFormField extends IFormFieldBase {
type: typeof REDIRECT
options: {
url: string
callback: {
trigger: string
params: Record<string, string>
}
Comment on lines +734 to +737
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good to comment/document here(/where?) on what these refer to exactly with examples

}
}

Expand Down Expand Up @@ -1238,10 +1242,14 @@ export interface Ii18nButtonFormField extends Ii18nFormFieldBase {
}
}

interface Ii18nRedirectFormField extends Ii18nFormFieldBase {
export interface Ii18nRedirectFormField extends Ii18nFormFieldBase {
type: typeof REDIRECT
options: {
url: string
callback: {
trigger: string
params: Record<string, string>
}
}
}

Expand Down
Loading