Skip to content

Commit

Permalink
fix(#2403): fix DRep link and description validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Dec 27, 2024
1 parent 840e065 commit 4aaac44
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ changes.
### Fixed

- Fix calculating DRep live voting power [Issue 2460](https://github.com/IntersectMBO/govtool/issues/2460)
- Fix link and description validation [Issue 2403](https://github.com/IntersectMBO/govtool/issues/2403)

### Changed

Expand Down
30 changes: 27 additions & 3 deletions govtool/frontend/src/components/molecules/DRepDataForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Control,
FieldErrors,
UseFormRegister,
UseFormWatch,
useFieldArray,
} from "react-hook-form";
import { Box } from "@mui/material";
Expand All @@ -20,9 +21,10 @@ type Props = {
control: Control<DRepDataFormValues>;
errors: FieldErrors<DRepDataFormValues>;
register: UseFormRegister<DRepDataFormValues>;
watch: UseFormWatch<DRepDataFormValues>;
};

export const DRepDataForm = ({ control, errors, register }: Props) => {
export const DRepDataForm = ({ control, errors, register, watch }: Props) => {
const { t } = useTranslation();
const { isMobile } = useScreenDimension();

Expand Down Expand Up @@ -111,12 +113,14 @@ export const DRepDataForm = ({ control, errors, register }: Props) => {
control={control}
errors={errors}
register={register}
watch={watch}
/>
<ReferencesSection
type="identity"
control={control}
errors={errors}
register={register}
watch={watch}
/>
</Box>
<div>
Expand Down Expand Up @@ -176,13 +180,15 @@ type ReferencesSectionProps = {
control: Control<DRepDataFormValues>;
errors: FieldErrors<DRepDataFormValues>;
register: UseFormRegister<DRepDataFormValues>;
watch: UseFormWatch<DRepDataFormValues>;
};

const ReferencesSection = ({
type,
control,
errors,
register,
watch,
}: ReferencesSectionProps) => {
const { fieldName, jsonldType } = (() => {
// eslint-disable-next-line default-case
Expand Down Expand Up @@ -237,7 +243,16 @@ const ReferencesSection = ({
helpfulTextDataTestId={`${type}-reference-description-${
index + 1
}-hint`}
rules={Rules.LINK_DESCRIPTION}
rules={{
...Rules.LINK_DESCRIPTION,
validate: (value) => {
const isLink = watch(`${fieldName}.${index}.uri`);
if (!value && Boolean(isLink)) {
return t("dRepData.required");
}
return true;
},
}}
/>
<ControlledField.Input
{...register(`${fieldName}.${index}.uri`)}
Expand All @@ -258,7 +273,16 @@ const ReferencesSection = ({
dataTestId={`${type}-reference-url-${index + 1}-input`}
errorDataTestId={`${type}-reference-url-${index + 1}-error`}
helpfulTextDataTestId={`${type}-reference-url-${index + 1}-hint`}
rules={Rules.LINK_URL}
rules={{
...Rules.LINK_URL,
validate: (value) => {
const isDescription = watch(`${fieldName}.${index}.label`);
if (!value && Boolean(isDescription)) {
return t("dRepData.required");
}
return true;
},
}}
/>
</Fragment>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ export const EditDRepForm = ({

return (
<Box sx={{ display: "flex", flexDirection: "column", gap: 8 }}>
<DRepDataForm control={control} errors={errors} register={register} />
<DRepDataForm
control={control}
errors={errors}
register={register}
watch={watch}
/>
<CenteredBoxBottomButtons
onActionButton={onClickContinue}
disableActionButton={isContinueButtonDisabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export const RegisterAsDRepForm = ({

return (
<Box sx={{ display: "flex", flexDirection: "column", gap: 8 }}>
<DRepDataForm control={control} errors={errors} register={register} />
<DRepDataForm
control={control}
errors={errors}
register={register}
watch={watch}
/>
<CenteredBoxBottomButtons
onActionButton={onClickContinue}
disableActionButton={isContinueButtonDisabled}
Expand Down

0 comments on commit 4aaac44

Please sign in to comment.