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

[Fil-480] Add validation to add storage providers input #166

Merged
merged 2 commits into from
Dec 16, 2024
Merged
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
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])
Comment on lines +105 to +107
Copy link
Collaborator

Choose a reason for hiding this comment

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

@tplocic20 what do you think about DYI form validation and debouncing? Is this the best way given the state of the app or is there some good lightweight lib we could include?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally we would use something like hookForm - but it's too much of a refactor I'm afraid

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
Loading