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

feat(RSS-ECOMM-4_33): implement editing address #322

Merged
merged 8 commits into from
May 29, 2024
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
5 changes: 4 additions & 1 deletion src/app/styles/variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
// 1440px
body.light {
// colors
--black: #000;
--white: #fff;
--white-tr: #ffffffa9;
--black: #000;
--noble-white-100: #f5f5f5;
--noble-white-200: #f0f0f0;
--noble-gray-200: #eaeaea;
Expand Down Expand Up @@ -86,6 +86,8 @@
--noble-gray-600: #cdcdcd;
--noble-gray-700: #b0b0b0;
--noble-gray-800: #c4c4c4;
--noble-gray-tr-800: #acacacbd;
--noble-gray-900: #1d1d1d;
--noble-gray-1000: #1a1a1ab5;
--red-power-600: #d0302f;
--steam-green-300: #46a35880;
Expand All @@ -94,6 +96,7 @@
--steam-green-500: #b6f09c;
--steam-green-700: #70d27a;
--steam-green-800: #46a358;
--steam-green-900: #46a3581a;
--steam-green-gr-800: #c5e1cb2b;
}
}
18 changes: 16 additions & 2 deletions src/entities/Address/model/AddressModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AddressModel {

private view: AddressView;

constructor(addressType: AddressType, options: AddressOptions) {
constructor(options: AddressOptions, addressType: AddressType = ADDRESS_TYPE.GENERAL) {
this.addressType = addressType;
this.view = new AddressView(addressType, options);
this.init();
Expand All @@ -26,9 +26,22 @@ class AddressModel {

public getAddressData(personalData: PersonalData): Address {
const store = getStore().getState();
let country: string;

switch (this.addressType) {
case ADDRESS_TYPE.BILLING:
country = store.billingCountry;
break;
case ADDRESS_TYPE.SHIPPING:
country = store.shippingCountry;
break;
default:
country = store.defaultCountry;
break;
}
const addressData: Address = {
city: formattedText(this.view.getCityField().getView().getValue()),
country: this.addressType === ADDRESS_TYPE.BILLING ? store.billingCountry : store.shippingCountry,
country,
email: personalData.email,
firstName: personalData.firstName,
id: '',
Expand All @@ -38,6 +51,7 @@ class AddressModel {
streetName: formattedText(this.view.getStreetField().getView().getValue()),
streetNumber: '',
};

return addressData;
}

Expand Down
28 changes: 19 additions & 9 deletions src/entities/Address/view/AddressView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,29 @@ class AddressView {
}

private createTitle(): HTMLHeadingElement {
let titleText: string;
let key: string;
switch (this.addressType) {
case ADDRESS_TYPE.BILLING:
titleText = TITLE_TEXT[getStore().getState().currentLanguage].BILLING_ADDRESS;
key = TITLE_TEXT_KEYS.BILLING_ADDRESS;
break;
case ADDRESS_TYPE.SHIPPING:
titleText = TITLE_TEXT[getStore().getState().currentLanguage].SHIPPING_ADDRESS;
key = TITLE_TEXT_KEYS.SHIPPING_ADDRESS;
break;
default:
titleText = TITLE_TEXT[getStore().getState().currentLanguage].ADDRESS;
key = TITLE_TEXT_KEYS.ADDRESS;
break;
}

const title = createBaseElement({
cssClasses: [styles.title],
innerContent:
this.addressType === ADDRESS_TYPE.SHIPPING
? TITLE_TEXT[getStore().getState().currentLanguage].SHIPPING_ADDRESS
: TITLE_TEXT[getStore().getState().currentLanguage].BILLING_ADDRESS,
innerContent: titleText,
tag: 'h3',
});
observeCurrentLanguage(
title,
TITLE_TEXT,
this.addressType === ADDRESS_TYPE.SHIPPING ? TITLE_TEXT_KEYS.SHIPPING_ADDRESS : TITLE_TEXT_KEYS.BILLING_ADDRESS,
);
observeCurrentLanguage(title, TITLE_TEXT, key);
return title;
}

Expand Down
1 change: 1 addition & 0 deletions src/entities/Address/view/addressView.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

.shippingAddressWrapper {
grid-row: 3;
animation: show 0.5s ease-in forwards;
}

.billingAddressWrapper {
Expand Down
63 changes: 28 additions & 35 deletions src/entities/UserAddress/model/UserAddressModel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/* eslint-disable max-lines-per-function */
import type { Address, User } from '@/shared/types/user.ts';

import AddressEditModel from '@/features/AddressEdit/model/AddressEditModel.ts';
import getCustomerModel, { CustomerModel } from '@/shared/API/customer/model/CustomerModel.ts';
import ConfirmModel from '@/shared/Confirm/model/ConfirmModel.ts';
import EventMediatorModel from '@/shared/EventMediator/model/EventMediatorModel.ts';
import LoaderModel from '@/shared/Loader/model/LoaderModel.ts';
import modal from '@/shared/Modal/model/ModalModel.ts';
import serverMessageModel from '@/shared/ServerMessage/model/ServerMessageModel.ts';
import MEDIATOR_EVENT from '@/shared/constants/events.ts';
import { ADDRESS_TYPE, type AddressTypeType } from '@/shared/constants/forms.ts';
import { MESSAGE_STATUS, SERVER_MESSAGE_KEYS } from '@/shared/constants/messages.ts';
import { LOADER_SIZE } from '@/shared/constants/sizes.ts';
import showErrorMessage from '@/shared/utils/userMessage.ts';

import UserAddressView from '../view/UserAddressView.ts';
Expand All @@ -30,6 +29,23 @@ class UserAddressModel {
this.setLabelClickHandler();
}

private async deleteAddress(address: Address): Promise<void> {
try {
const user = await getCustomerModel().getCurrentUser();
if (user) {
try {
await getCustomerModel().editCustomer([CustomerModel.actionRemoveAddress(address)], user);
EventMediatorModel.getInstance().notify(MEDIATOR_EVENT.REDRAW_USER_ADDRESSES, '');
serverMessageModel.showServerMessage(SERVER_MESSAGE_KEYS.ADDRESS_DELETED, MESSAGE_STATUS.SUCCESS);
} catch (error) {
showErrorMessage(error);
}
}
} catch (error) {
showErrorMessage(error);
}
}

private async handleAddressType(user: User, activeType: AddressTypeType, inactive: boolean): Promise<void> {
const customerModel = getCustomerModel();

Expand Down Expand Up @@ -63,22 +79,15 @@ class UserAddressModel {
} else {
switch (activeType) {
case ADDRESS_TYPE.BILLING:
case ADDRESS_TYPE.DEFAULT_BILLING:
await customerModel.editCustomer([CustomerModel.actionRemoveBillingAddress(this.currentAddress)], user);
break;

case ADDRESS_TYPE.SHIPPING:
case ADDRESS_TYPE.DEFAULT_SHIPPING:
await customerModel.editCustomer([CustomerModel.actionRemoveShippingAddress(this.currentAddress)], user);
break;

// TBD Check the adding/removing default address
// case ADDRESS_TYPE.DEFAULT_BILLING:
// await customerModel.editCustomer([CustomerModel.actionEditDefaultBillingAddress(null)], user);
// break;

// case ADDRESS_TYPE.DEFAULT_SHIPPING:
// await customerModel.editCustomer([CustomerModel.actionEditDefaultShippingAddress(null)], user);
// break;

default:
break;
}
Expand All @@ -102,30 +111,14 @@ class UserAddressModel {
this.view
.getDeleteButton()
.getHTML()
.addEventListener('click', async () => {
const loader = new LoaderModel(LOADER_SIZE.SMALL).getHTML();
this.view.getDeleteButton().getHTML().append(loader);
try {
const user = await getCustomerModel().getCurrentUser();
if (user) {
try {
await getCustomerModel().editCustomer([CustomerModel.actionRemoveAddress(address)], user);
EventMediatorModel.getInstance().notify(MEDIATOR_EVENT.REDRAW_USER_ADDRESSES, '');
serverMessageModel.showServerMessage(SERVER_MESSAGE_KEYS.ADDRESS_DELETED, MESSAGE_STATUS.SUCCESS);
} catch (error) {
showErrorMessage(error);
}
}
} catch (error) {
showErrorMessage(error);
} finally {
loader.remove();
}
.addEventListener('click', () => {
const confirmModel = new ConfirmModel(() => this.deleteAddress(address));
modal.setContent(confirmModel.getHTML());
modal.show();
});
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
private setEditButtonHandler(_address: Address): void {
private setEditButtonHandler(address: Address): void {
this.view
.getEditButton()
.getHTML()
Expand All @@ -135,11 +128,11 @@ class UserAddressModel {
if (!user) {
return;
}
const newAddressEditForm = new AddressEditModel(address, user).getHTML();
modal.show();
modal.setContent(newAddressEditForm);
} catch (error) {
showErrorMessage(error);
} finally {
modal.hide();
}
});
}
Expand Down
13 changes: 9 additions & 4 deletions src/entities/UserAddress/view/UserAddressView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ class UserAddressView {
}

this.view.append(addressText, this.editButton.getHTML(), this.deleteButton.getHTML());

observeStore(selectCurrentLanguage, () => {
addressText.innerText = this.createAddressText(getStore().getState().currentLanguage);
});

return this.view;
}

Expand All @@ -139,20 +144,20 @@ class UserAddressView {
let addressType = null;
switch (ActiveType) {
case ADDRESS_TYPE.BILLING:
addressType = this.createLabel(ActiveType, [styles.billing], TOOLTIP_TEXT_KEYS.EDIT_BILLING_ADDRESS);
addressType = this.createLabel(ActiveType, [styles.billing], TOOLTIP_TEXT_KEYS.SWITCH_BILLING_ADDRESS);
this.view.append(addressType);
break;

case ADDRESS_TYPE.SHIPPING:
addressType = this.createLabel(ActiveType, [styles.shipping], TOOLTIP_TEXT_KEYS.EDIT_SHIPPING_ADDRESS);
addressType = this.createLabel(ActiveType, [styles.shipping], TOOLTIP_TEXT_KEYS.SWITCH_SHIPPING_ADDRESS);
this.view.append(addressType);
break;

case ADDRESS_TYPE.DEFAULT_BILLING:
addressType = this.createLabel(
ActiveType,
[styles.defaultBilling],
TOOLTIP_TEXT_KEYS.EDIT_DEFAULT_BILLING_ADDRESS,
TOOLTIP_TEXT_KEYS.SWITCH_DEFAULT_BILLING_ADDRESS,
);
this.view.append(addressType);
break;
Expand All @@ -161,7 +166,7 @@ class UserAddressView {
addressType = this.createLabel(
ActiveType,
[styles.defaultShipping],
TOOLTIP_TEXT_KEYS.EDIT_DEFAULT_SHIPPING_ADDRESS,
TOOLTIP_TEXT_KEYS.SWITCH_DEFAULT_SHIPPING_ADDRESS,
);
this.view.append(addressType);
break;
Expand Down
3 changes: 2 additions & 1 deletion src/features/AddressAdd/model/AddressAddModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AddressAddModel {

constructor(type: AddressType, options: Record<string, boolean>) {
this.addressType = type;
this.newAddress = new AddressModel(this.addressType, options);
this.newAddress = new AddressModel(options, this.addressType);
this.init();
}

Expand Down Expand Up @@ -148,6 +148,7 @@ class AddressAddModel {
const cancelButton = this.view.getCancelButton().getHTML();
cancelButton.addEventListener('click', () => {
modal.hide();
modal.removeContent();
});
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/AddressAdd/view/AddressAddView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AddressAddView {
private createSaveChangesButton(): ButtonModel {
this.saveChangesButton = new ButtonModel({
classes: [styles.saveChangesButton],
text: BUTTON_TEXT[getStore().getState().currentLanguage].SAVE_CHANGES,
text: BUTTON_TEXT[getStore().getState().currentLanguage].ADD_ADDRESS,
});
this.saveChangesButton.setDisabled();
return this.saveChangesButton;
Expand Down
Loading