Skip to content

Commit 545fbb8

Browse files
authored
fix: clear deposit on save and continue (#5598)
1 parent 0a2b691 commit 545fbb8

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

api/src/decorators/validate-listing-deposit.decorator.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,23 @@ export class DepositValueConstraint implements ValidatorConstraintInterface {
3535
return true;
3636
}
3737

38-
// Verify if both fields are either filled or empty
39-
const areRangeFieldsInvalid =
40-
(this.isFieldEmpty(depositMin) && !this.isFieldEmpty(depositMax)) ||
41-
(!this.isFieldEmpty(depositMin) && this.isFieldEmpty(depositMax));
42-
4338
if (value === DepositTypeEnum.fixedDeposit) {
44-
return (
45-
this.isFieldEmpty(depositMin) &&
46-
this.isFieldEmpty(depositMax) &&
47-
!this.isFieldEmpty(depositValue)
48-
);
39+
return this.isFieldEmpty(depositMin) && this.isFieldEmpty(depositMax);
40+
}
41+
if (value === DepositTypeEnum.depositRange) {
42+
return this.isFieldEmpty(depositValue);
4943
}
5044

51-
return this.isFieldEmpty(depositValue) && !areRangeFieldsInvalid;
45+
// If no Deposit type is selected then validate that it's either just depositValue or just the range values
46+
return (
47+
this.isFieldEmpty(depositValue) ||
48+
(this.isFieldEmpty(depositMin) && this.isFieldEmpty(depositMax))
49+
);
5250
}
5351
defaultMessage(args?: ValidationArguments): string {
5452
const value = args.value as DepositTypeEnum;
55-
const { listingType } = args.object as Listing;
5653

57-
if (!listingType || listingType === ListingTypeEnum.regulated) {
58-
return 'The "depositMin" and "depositMax" fields must be filled';
59-
} else if (value === DepositTypeEnum.fixedDeposit) {
54+
if (value === DepositTypeEnum.fixedDeposit) {
6055
return 'When deposit is of type "fixedDeposit" the "depositValue" must be filled and the "depositMin"|"depositMax" fields must be null';
6156
}
6257
return 'When deposit is of type "depositRange" the "depositMin" and "depositMax" fields must be filled and "depositValue" must be null';

sites/partners/src/components/listings/PaperListingForm/sections/AdditionalFees.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import SectionWithGrid from "../../../shared/SectionWithGrid"
1414
import styles from "../ListingForm.module.scss"
1515
import { GridRow } from "@bloom-housing/ui-seeds/src/layout/Grid"
16+
import { ListingContext } from "../../ListingContext"
1617

1718
type AdditionalFeesProps = {
1819
existingUtilities: ListingUtilities
@@ -22,6 +23,7 @@ type AdditionalFeesProps = {
2223
const AdditionalFees = (props: AdditionalFeesProps) => {
2324
const formMethods = useFormContext()
2425
const { doJurisdictionsHaveFeatureFlagOn } = useContext(AuthContext)
26+
const listing = useContext(ListingContext)
2527
// eslint-disable-next-line @typescript-eslint/unbound-method
2628
const { register, watch, errors, clearErrors, setValue } = formMethods
2729

@@ -57,6 +59,14 @@ const AdditionalFees = (props: AdditionalFeesProps) => {
5759
}
5860
}, [enableUtilitiesIncluded, setValue])
5961

62+
// After submitting the deposit max, min, and value can be removed via AdditionalMetadataFormatter.
63+
// On a save and continue flow the values need to be updated in the form
64+
useEffect(() => {
65+
setValue("depositMax", listing?.depositMax)
66+
setValue("depositMin", listing?.depositMin)
67+
setValue("depositValue", listing?.depositValue)
68+
}, [listing?.depositMax, listing?.depositMin, listing?.depositValue, setValue])
69+
6070
const showAsNonRegulated =
6171
enableNonRegulatedListings && listingType === EnumListingListingType.nonRegulated
6272

0 commit comments

Comments
 (0)