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

Provider/create address #628

Merged
merged 10 commits into from
Nov 11, 2021
2 changes: 1 addition & 1 deletion src/app/shared/models/workshop.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface WorkshopCard {
minAge: number;
photo?: string;
price: number;
providerId: number;
providerId: string;
providerTitle: string;
rating: number;
title: string;
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/store/user.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class DeleteWorkshopById {
}
export class OnDeleteWorkshopSuccess {
static readonly type = '[user] delete Workshop success';
constructor(public payload: string) { }
constructor(public payload: WorkshopCard) { }
}
export class OnDeleteWorkshopFail {
static readonly type = '[user] delete Workshop fail';
Expand Down
10 changes: 6 additions & 4 deletions src/app/shared/store/user.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class UserState {
return this.userWorkshopService
.deleteWorkshop(payload.workshopId)
.pipe(
tap((res) => dispatch(new OnDeleteWorkshopSuccess(payload.title))),
tap((res) => dispatch(new OnDeleteWorkshopSuccess(payload))),
catchError((error: Error) => of(dispatch(new OnDeleteWorkshopFail(error))))
);
}
Expand All @@ -259,8 +259,10 @@ export class UserState {

@Action(OnDeleteWorkshopSuccess)
onDeleteWorkshopSuccess({ dispatch }: StateContext<UserStateModel>, { payload }: OnDeleteWorkshopSuccess): void {
console.log('Workshop is deleted', payload);
console.log('Workshop is deleted', payload.title);
dispatch(new ShowMessageBar({ message: `Дякуємо! Гурток "${payload}" видалено!`, type: 'success' }));
dispatch(new GetWorkshopsByProviderId(payload.providerId));

}

@Action(CreateChildren)
Expand Down Expand Up @@ -424,11 +426,11 @@ export class UserState {
}

@Action(OnUpdateProviderSuccess)
onUpdateProviderSuccess({ dispatch }: StateContext<UserStateModel>, { payload }: OnUpdateProviderSuccess): void {
onUpdateProviderSuccess({ dispatch, patchState }: StateContext<UserStateModel>, { payload }: OnUpdateProviderSuccess): void {
dispatch(new MarkFormDirty(false));
console.log('Provider is updated', payload);
dispatch(new ShowMessageBar({ message: 'Організація успішно відредагована', type: 'success' }));
this.router.navigate(['/personal-cabinet/provider/info']);
dispatch(new GetProfile()).subscribe(() => this.router.navigate(['/personal-cabinet/provider/info']));
}

@Action(UpdateUser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ <h4 class="step-title">Юридична адреса</h4>

</form>
<hr fxLayout='row' fxLayoutAlign='center center' width="500" size="2" color="#E3E3E3" />
<form [formGroup]="ActualAddressFormGroup" fxLayout='column' fxLayoutAlign='center space-between' class="step">
<h4>Фактична адреса</h4>

<div fxLayout='column' fxLayoutAlign='center space-between' class="step">
<h4>Фактична адреса</h4>
<mat-checkbox class="step-label" color="primary" [formControl]="isSameAddressControl">Співпадає з юридичною
адресою
</mat-checkbox>
</div>

<form [formGroup]="ActualAddressFormGroup" fxLayout='column' fxLayoutAlign='center space-between' class="step"
[ngClass]="{'disbale': isSameAddressControl.value}">

<label class="step-label">Область<span class="step-required">*</span></label>
<mat-form-field>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
@import "src/app/shared/styles/create-form.scss";
@import "src/app/shared/styles/validation-form.scss";
@import "src/app/shared/styles/validation-form.scss";
.disbale{
opacity: 0.3;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,61 @@ export class CreateContactsFormComponent implements OnInit {
buildingNumber: new FormControl('', [Validators.required, Validators.pattern(TEXT_WITH_DIGITS_REGEX)]),
city: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)]),
district: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)]),
region: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)])
region: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)]),
});

this.ActualAddressFormGroup = this.formBuilder.group({
street: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)]),
buildingNumber: new FormControl('', [Validators.required, Validators.pattern(TEXT_WITH_DIGITS_REGEX)]),
city: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)]),
district: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)]),
region: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)])
region: new FormControl('', [Validators.required, Validators.pattern(TEXT_REGEX)]),
});
}

ngOnInit(): void {
this.passActualAddressFormGroup.emit(this.ActualAddressFormGroup);
this.passLegalAddressFormGroup.emit(this.LegalAddressFormGroup);
this.isSameAddressControl.valueChanges.subscribe((isSame: boolean) => {
(isSame) ? this.ActualAddressFormGroup.patchValue(this.LegalAddressFormGroup.value) : this.ActualAddressFormGroup.reset();
});
this.initDisableIsSameAddressControl();

this.provider && this.activateEditMode();
}

activateEditMode(): void {
this.LegalAddressFormGroup.addControl('id', this.formBuilder.control(''));
this.ActualAddressFormGroup.addControl('id', this.formBuilder.control(''));

this.isSameAddressControl.setValue(!Boolean(this.provider.actualAddress));

this.LegalAddressFormGroup.patchValue(this.provider.legalAddress, { emitEvent: false });
this.provider.actualAddress && this.ActualAddressFormGroup.patchValue(this.provider.actualAddress, { emitEvent: false });
}

if (this.provider?.actualAddress) {
this.ActualAddressFormGroup.patchValue(this.provider.actualAddress, { emitEvent: false });
} else {
this.ActualAddressFormGroup.patchValue(this.provider.legalAddress, { emitEvent: false });
}
/**
* This method makes input enable if radiobutton value is true and sets the value to the formgroup
*/
initDisableIsSameAddressControl(): void {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would name it as sameAddressHandler
very confusing name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

this.isSameAddressControl.valueChanges.subscribe((isSame: boolean) => {
if (isSame) {
this.ActualAddressFormGroup.reset();
this.ActualAddressFormGroup.disable();
this.ActualAddressFormGroup.clearValidators();
} else {
this.ActualAddressFormGroup.enable();
this.ActualAddressFormGroup.markAsUntouched();
this.setValidators();
this.provider.actualAddress && this.ActualAddressFormGroup.get('id').setValue(this.provider.actualAddress.id);
}
});
}
/**
* This method add validators to teh form-group when actual address is not teh same as legal address
*/
setValidators(): void {
const addValidator = (formControlTitle: string) => (formControlTitle !== 'buildingNumber') ? Validators.pattern(TEXT_REGEX) : Validators.pattern(TEXT_WITH_DIGITS_REGEX);

Object.keys(this.ActualAddressFormGroup.controls).forEach((formControlTitle: string) => {
this.ActualAddressFormGroup.get(formControlTitle).setValidators([addValidator(formControlTitle), Validators.required]);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,18 @@ export class CreateProviderComponent implements OnInit, AfterViewInit, OnDestroy
this.checkValidation(this.PhotoFormGroup);
} else {
const user: User = this.store.selectSnapshot<User>(RegistrationState.user);

let legalAddress: Address;
let actulaAdress: Address;
let provider: Provider;

if (this.editMode) {
legalAddress = new Address(this.ActualAddressFormGroup.value, this.provider.legalAddress);
actulaAdress = new Address(this.LegalAddressFormGroup.value, this.provider.actualAddress);
legalAddress = new Address(this.LegalAddressFormGroup.value, this.provider.legalAddress);
actulaAdress = this.ActualAddressFormGroup.disabled ? null : new Address(this.ActualAddressFormGroup.value, this.provider.actualAddress);
provider = new Provider(this.InfoFormGroup.value, legalAddress, actulaAdress, this.PhotoFormGroup.value, user, this.provider);
this.store.dispatch(new UpdateProvider(provider));
} else {
legalAddress = new Address(this.ActualAddressFormGroup.value);
actulaAdress = new Address(this.LegalAddressFormGroup.value);
legalAddress = new Address(this.LegalAddressFormGroup.value);
actulaAdress = this.ActualAddressFormGroup.disabled ? null : new Address(this.ActualAddressFormGroup.value);
provider = new Provider(this.InfoFormGroup.value, legalAddress, actulaAdress, this.PhotoFormGroup.value, user);
this.store.dispatch(new CreateProvider(provider));
}
Expand Down