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

fix(#2403): fix DRep link and description validation #2581

Merged
Show file tree
Hide file tree
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
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
Loading