Commit 35c56237 authored by Joel's avatar Joel

feat: url selector

parent 236cc6f5
'use client'
import type { FC } from 'react'
import React, { useCallback } from 'react'
import { MethodEnum } from '../types'
import Selector from '../../_base/components/selector'
import { ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows'
const MethodOptions = [
{ label: 'GET', value: MethodEnum.get },
{ label: 'POST', value: MethodEnum.post },
{ label: 'HEAD', value: MethodEnum.head },
{ label: 'PATCH', value: MethodEnum.patch },
{ label: 'PUT', value: MethodEnum.put },
{ label: 'DELETE', value: MethodEnum.delete },
]
type Props = {
readonly: boolean
method: MethodEnum
onMethodChange: (method: MethodEnum) => void
url: string
onUrlChange: (url: string) => void
}
const ApiInput: FC<Props> = ({
readonly,
method,
onMethodChange,
url,
onUrlChange,
}) => {
const handleUrlChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
onUrlChange(e.target.value)
}, [onUrlChange])
return (
<div className='flex items-center h-8 rounded-lg bg-white border border-gray-200 shadow-xs'>
<Selector
value={method}
onChange={onMethodChange}
options={MethodOptions}
trigger={
<div className='h-8 shrink-0 flex items-center px-2.5 cursor-pointer border-r border-black/5'>
<div className='w-12 pl-0.5 leading-[18px] text-xs font-medium text-gray-900 uppercase'>{method}</div>
<ChevronDown className='ml-1 w-3.5 h-3.5 text-gray-700' />
</div>
}
popupClassName='top-[34px] w-[108px]'
showChecked
readonly={readonly}
/>
<input
type='text'
readOnly={readonly}
value={url}
onChange={handleUrlChange}
className='w-0 grow h-6 leading-6 px-2.5 border-0 text-gray-900 text-[13px] placeholder:text-gray-400 focus:outline-none'
/>
</div>
)
}
export default React.memo(ApiInput)
import { useCallback, useState } from 'react'
import type { KeyValue } from '../types'
const strToKeyValueList = (value: string) => {
return value.split('\n').map((item) => {
const [key, value] = item.split(':')
return { key: key.trim(), value: value.trim() }
})
}
const useKeyValueList = (value: string) => {
const [list, setList] = useState<KeyValue[]>(value ? strToKeyValueList(value) : [])
const addItem = useCallback(() => {
setList(prev => [...prev, { key: '', value: '' }])
}, [])
return {
list,
setList,
addItem,
}
}
export default useKeyValueList
......@@ -2,12 +2,12 @@ import type { FC } from 'react'
import { useTranslation } from 'react-i18next'
import useConfig from './use-config'
import { mockData } from './mock'
import ApiInput from './components/api-input'
import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
import Field from '@/app/components/workflow/nodes/_base/components/field'
import AddButton from '@/app/components/base/button/add-button'
import Split from '@/app/components/workflow/nodes/_base/components/split'
import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
const i18nPrefix = 'workflow.nodes.http'
const Panel: FC = () => {
......@@ -18,6 +18,8 @@ const Panel: FC = () => {
inputs,
handleVarListChange,
handleAddVariable,
handleMethodChange,
handleUrlChange,
} = useConfig(mockData)
return (
......@@ -38,7 +40,13 @@ const Panel: FC = () => {
<Field
title={t(`${i18nPrefix}.api`)}
>
API
<ApiInput
readonly={readOnly}
method={inputs.method}
onMethodChange={handleMethodChange}
url={inputs.url}
onUrlChange={handleUrlChange}
/>
</Field>
<Field
title={t(`${i18nPrefix}.headers`)}
......
import type { CommonNodeType, Variable } from '@/app/components/workflow/types'
export enum MethodEnum {
get = 'get',
post = 'post',
head = 'head',
patch = 'patch',
put = 'put',
delete = 'delete',
}
export type KeyValue = {
key: string
value: string
}
export type HttpNodeType = CommonNodeType & {
variables: Variable[]
method: string
method: MethodEnum
url: string
headers: string
params: string
......
import { useState } from 'react'
import { useCallback, useState } from 'react'
import useVarList from '../_base/hooks/use-var-list'
import type { HttpNodeType } from './types'
import type { HttpNodeType, MethodEnum } from './types'
import useKeyValueList from './hooks/use-key-value-list'
const useConfig = (initInputs: HttpNodeType) => {
const [inputs, setInputs] = useState<HttpNodeType>(initInputs)
......@@ -10,10 +10,31 @@ const useConfig = (initInputs: HttpNodeType) => {
setInputs,
})
const handleMethodChange = useCallback((method: MethodEnum) => {
setInputs(prev => ({
...prev,
method,
}))
}, [])
const handleUrlChange = useCallback((url: string) => {
setInputs(prev => ({
...prev,
url,
}))
}, [])
const { list: headers, setList: setHeaders, addItem: addHeader } = useKeyValueList(inputs.headers)
return {
inputs,
handleVarListChange,
handleAddVariable,
handleMethodChange,
handleUrlChange,
headers,
setHeaders,
addHeader,
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment