Skip to content

Commit

Permalink
refactor: add a utility function to get current language
Browse files Browse the repository at this point in the history
  • Loading branch information
stardustmeg committed Jun 11, 2024
1 parent f5bc7a5 commit c271a2d
Show file tree
Hide file tree
Showing 56 changed files with 291 additions and 312 deletions.
14 changes: 7 additions & 7 deletions src/entities/Address/view/AddressView.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import InputFieldModel from '@/entities/InputField/model/InputFieldModel.ts';
import InputModel from '@/shared/Input/model/InputModel.ts';
import getStore from '@/shared/Store/Store.ts';
import { FORM_TEXT, FORM_TEXT_KEYS, INPUT_TYPE } from '@/shared/constants/forms.ts';
import * as FORM_FIELDS from '@/shared/constants/forms/fieldParams.ts';
import { TITLE_TEXT, TITLE_TEXT_KEYS } from '@/shared/constants/forms/text.ts';
import * as FORM_VALIDATION from '@/shared/constants/forms/validationParams.ts';
import { ADDRESS_TYPE, type AddressOptions, type AddressType, SINGLE_ADDRESS } from '@/shared/types/address.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';
import observeCurrentLanguage from '@/shared/utils/observeCurrentLanguage.ts';

import styles from './addressView.module.scss';
Expand Down Expand Up @@ -67,7 +67,7 @@ class AddressView {

const checkBoxText = createBaseElement({
cssClasses: [styles.checkboxText],
innerContent: FORM_TEXT[getStore().getState().currentLanguage].SINGLE_ADDRESS,
innerContent: FORM_TEXT[getCurrentLanguage()].SINGLE_ADDRESS,
tag: 'span',
});
observeCurrentLanguage(checkBoxText, FORM_TEXT, FORM_TEXT_KEYS.SINGLE_ADDRESS);
Expand Down Expand Up @@ -95,8 +95,8 @@ class AddressView {

const textContent =
this.addressType === ADDRESS_TYPE.SHIPPING
? FORM_TEXT[getStore().getState().currentLanguage].DEFAULT_SHIPPING_ADDRESS
: FORM_TEXT[getStore().getState().currentLanguage].DEFAULT_BILLING_ADDRESS;
? FORM_TEXT[getCurrentLanguage()].DEFAULT_SHIPPING_ADDRESS
: FORM_TEXT[getCurrentLanguage()].DEFAULT_BILLING_ADDRESS;
const checkBoxText = createBaseElement({
cssClasses: [styles.checkboxText],
innerContent: textContent,
Expand Down Expand Up @@ -216,15 +216,15 @@ class AddressView {
let key: string;
switch (this.addressType) {
case ADDRESS_TYPE.BILLING:
titleText = TITLE_TEXT[getStore().getState().currentLanguage].BILLING_ADDRESS;
titleText = TITLE_TEXT[getCurrentLanguage()].BILLING_ADDRESS;
key = TITLE_TEXT_KEYS.BILLING_ADDRESS;
break;
case ADDRESS_TYPE.SHIPPING:
titleText = TITLE_TEXT[getStore().getState().currentLanguage].SHIPPING_ADDRESS;
titleText = TITLE_TEXT[getCurrentLanguage()].SHIPPING_ADDRESS;
key = TITLE_TEXT_KEYS.SHIPPING_ADDRESS;
break;
default:
titleText = TITLE_TEXT[getStore().getState().currentLanguage].ADDRESS;
titleText = TITLE_TEXT[getCurrentLanguage()].ADDRESS;
key = TITLE_TEXT_KEYS.ADDRESS;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/entities/Credentials/view/CredentialsView.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import InputFieldModel from '@/entities/InputField/model/InputFieldModel.ts';
import InputModel from '@/shared/Input/model/InputModel.ts';
import getStore from '@/shared/Store/Store.ts';
import { INPUT_TYPE } from '@/shared/constants/forms.ts';
import * as FORM_FIELDS from '@/shared/constants/forms/fieldParams.ts';
import * as FORM_CONSTANT from '@/shared/constants/forms/text.ts';
import * as FORM_VALIDATION from '@/shared/constants/forms/validationParams.ts';
import SVG_DETAILS from '@/shared/constants/svg.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import createSVGUse from '@/shared/utils/createSVGUse.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';
import observeCurrentLanguage from '@/shared/utils/observeCurrentLanguage.ts';

import styles from './credentialsView.module.scss';
Expand Down Expand Up @@ -83,7 +83,7 @@ class CredentialsView {
private createTitle(): HTMLHeadingElement {
this.title = createBaseElement({
cssClasses: [styles.title],
innerContent: FORM_CONSTANT.TITLE_TEXT[getStore().getState().currentLanguage].CREDENTIALS,
innerContent: FORM_CONSTANT.TITLE_TEXT[getCurrentLanguage()].CREDENTIALS,
tag: 'h3',
});
observeCurrentLanguage(this.title, FORM_CONSTANT.TITLE_TEXT, FORM_CONSTANT.TITLE_TEXT_KEYS.CREDENTIALS);
Expand Down
4 changes: 2 additions & 2 deletions src/entities/InputField/view/InputFieldView.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { InputFieldParams, InputParams, LabelParams } from '@/shared/types/form';

import InputModel from '@/shared/Input/model/InputModel.ts';
import getStore from '@/shared/Store/Store.ts';
import observeStore, { selectCurrentLanguage } from '@/shared/Store/observer.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';

import styles from './inputFieldView.module.scss';

Expand Down Expand Up @@ -71,7 +71,7 @@ class InputFieldView {

const updateLabelText = (): void => {
if (labelParams?.text) {
labelText.textContent = labelParams.text[getStore().getState().currentLanguage];
labelText.textContent = labelParams.text[getCurrentLanguage()];
}
};

Expand Down
14 changes: 7 additions & 7 deletions src/entities/Navigation/view/NavigationView.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import LinkModel from '@/shared/Link/model/LinkModel.ts';
import getStore from '@/shared/Store/Store.ts';
import { PAGE_LINK_TEXT, PAGE_LINK_TEXT_KEYS } from '@/shared/constants/links.ts';
import { PAGE_ID } from '@/shared/constants/pages.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';
import observeCurrentLanguage from '@/shared/utils/observeCurrentLanguage.ts';

import styles from './navigationView.module.scss';
Expand Down Expand Up @@ -56,7 +56,7 @@ class NavigationView {
href: PAGE_ID.ABOUT_US_PAGE,
},
classes: [styles.link],
text: PAGE_LINK_TEXT[getStore().getState().currentLanguage].ABOUT,
text: PAGE_LINK_TEXT[getCurrentLanguage()].ABOUT,
});

observeCurrentLanguage(this.toAboutLink.getHTML(), PAGE_LINK_TEXT, PAGE_LINK_TEXT_KEYS.ABOUT);
Expand All @@ -71,7 +71,7 @@ class NavigationView {
href: PAGE_ID.BLOG,
},
classes: [styles.link],
text: PAGE_LINK_TEXT[getStore().getState().currentLanguage].BLOG,
text: PAGE_LINK_TEXT[getCurrentLanguage()].BLOG,
});

observeCurrentLanguage(this.toBlogLink.getHTML(), PAGE_LINK_TEXT, PAGE_LINK_TEXT_KEYS.BLOG);
Expand All @@ -86,7 +86,7 @@ class NavigationView {
href: PAGE_ID.CATALOG_PAGE,
},
classes: [styles.link],
text: PAGE_LINK_TEXT[getStore().getState().currentLanguage].CATALOG,
text: PAGE_LINK_TEXT[getCurrentLanguage()].CATALOG,
});

observeCurrentLanguage(this.toCatalogLink.getHTML(), PAGE_LINK_TEXT, PAGE_LINK_TEXT_KEYS.CATALOG);
Expand All @@ -101,7 +101,7 @@ class NavigationView {
href: PAGE_ID.LOGIN_PAGE,
},
classes: [styles.link],
text: PAGE_LINK_TEXT[getStore().getState().currentLanguage].LOGIN,
text: PAGE_LINK_TEXT[getCurrentLanguage()].LOGIN,
});

observeCurrentLanguage(this.toLoginLink.getHTML(), PAGE_LINK_TEXT, PAGE_LINK_TEXT_KEYS.LOGIN);
Expand All @@ -116,7 +116,7 @@ class NavigationView {
href: PAGE_ID.MAIN_PAGE,
},
classes: [styles.link],
text: PAGE_LINK_TEXT[getStore().getState().currentLanguage].MAIN,
text: PAGE_LINK_TEXT[getCurrentLanguage()].MAIN,
});

observeCurrentLanguage(this.toMainLink.getHTML(), PAGE_LINK_TEXT, PAGE_LINK_TEXT_KEYS.MAIN);
Expand All @@ -131,7 +131,7 @@ class NavigationView {
href: PAGE_ID.REGISTRATION_PAGE,
},
classes: [styles.link],
text: PAGE_LINK_TEXT[getStore().getState().currentLanguage].REGISTRATION,
text: PAGE_LINK_TEXT[getCurrentLanguage()].REGISTRATION,
});

observeCurrentLanguage(this.toRegisterLink.getHTML(), PAGE_LINK_TEXT, PAGE_LINK_TEXT_KEYS.REGISTRATION);
Expand Down
4 changes: 2 additions & 2 deletions src/entities/PersonalInfo/view/PersonalInfoView.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import InputFieldModel from '@/entities/InputField/model/InputFieldModel.ts';
import InputModel from '@/shared/Input/model/InputModel.ts';
import getStore from '@/shared/Store/Store.ts';
import * as FORM_FIELDS from '@/shared/constants/forms/fieldParams.ts';
import * as FORM_CONSTANT from '@/shared/constants/forms/text.ts';
import * as FORM_VALIDATION from '@/shared/constants/forms/validationParams.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';
import observeCurrentLanguage from '@/shared/utils/observeCurrentLanguage.ts';

import './personalInfoView.scss';
Expand Down Expand Up @@ -52,7 +52,7 @@ class PersonalInfoView {
});
const titleElement = createBaseElement({
cssClasses: ['title'],
innerContent: FORM_CONSTANT.TITLE_TEXT[getStore().getState().currentLanguage].PERSONAL,
innerContent: FORM_CONSTANT.TITLE_TEXT[getCurrentLanguage()].PERSONAL,
tag: 'h3',
});
this.personalDataWrapper.append(titleElement);
Expand Down
6 changes: 3 additions & 3 deletions src/entities/Post/view/PostView.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Post } from '@/shared/constants/blog';

import getStore from '@/shared/Store/Store.ts';
import observeStore, { selectCurrentLanguage } from '@/shared/Store/observer.ts';
import { LANGUAGE_CHOICE } from '@/shared/constants/common.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import formattedMinutes from '@/shared/utils/formattedTime.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';

import styles from './post.module.scss';

Expand Down Expand Up @@ -40,7 +40,7 @@ export default class PostView {
}

private createCardInfoHTML(): string {
const ln = getStore().getState().currentLanguage;
const ln = getCurrentLanguage();
const read =
ln === LANGUAGE_CHOICE.EN
? `Read in ${this.post.time.toString()} minutes`
Expand Down Expand Up @@ -76,7 +76,7 @@ export default class PostView {
}

public createPostInfoHtml(): string {
const ln = getStore().getState().currentLanguage;
const ln = getCurrentLanguage();
this.post.content[ln] = this.post.content[ln].replace(/<p>/g, `<p class=${styles.paragraph}>`);
this.post.content[ln] = this.post.content[ln].replace(/<h2>/g, `<h4 class=${styles.headText}>`);
this.post.content[ln] = this.post.content[ln].replace(/<h3>/g, `<h4 class=${styles.headText}>`);
Expand Down
4 changes: 2 additions & 2 deletions src/entities/ProductCard/model/ProductCardModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import ProductPriceModel from '@/entities/ProductPrice/model/ProductPriceModel.t
import WishlistButtonModel from '@/features/WishlistButton/model/WishlistButtonModel.ts';
import getCartModel from '@/shared/API/cart/model/CartModel.ts';
import modal from '@/shared/Modal/model/ModalModel.ts';
import getStore from '@/shared/Store/Store.ts';
import { LANGUAGE_CHOICE } from '@/shared/constants/common.ts';
import { PAGE_ID } from '@/shared/constants/pages.ts';
import { SEARCH_PARAMS_FIELD } from '@/shared/constants/product.ts';
import * as buildPath from '@/shared/utils/buildPathname.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';
import { productAddedToCartMessage } from '@/shared/utils/messageTemplates.ts';
import { showErrorMessage, showSuccessMessage } from '@/shared/utils/userMessage.ts';
import ProductInfoModel from '@/widgets/ProductInfo/model/ProductInfoModel.ts';
Expand Down Expand Up @@ -52,7 +52,7 @@ class ProductCardModel {

private getProductMeta(): AddCartItem {
return {
name: this.params.name[Number(getStore().getState().currentLanguage === LANGUAGE_CHOICE.RU)].value,
name: this.params.name[Number(getCurrentLanguage() === LANGUAGE_CHOICE.RU)].value,
productId: this.params.id,
quantity: 1,
variantId: this.currentVariant.id,
Expand Down
17 changes: 8 additions & 9 deletions src/entities/ProductCard/view/ProductCardView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SVG_DETAILS from '@/shared/constants/svg.ts';
import * as buildPath from '@/shared/utils/buildPathname.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import createSVGUse from '@/shared/utils/createSVGUse.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';

import styles from './productCardView.module.scss';

Expand Down Expand Up @@ -120,12 +121,12 @@ class ProductCardView {

const discountSpan = createBaseElement({
cssClasses: [styles.discountSpan],
innerContent: PRODUCT_INFO_TEXT[getStore().getState().currentLanguage].DISCOUNT_LABEL,
innerContent: PRODUCT_INFO_TEXT[getCurrentLanguage()].DISCOUNT_LABEL,
tag: 'span',
});

observeStore(selectCurrentLanguage, () => {
discountSpan.textContent = PRODUCT_INFO_TEXT[getStore().getState().currentLanguage].DISCOUNT_LABEL;
discountSpan.textContent = PRODUCT_INFO_TEXT[getCurrentLanguage()].DISCOUNT_LABEL;
});

this.discountLabel.append(discountSpan);
Expand Down Expand Up @@ -167,7 +168,7 @@ class ProductCardView {
private createMoreButton(): ButtonModel {
this.moreButton = new ButtonModel({
classes: [styles.moreButton],
text: MORE_TEXT[getStore().getState().currentLanguage].MORE,
text: MORE_TEXT[getCurrentLanguage()].MORE,
});

return this.moreButton;
Expand Down Expand Up @@ -207,32 +208,30 @@ class ProductCardView {
}

private createProductName(): HTMLHeadingElement {
const innerContent = this.params.name[Number(getStore().getState().currentLanguage === LANGUAGE_CHOICE.RU)].value;
const innerContent = this.params.name[Number(getCurrentLanguage() === LANGUAGE_CHOICE.RU)].value;
const productName = createBaseElement({
cssClasses: [styles.productName],
innerContent,
tag: 'h3',
});

observeStore(selectCurrentLanguage, () => {
const textContent = this.params.name[Number(getStore().getState().currentLanguage === LANGUAGE_CHOICE.RU)].value;
const textContent = this.params.name[Number(getCurrentLanguage() === LANGUAGE_CHOICE.RU)].value;
productName.textContent = textContent;
});
return productName;
}

private createProductShortDescription(): HTMLParagraphElement {
const innerContent =
this.params.description[Number(getStore().getState().currentLanguage === LANGUAGE_CHOICE.RU)].value;
const innerContent = this.params.description[Number(getCurrentLanguage() === LANGUAGE_CHOICE.RU)].value;
this.productShortDescription = createBaseElement({
cssClasses: [styles.productShortDescription],
innerContent,
tag: 'p',
});

observeStore(selectCurrentLanguage, () => {
const textContent =
this.params.description[Number(getStore().getState().currentLanguage === LANGUAGE_CHOICE.RU)].value;
const textContent = this.params.description[Number(getCurrentLanguage() === LANGUAGE_CHOICE.RU)].value;
this.productShortDescription.textContent = textContent;
});

Expand Down
23 changes: 12 additions & 11 deletions src/entities/PromocodeSlider/view/PromoCodeSliderView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SVG_DETAILS from '@/shared/constants/svg.ts';
import calcUserBirthDayRange from '@/shared/utils/calcUserBirthDayRange.ts';
import createBaseElement from '@/shared/utils/createBaseElement.ts';
import createSVGUse from '@/shared/utils/createSVGUse.ts';
import getCurrentLanguage from '@/shared/utils/getCurrentLanguage.ts';
import { promoCodeCopiedMessage } from '@/shared/utils/messageTemplates.ts';
import { showErrorMessage, showSuccessMessage } from '@/shared/utils/userMessage.ts';

Expand Down Expand Up @@ -36,7 +37,7 @@ class PromoCodeSliderView {
const start = createBaseElement({
cssClasses: [styles.sliderDateStart],
innerContent: ((): string => {
const promoContent = PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].date;
const promoContent = PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].date;
if (currentUser) {
return `${calcUserBirthDayRange(currentUser.birthDate).start} &mdash;`;
}
Expand All @@ -52,19 +53,19 @@ class PromoCodeSliderView {
cssClasses: [styles.sliderDateEnd],
innerContent: currentUser
? calcUserBirthDayRange(currentUser.birthDate).end
: PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].date.end ?? '',
: PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].date.end ?? '',
tag: 'span',
});

observeStore(selectCurrentLanguage, () => {
if (currentUser) {
start.innerHTML = getStore().getState().isUserLoggedIn
? `${calcUserBirthDayRange(currentUser.birthDate).start} &mdash;`
: PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].date.start;
: PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].date.start;
end.textContent = getStore().getState().isUserLoggedIn ? calcUserBirthDayRange(currentUser.birthDate).end : '';
} else {
start.innerHTML = PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].date.start;
end.textContent = PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].date.end ?? '';
start.innerHTML = PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].date.start;
end.textContent = PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].date.end ?? '';
}
});

Expand Down Expand Up @@ -131,18 +132,18 @@ class PromoCodeSliderView {
cssClasses: [styles.sliderContent],
tag: 'div',
});
slide.classList.add(styles[PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].style]);
slide.classList.add(styles[PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].style]);
const { description, title } = this.createSliderSlideInfo(index);

slide.append(
title,
description,
this.createPromoCodeSpan(PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].promoCode),
this.createPromoCodeSpan(PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].promoCode),
);

observeStore(selectCurrentLanguage, () => {
title.innerHTML = PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].title;
description.innerHTML = PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].description;
title.innerHTML = PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].title;
description.innerHTML = PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].description;
});

if (PROMO_SLIDER_CONTENT[index].en.date.end === null && getStore().getState().isUserLoggedIn) {
Expand All @@ -163,13 +164,13 @@ class PromoCodeSliderView {
} {
const title = createBaseElement({
cssClasses: [styles.sliderTitle],
innerContent: PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].title,
innerContent: PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].title,
tag: 'h3',
});

const description = createBaseElement({
cssClasses: [styles.sliderDescription],
innerContent: PROMO_SLIDER_CONTENT[index][getStore().getState().currentLanguage].description,
innerContent: PROMO_SLIDER_CONTENT[index][getCurrentLanguage()].description,
tag: 'p',
});

Expand Down
Loading

0 comments on commit c271a2d

Please sign in to comment.