mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 20:48:01 +08:00
form submit
This commit is contained in:
parent
a4049e1ea7
commit
36acd0b9dd
@ -1,5 +1,5 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import React from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import useDocumentTitle from '@/hooks/use-document-title'
|
import useDocumentTitle from '@/hooks/use-document-title'
|
||||||
import { useParams } from 'next/navigation'
|
import { useParams } from 'next/navigation'
|
||||||
@ -7,30 +7,40 @@ import {
|
|||||||
RiCheckboxCircleFill,
|
RiCheckboxCircleFill,
|
||||||
RiInformation2Fill,
|
RiInformation2Fill,
|
||||||
} from '@remixicon/react'
|
} from '@remixicon/react'
|
||||||
|
import Loading from '@/app/components/base/loading'
|
||||||
import AppIcon from '@/app/components/base/app-icon'
|
import AppIcon from '@/app/components/base/app-icon'
|
||||||
import { Markdown } from '@/app/components/base/markdown'
|
import { Markdown } from '@/app/components/base/markdown'
|
||||||
import Button, { } from '@/app/components/base/button'
|
import Button from '@/app/components/base/button'
|
||||||
import DifyLogo from '@/app/components/base/logo/dify-logo'
|
import DifyLogo from '@/app/components/base/logo/dify-logo'
|
||||||
import { UserActionButtonType } from '@/app/components/workflow/nodes/human-input/types'
|
import { UserActionButtonType } from '@/app/components/workflow/nodes/human-input/types'
|
||||||
|
import type { FormInputItem, UserAction } from '@/app/components/workflow/nodes/human-input/types'
|
||||||
|
import { getHumanInputForm, submitHumanInputForm } from '@/service/share'
|
||||||
import cn from '@/utils/classnames'
|
import cn from '@/utils/classnames'
|
||||||
|
|
||||||
import { MOCK_DATA } from './mock'
|
export type FormData = {
|
||||||
|
site: any
|
||||||
const success = true
|
form_content: string
|
||||||
|
inputs: FormInputItem[]
|
||||||
const expired = true
|
user_actions: UserAction[]
|
||||||
|
timeout: number
|
||||||
const submitted = true
|
timeout_unit: 'hour' | 'day'
|
||||||
|
}
|
||||||
|
|
||||||
const FormContent = () => {
|
const FormContent = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
const { token } = useParams()
|
const { token } = useParams<{ token: string }>()
|
||||||
useDocumentTitle('')
|
useDocumentTitle('')
|
||||||
|
|
||||||
const { site } = MOCK_DATA.site
|
const [isLoading, setIsLoading] = useState(true)
|
||||||
const { form_content, user_actions, timeout, timeout_unit } = MOCK_DATA
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
|
const [formData, setFormData] = useState<FormData>()
|
||||||
|
const [inputs, setInputs] = useState({})
|
||||||
|
const [success, setSuccess] = useState(false)
|
||||||
|
const [expired, setExpired] = useState(false)
|
||||||
|
const [submitted, setSubmitted] = useState(false)
|
||||||
|
|
||||||
|
const site = formData?.site.site
|
||||||
|
|
||||||
const getButtonStyle = (style: UserActionButtonType) => {
|
const getButtonStyle = (style: UserActionButtonType) => {
|
||||||
if (style === UserActionButtonType.Primary)
|
if (style === UserActionButtonType.Primary)
|
||||||
@ -43,6 +53,43 @@ const FormContent = () => {
|
|||||||
return 'ghost'
|
return 'ghost'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getForm = async (token: string) => {
|
||||||
|
try {
|
||||||
|
const data = await getHumanInputForm(token)
|
||||||
|
setFormData(data)
|
||||||
|
setIsLoading(false)
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const submit = async (actionID: string) => {
|
||||||
|
setIsSubmitting(true)
|
||||||
|
try {
|
||||||
|
await submitHumanInputForm(token, { inputs, action: actionID })
|
||||||
|
setSuccess(true)
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
setExpired(true)
|
||||||
|
setSubmitted(true)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
setIsSubmitting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getForm(token)
|
||||||
|
}, [token])
|
||||||
|
|
||||||
|
if (isLoading || !formData) {
|
||||||
|
return (
|
||||||
|
<Loading type='app' />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
return (
|
return (
|
||||||
<div className={cn('flex h-full w-full flex-col items-center justify-center')}>
|
<div className={cn('flex h-full w-full flex-col items-center justify-center')}>
|
||||||
@ -138,16 +185,21 @@ const FormContent = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className='h-0 w-full grow overflow-y-auto'>
|
<div className='h-0 w-full grow overflow-y-auto'>
|
||||||
<div className='border-components-divider-subtle rounded-[20px] border bg-chat-bubble-bg p-4 shadow-lg backdrop-blur-sm'>
|
<div className='border-components-divider-subtle rounded-[20px] border bg-chat-bubble-bg p-4 shadow-lg backdrop-blur-sm'>
|
||||||
<Markdown content={form_content || ''} />
|
<Markdown content={formData.form_content || ''} />
|
||||||
<div className='flex flex-wrap gap-1 py-1'>
|
<div className='flex flex-wrap gap-1 py-1'>
|
||||||
{user_actions.map((action: any) => (
|
{formData.user_actions.map((action: any) => (
|
||||||
<Button key={action.id} variant={getButtonStyle(action.button_style) as any}>
|
<Button
|
||||||
|
key={action.id}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
variant={getButtonStyle(action.button_style) as any}
|
||||||
|
onClick={() => submit(action.id)}
|
||||||
|
>
|
||||||
{action.title}
|
{action.title}
|
||||||
</Button>
|
</Button>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
<div className='system-xs-regular mt-1 text-text-tertiary'>
|
<div className='system-xs-regular mt-1 text-text-tertiary'>
|
||||||
{timeout_unit === 'day' ? t('share.humanInput.timeoutDay', { count: timeout }) : t('share.humanInput.timeoutHour', { count: timeout })}
|
{formData.timeout_unit === 'day' ? t('share.humanInput.timeoutDay', { count: formData.timeout }) : t('share.humanInput.timeoutHour', { count: formData.timeout })}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className='flex flex-row-reverse px-2 py-3'>
|
<div className='flex flex-row-reverse px-2 py-3'>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user