Skip to content

Commit

Permalink
[Fil-480] Add validation to add storage providers input (#166)
Browse files Browse the repository at this point in the history
* Add validation to add storage provider input
---------

Co-authored-by: Filip Lelek <[email protected]>
  • Loading branch information
Filip-L and filip-neti authored Dec 16, 2024
1 parent a65bfc8 commit 36d8b66
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/components/cards/dialogs/allowedSps/AllowedSps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const AllowedSPs: React.FC<ComponentProps> = ({
const [maxDeviation, setMaxDeviation] = useState<string>(initDeviation ?? '')
const [data, setData] = useState<string[]>([''])
const [initData, setInitData] = useState<string[]>([''])
const [errors, setErrors] = useState([''])
const [debounceTimer, setDebounceTimer] = useState<number>(0)

const { getClientSPs, getClientConfig } = useWallet()

Expand Down Expand Up @@ -83,14 +85,39 @@ export const AllowedSPs: React.FC<ComponentProps> = ({
})
}

const isValidInput = (input: string): boolean => {
const regex = /^(f0)?\d+$/
return regex.test(input)
}

const handleInputChange = (index: number, value: string): void => {
const newData = [...data]
const updatedErrors = [...errors]
newData[index] = value
value = value.trim()
clearTimeout(debounceTimer)
const timer = window.setTimeout(() => {
value = value.trim()
if (!value) {
updatedErrors[index] = 'Value cannot be empty'
setErrors([...updatedErrors])
return
} else if (!isValidInput(value)) {
updatedErrors[index] = 'Invalid storage provider id'
setErrors([...updatedErrors])
return
}
updatedErrors[index] = ''
setErrors(updatedErrors)
}, 200)

setData(newData)
checkIsDirty(newData)
setDebounceTimer(timer)
}

const hasErrors = errors.some((error) => error)

const handleAddItem = (): void => {
const newData = [...data, '']

Expand All @@ -99,20 +126,24 @@ export const AllowedSPs: React.FC<ComponentProps> = ({
}

const handleRemoveItem = (index: number): void => {
const updatedErrors = [...errors]
const newData = data.filter((_, i) => i !== index)
updatedErrors.splice(index, 1)
setErrors(updatedErrors)
setData(newData)
checkIsDirty(newData)
}

const handleSubmit = async (): Promise<void> => {
try {
setApiCalling(true)
const added = data.filter(
(item) => !availableAllowedSPs?.includes(item),
const cleanedData = data.map((item) => item.replace('f0', ''))
const added = cleanedData.filter(
(item) => !availableAllowedSPs?.includes(item) && item.length,
) ?? ['']

const removed: string[] = availableAllowedSPs?.filter(
(item) => !data.includes(item),
(item) => !cleanedData.includes(item),
) ?? ['']

const afterAdd = [...(availableAllowedSPs ?? ['']), ...added]
Expand Down Expand Up @@ -216,6 +247,8 @@ export const AllowedSPs: React.FC<ComponentProps> = ({
handleInputChange(index, e.target.value)
}}
className="flex-1"
error={!!errors[index]}
helperText={errors[index]}
/>
<IconButton
onClick={() => {
Expand Down Expand Up @@ -250,7 +283,7 @@ export const AllowedSPs: React.FC<ComponentProps> = ({
</Button>

<Button
disabled={isApiCalling || !isDirty}
disabled={isApiCalling || !isDirty || hasErrors}
onClick={() => {
void handleSubmit()
}}
Expand Down

0 comments on commit 36d8b66

Please sign in to comment.