Skip to content

Commit

Permalink
chore: update production deploy with implemented features from [sprin…
Browse files Browse the repository at this point in the history
…t_4] (#334)

* chore(RSS-ECOMM-4_00): update sprint number (#309)

chore: update sprint number

* refactor(RSS-ECOMM-4_30): update styles (#311)

refactor(RSS-ECOMM-3_58): separate styles in mixins (#310)

* docs(RSS-ECOMM-2_37): Update pipeline scripts and CD for a new sprint (#199)

* docs: update the PR template

* chore: add a job to generate PR titles

* chore: remove unnecessary paths from configs

* chore: remove unused package

* chore: remove a job from CI

* refactor(RSS-ECOMM-3_27): remove redundancies (#202)

* refactor: remove redundant tag constant

* refactor: replace redundant event names

* feat(RSS-ECOMM-3_03): add filters (#201)

* feat: add size product count request

* feat: add filter, edit sort, edit get product request

* feat: add search options

* refactor(RSS-ECOMM-2_53): registration form (#204)

* refactor: separate validators into separate functions

* refactor: separate address logic into Address component

* refactor: change getIsValid method

* fix: replace type for authCustomer method

* refactor: separate disabling into ButtonView

* fix: pattern for validate

* feat: types for Address component

* feat: create messageTemplate

* refactor: create every inputField separately

* refactor: create every inputField separately

* fix: header styles

* fix(RSS-ECOMM-2_54): visualisation forms (#206)

* fix: layout registration form

* fix: layout login form

* fix: layout address

* fix: layout countryChoice

* fix: layout login and registration pages

* refactor: remove the mandatory field symbol from field signatures in forms

* feat: add formatted text function

* fix: remove formatting from fields that should not be formatted

* feat: formatting of address fields

* refactor(RSS-ECOMM-2_55): router component (#207)

* refactor: rewrite Router component

* fix: navigation links

* fix: choosing country

* refactor: country lang choice based on input

* refactor: visual layout of pages

* fix: update store

* refactor: remove redundant code

* fix: styles

* feat: add init method for App component

* Apply suggestions from code review



---------



* feat(RSS-ECOMM-2_99): edit api client (#209)

feat: add save token, add auth and anonym client

* feat(RSS-ECOMM-3_29): pages lazy loading (#210)

* feat: implement lazy loading for pages

* fix: add a catch block

* feat(RSS-ECOMM-3_30): implement language switch (#212)

* feat: implement switching language on registration checkboxes

* feat: switch titles language

* feat: implement label text switching

* test: observe current language

* feat: add language choice to user messages

* fix: showPasswordElement position

* refactor: update text content in Russian

* feat: add a logo for language switch

* fix(RSS-ECOMM-3_31): rerouting to main (#213)

fix: rerouting to main

* feat(RSS-ECOMM-3_25): create pages components (#214)

* feat: add base page components

* docs: update PR template

* fix: link styles

* fix: button naming

* Apply suggestions from code review



---------



* feat(RSS-ECOMM-3_19): implement routing for all pages (#215)

feat: implement routing for all pages

* feat(RSS-ECOMM-3_21): implement navigation (#217)

* feat: implement navigation to catalog and cart pages

* fix: footer styles

* fix: header styles

* feat: implement navigation to other pages

* feat: check auth user to profile page

* feat: check auth user with init app

* feat: visually profile button

* feat(RSS-ECOMM-3_34): add catalog component (#218)

feat: add catalog component

* feat(RSS-ECOMM-3_33):  implement burger menu (#221)

feat: implement burger menu

* feat(RSS-ECOMM-3_01): implement product list (#223)

* refactor: api types

* feat: implement product list

* feat(RSS-ECOMM-3_36): implement theme app (#225)

* feat: implement app theme change

* feat: save current app theme in store

* fix: update store actions

* feat(RSS-ECOMM-3_02): display price and discount (#226)

* fix: api types for product

* feat: display price and discount on product card

* fix: rename SizeType

* feat(RSS-ECOMM-3_99): add blog page (#228)

* feat: add blog page

* feat: add blog page, posts, widget

* refactor: delete comment and magic numbers

* fix: mock data

* feat(RSS-ECOMM-3_03): implement filtering product list (#227)

* feat: expand loder options

* fix: styles product card

* feat: implement saving Set into LS Co-authored-by: Meg G. <[email protected]>

* feat: get and draw product items

* feat: create ProductFilters component

* feat: implement price filter

* feat: implement size filter

* feat: filters reset button

* fix: burger styles

* fix: lost title

* feat(RSS-ECOMM-3_98): add all data in one request (#229)

* feat: add all data in one request

* feat: add refresh token, fix auth, refactor client

* fix(RSS-ECOMM-3_40): handle popstate event (#231)

fix: handle popstate event

* feat(RSS-ECOMM-3_37): implement switching user locale (#233)

* feat: add a user notification on switching language

* feat: implement switching locale

* feat(RSS-ECOMM-3_97): add filter Object, make filer with OR concatenation (#230)

* feat: add filter Object, make filer with OR concatenation

* refactor: delete comment

* feat: add categoryCount and SizeCount to Products response

* feat(RSS-ECOMM-3_03): implement meta filters (#235)

* fix: router

* feat: implement meta filters

* Apply suggestions from code review



---------



* test(RSS-ECOMM-3_24): cover main codebase (#236)

test(RSS-ECOMM-3_24): cover main codebase

* refactor(RSS-ECOMM-3_41): logic input components (#237)

* feat: create method to add data attributes for input

* refactor: remove unused data for creating input fields

* refactor: delete unused options for address component

* feat(RSS-ECOMM-3_14): display user info (#234)

* feat: add user info menu

* refactor: replace recurring code with a util function

* feat: add common utils

* feat: display user info

* refactor: remove the language switch

* chore: complete merge

* refactor: remove sending request until user is logged in on refresh

* refactor: move language choice from buttons constants

* feat: add modal

* fix: remove unused var

* fix: modal styles

* feat: create separate entities for user info and user addresses

* refactor: remove redundant code

* refactor: add comments

* feat(RSS-ECOMM-3_04): implement product sorts (#238)

* chore: integrate plop to create component folder

* chore: add templates folder

* docs: update readme

* chore: update configs

* chore: update configs

* feat: implement products sorting

* Apply suggestions from code review



* fix: after merge

---------



* feat(RSS-ECOMM-3_96): add cart and shop list (#239)

* feat: add cart, refactor root, delete product from store

* feat: add and delete cart item

* feat: add anonymId to store, merge anonym cart after login user

* feat: create create for new customer, getCart

* feat: add shoppingList

* feat: create shopping list after registration

* feat: getCategories function

* feat(RSS-ECOMM-3_95): add price range (#240)

feat: add price range

* feat(RSS-ECOMM-3_42): transfer styles to clamp (#242)

* refactor: transfer to clamp

* refactor: remove background with unused pictures

* refactor: remove commented out code

* chore(RSS-ECOMM-3_43): modernise the creation of a component via plopfile (#243)

chore: modernise the creation of a component via plopfile

* chore(RSS-ECOMM-3_44): update display select list in terminal (#244)

chore: update display select list in terminal

* fix(RSS-ECOMM-3_45): styles (#245)

* fix: sorting wrapper style

* fix: error and burger z-indices

* chore(RSS-ECOMM-3_45): update readme (#246)

* docs: update readme

* chore: update vite configs to minify build code

* fix: typo

* feat(RSS-ECOMM-3_05): implement product search (#247)

* feat: create product search component

* feat: implement product search

* feat(RSS-ECOMM-3_94): update user addresses; save token to store (#248)

* feat: move save token from cook to store

* feat: add shipping billing address to user

* feat(RSS-ECOMM-3_46): implement custom scroll (#249)

feat: implement custom scroll

* fix(RSS-ECOMM-3_47): display products with selected filters (#250)

fix: display products with selected filters

* feat(RSS-ECOMM-3_94): add id filter for products (#251)

feat: add id filter for products

* feat(RSS-ECOMM-3_48): implement buttons to product card (#252)

* feat: implement buttons to product card

* feat: getters

* feat(RSS-ECOMM-3_48): translate filters, sorting and search (#254)

feat: translate filters, sorting and search

* feat(RSS-ECOMM-3_49): add product to cart and wishlist (#255)

feat: add product to cart and wishlist

* fix(RSS-ECOMM-3_50): incoming product card parameters (#256)

* fix: incoming product card parameters

* fix: catalog styles

* feat(RSS-ECOMM-3_93): add getCurrentUser (#257)

feat: add getCurrentUser

* fix(RSS-ECOMM-3_51): router component (#258)

* fix: routing to detailed product page

* fix: redirect to default page

* feat(RSS-ECOMM-3_92): add parent to category (#260)

* refactor(RSS-ECOMM-3_52): router component (#261)

* refactor: get rid of asynchronous methods in the router

* refactor: add / at the end of each route

* feat: add search queries to url

* fix: router test

* fix: update netlify

* fix: update netlify

* fix: update netlify

* fix: update netlify

* fix: router

* fix: router

* feat(RSS-ECOMM-3_91): update auth client after password change (#263)

feat: update auth client after password change

* feat(RSS-ECOMM-3_91): add slug for category and product (#262)

feat: add slug for category and product

* feat(RSS-ECOMM-3_15): update user info (#253)

* refactor: separate PersonalInfoComponent

* refactor: separate credentials into a single component

* refactor: remove user from store

* feat: add a request before showing personal info

* feat: implement changing password

* fix: switch language for anonymous user

* refactor: create one instance of modal content

* fix: navigation styles

* fix: redirect to main on logo

* feat(RSS-ECOMM-3_08): implement breadcrumb navigation (#289)

* feat: implement event mediator component

* feat: implement search params in url

* refactor: remove user from state

* feat: implement breadcrumbs

* Update src/shared/constants/sorting.ts



* fix: prettier

---------



* feat(RSS-ECOMM-3_09): display product details on product page (#294)

feat: display product details on product page

* feat(RSS_ECOMM-3_90): add key request (#295)

feat: add key request

* feat(RSS-ECOMM-3_89): add adapt Product (#296)

feat: add adapt Product

* feat(RSS-ECOMM-3_10): implement slider on product page (#297)

feat: implement slider on product page

* fix(RSS-ECOMM-3_53): routing on product page (#298)

* fix: routing on product page

* fix: search params

* feat(RSS-ECOMM-3_13): implement product modal slider (#299)

feat: implement product modal slider

* feat(RSS-ECOMM-3_90) cart (#300)

* feat: add cart page

* feat: badge for cart

* feat: add actions for cart products

* feat: merge cart after registration

* feat(RSS-ECOMM-3_16): implement personal info edit (#301)

* feat: close modal on click on background

* feat: implement personal info edit

* feat: implement changing component styles

* refactor(RSS-ECOMM-3_54): router component (#303)

* refactor: make Router singleton

* refactor: remove redundant router field

* feat: separate product price in component

* refactor: styles

* Apply suggestions from code review



* fix: prettier

---------



* feat(RSS-ECOMM-4_08): implement cart button to product page (#304)

* feat: add modal window closing on click outside the window

* feat: add a modal window with product details

* feat: add loader in product modal slider

* feat: implement cart button to product page

* Apply suggestions from code review



---------



* fix(RSS-ECOMM-3_55): deploy (#305)

fix: deploy

* feat(RSS-ECOMM-3_56): add progress bar to server message (#307)

feat: add progress bar to server message

* refactor: separate style buttons in mixin

* fix: delete navigation from footer

---------

Co-authored-by: Max <[email protected]>
Co-authored-by: Yuliya Kursevich <[email protected]>

* feat(RSS-ECOMM-4_99): add navigate and languages to footer (#312)

* feat: add navigate and languages

* feat: translate for social and pay

* feat: styles

* feat(RSS-ECOMM-4_31): adaptive (#314)

* feat: add scroll top on every page

* feat: adaptive noUiSlider

* feat: add adaptive up to 380px

* feat(RSS-ECOMM-4_98): add level to product (#313)

feat: add level to product

* feat(RSS-ECOMM-4_97): add address actions (#315)

feat: add address actions

* feat(RSS-ECOMM-4_96): add total count for products (#316)

feat: add total count for products

* feat(RSS-ECOMM-4_03): implement effecting loading products (#318)

* feat: separate control search params in router

* feat: add pagination component

* refactor: remove redundant fields in store

* feat(RSS-ECOMM-4_32): display difficultyPlants (#319)

* feat: display difficultyPlants

* fix: position difficulty plants

* feat(RSS-ECOMM-3_18): implement addresses management (#317)

* feat: implement adding address

* feat: implement adding billing and shipping address

* feat: add default address

* feat: add styles to add address form

* feat: add mixins

* feat: implement deleting address

* refactor: add error message to a message notification

* refactor: remove determining address type to a separate function

* feat: implement redrawing after changes

* fix: determine address type

* fix(RSS-ECOMM-4_33): reloading product page (#320)

fix: reloading product page

* feat(RSS-ECOMM-4_10)/cart (#321)

* feat: modify quantity

* feat: delete button for mobile

* feat: add clear cart

* feat: language and message

* fix: src img

* feat(RSS-ECOMM-4_97): filter range (#323)

* feat: filter Type

* feat: catalog filter

* feat: filter price range

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

* feat: implement Confirm component

* fix: styles for responsive

* feat: add confirmation message

* fix: dark theme variable

* feat: implement changing address fields

* feat: add styles to edit address window

* feat: add remove content method to modal

* fix(RSS-ECOMM-4_34): catalog (#324)

* fix: country validation

* fix: last link in breadcrumbs

* fix: range price slider

* feat(RSS-ECOMM-4_35): update address editing (#325)

* feat: update errorMessage to include a custom message

* feat: update edit to remove default only

* fix: typo in title

* feat: add loader for address request

* refactor: replace bin picture

* fix(RSS-ECOMM-4_36): user addresses (#326)

* fix: validation postal code

* fix: display user addresses

* feat(RSS-ECOMM-4_37): display product discount (#327)

* feat: display product discount

* fix: submit form addresses

* feat: increase the interactive area of the description button on the product card

* fix(RSS-ECOMM-4_96): cart blog footer (#328)

* fix: color for dark theme

* fix: subcategory link in footer navigation

* fix: delete submit for subscribe button

* fix: scroll after swipe

* fix: price with discount for cart

* fix: sort price and cart message

* Merge branch 'sprint-4' into fix(RSS-ECOMM-4_96)/cartBlogFooter

* refactor: delete comments

* feat(RSS-ECOMM-4_37): update app themes (#329)

* fix: replace notFound image

* feat: implement redirection for unauth user

* feat: translate app title

* refactor: update setting app title

* refactor: break down into smaller functions

* refactor: update blog titles

* feat: update themes colors

* refactor: make two tumblers same size

* fix: addresses colors

* fix: footer logo margin

* fix: discount label text color

* feat(RSS-ECOMM-4_38): scroll to top button (#330)

* feat: implement scrollToTop button

* fix: binding context

* refactor: update confirm component

* refactor: update address edit buttons

* fix: table heading color

* refactor(RSS-ECOMM-4_39): search params (#331)

* feat(RSS-ECOMM-4_95): add confirm and link (#333)

* feat: add confirm and link

* fix: cart request

* refactor(RSS-ECOMM-4_40): update password edit form (#332)

* refactor: replace div with form

* feat: add mixin for repeated links

* refactor: update footer links

* fix: footer paddings

* fix: add semantics to contact information

* fix: add transition to blog articles shadow

* refactor: replace to catalog link with mixin

* refactor: discard redundant user check

* chore: update CI/CD for sprint 5

* chore: solve conflicts

* fix: after merge develop

* fix: footer input not required

* fix: validation params

---------

Co-authored-by: Max <[email protected]>
Co-authored-by: Yuliya Kursevich <[email protected]>
  • Loading branch information
3 people authored Jun 3, 2024
1 parent 0f5def2 commit 0a0d5e1
Show file tree
Hide file tree
Showing 166 changed files with 5,252 additions and 1,917 deletions.
3 changes: 3 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ sprint3:

sprint4:
- head-branch: 'ECOMM-4'

sprint5:
- head-branch: 'ECOMM-5'
4 changes: 2 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
📍 conforms with the following format:

- [ ] prefix (following the [convention](https://www.conventionalcommits.org/en/v1.0.0-beta.2/): `feat`, `fix`, `hotfix`, `chore`, `refactor`, `revert`, `docs`, `style`, `test`)
- [ ] sprint and issue number (e.g. `RSS-ECOMM-3_01`, where `3` - is the sprint number and `01` - is the issue number)
- [ ] sprint and issue number (e.g. `RSS-ECOMM-5_01`, where `5` - is the sprint number and `01` - is the issue number)
- [ ] short description

👀 Example: `feat(RSS-ECOMM-3_01): description`
👀 Example: `feat(RSS-ECOMM-5_01): description`

## PR Description 🧙‍♂️

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- sprint-2
- sprint-3
- sprint-4
- sprint-5

jobs:
format:
Expand Down
6 changes: 5 additions & 1 deletion .validate-branch-namerc.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
module.exports = {
pattern: /^(feat|fix|hotfix|chore|refactor|revert|docs|style|test|)\(RSS-ECOMM-\d_\d{2}\)\/[a-z]+[a-zA-Z0-9]*$/,
pattern:
/^sprint-4|^(feat|fix|hotfix|chore|refactor|revert|docs|style|test|)\(RSS-ECOMM-\d_\d{2}\)\/[a-z]+[a-zA-Z0-9]*$/,
errorMsg: 'Please use correct branch name',
};

// Branch Name Examples:

// "feat(RSS-ECOMM-1_01)/addNewProduct" // where 1 is the sprint number and 01 is the issue number
// "fix(RSS-ECOMM-2_15)/addCorrectProduct" // where 2 is the sprint number and 15 is the issue number
// "chore(RSS-ECOMM-3_01)/addNewProduct" // where 3 is the sprint number and 01 is the issue number
// "refactor(RSS-ECOMM-4_01)/addNewProduct" // where 4 is the sprint number and 01 is the issue number
// "revert(RSS-ECOMM-5_01)/addNewProduct" // where 5 is the sprint number and 01 is the issue number
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@
"@commercetools/sdk-client-v2": "^2.5.0",
"@commercetools/sdk-middleware-auth": "^7.0.1",
"@commercetools/sdk-middleware-http": "^7.0.4",
"@types/hammerjs": "^2.0.45",
"@types/js-cookie": "^3.0.6",
"@types/uuid": "^9.0.8",
"autoprefixer": "^10.4.19",
"hammerjs": "^2.0.8",
"isomorphic-fetch": "^3.0.0",
"js-cookie": "^3.0.5",
"materialize-css": "^1.0.0-rc.2",
Expand All @@ -75,6 +78,7 @@
"postcode-validator": "^3.8.20",
"sharp": "^0.33.3",
"swiper": "^11.1.3",
"uuid": "^9.0.1",
"vite-plugin-checker": "^0.6.4",
"vite-plugin-image-optimizer": "^1.1.7",
"vite-plugin-svg-spriter": "^1.0.0",
Expand Down
Binary file modified public/img/png/instagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/png/instagram_.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/img/png/linkedin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/img/png/meta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/png/pay-ae.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/png/pay-mc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/png/pay-pp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/png/pay-visa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/img/png/twitterx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/img/png/union.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions src/app/App/model/AppModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Page, PageParams, PagesType } from '@/shared/types/page.ts';

import RouterModel from '@/app/Router/model/RouterModel.ts';
import modal from '@/shared/Modal/model/ModalModel.ts';
import ScrollToTopModel from '@/shared/ScrollToTop/model/ScrollToTopModel.ts';
import { PAGE_ID } from '@/shared/constants/pages.ts';
import FooterModel from '@/widgets/Footer/model/FooterModel.ts';
import HeaderModel from '@/widgets/Header/model/HeaderModel.ts';
Expand Down Expand Up @@ -32,9 +33,9 @@ class AppModel {
const { default: CartPageModel } = await import('@/pages/CartPage/model/CartPageModel.ts');
return new CartPageModel(this.appView.getHTML());
},
[PAGE_ID.CATALOG_PAGE]: async (params: PageParams): Promise<Page> => {
[PAGE_ID.CATALOG_PAGE]: async (): Promise<Page> => {
const { default: CatalogPageModel } = await import('@/pages/CatalogPage/model/CatalogPageModel.ts');
return new CatalogPageModel(this.appView.getHTML(), params);
return new CatalogPageModel(this.appView.getHTML());
},
[PAGE_ID.DEFAULT_PAGE]: async (): Promise<Page> => {
const { default: MainPageModel } = await import('@/pages/MainPage/model/MainPageModel.ts');
Expand Down Expand Up @@ -77,7 +78,8 @@ class AppModel {
private async initialize(): Promise<RouterModel> {
const routes = await this.createRoutes();
const router = new RouterModel(routes);
document.body.append(this.appView.getHTML());
const scrollToTop = new ScrollToTopModel();
document.body.append(this.appView.getHTML(), scrollToTop.getHTML().getHTML());
this.appView.getHTML().insertAdjacentElement('beforebegin', new HeaderModel(this.appView.getHTML()).getHTML());
this.appView.getHTML().insertAdjacentElement('afterend', new FooterModel().getHTML());
this.appView.getHTML().insertAdjacentElement('afterend', modal.getHTML());
Expand Down
78 changes: 53 additions & 25 deletions src/app/Router/model/RouterModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { PageParams, PagesType } from '@/shared/types/page';

import getStore from '@/shared/Store/Store.ts';
import observeStore, { selectCurrentLanguage } from '@/shared/Store/observer.ts';
import { PAGE_ID } from '@/shared/constants/pages.ts';
import { isValidPath, isValidState } from '@/shared/types/validation/paths.ts';
import formattedText from '@/shared/utils/formattedText.ts';
import setPageTitle from '@/shared/utils/setPageTitle.ts';
import showErrorMessage from '@/shared/utils/userMessage.ts';

const PROJECT_TITLE = import.meta.env.VITE_APP_PROJECT_TITLE;
const DEFAULT_SEGMENT = import.meta.env.VITE_APP_DEFAULT_SEGMENT;
const NEXT_SEGMENT = import.meta.env.VITE_APP_NEXT_SEGMENT;
const PATH_SEGMENTS_TO_KEEP = import.meta.env.VITE_APP_PATH_SEGMENTS_TO_KEEP;
Expand All @@ -23,17 +24,19 @@ class RouterModel {

this.routes = routes;
document.addEventListener('DOMContentLoaded', () => {
const currentPath = window.location.pathname.slice(NEXT_SEGMENT).split(DEFAULT_SEGMENT) || PAGE_ID.DEFAULT_PAGE;
const currentPath =
(window.location.pathname + window.location.search).slice(NEXT_SEGMENT).split(DEFAULT_SEGMENT) ||
PAGE_ID.DEFAULT_PAGE;
this.navigateTo(currentPath.join(DEFAULT_SEGMENT));
});

window.addEventListener('popstate', (event) => {
const currentState: unknown = event.state;
let currentPage = '';
let [currentPage] = '';
let currentPath = '';

if (isValidState(currentState)) {
currentPage = currentState.path.split(DEFAULT_SEGMENT)[PATH_SEGMENTS_TO_KEEP] + DEFAULT_SEGMENT;
[currentPage] = currentState.path.split(DEFAULT_SEGMENT)[PATH_SEGMENTS_TO_KEEP].split(SEARCH_SEGMENT);
currentPath = currentState.path;
}

Expand All @@ -46,20 +49,51 @@ class RouterModel {
});
}

public static appendSearchParams(key: string, value: string): void {
const url = new URL(decodeURIComponent(window.location.href));
url.searchParams.append(key, value);
const path = url.pathname + url.search.toString();
window.history.pushState({ path: path.slice(NEXT_SEGMENT) }, '', path);
}

public static clearSearchParams(): void {
const url = new URL(decodeURIComponent(window.location.href));
const path = `${DEFAULT_SEGMENT}${url.pathname.split(DEFAULT_SEGMENT)[NEXT_SEGMENT]}${DEFAULT_SEGMENT}`;
window.history.pushState({ path: path.slice(NEXT_SEGMENT) }, '', path);
}

public static deleteSearchParams(key: string): void {
const url = new URL(decodeURIComponent(window.location.href));
url.searchParams.delete(key);
const path = url.pathname + url.search.toString();
window.history.pushState({ path: path.slice(NEXT_SEGMENT) }, '', path);
}

public static getInstance(): RouterModel {
return RouterModel.router;
}

public static getSearchParams(): URLSearchParams {
return new URL(decodeURIComponent(window.location.href)).searchParams;
}

public static setSearchParams(key: string, value: string): void {
const url = new URL(decodeURIComponent(window.location.href));
url.searchParams.delete(key);
url.searchParams.set(key, value);
const path = url.pathname + url.search.toString();
window.history.pushState({ path: path.slice(NEXT_SEGMENT) }, '', path);
}

private async checkPageAndParams(
currentPage: string,
path: string,
): Promise<{ hasRoute: boolean; params: PageParams } | null> {
const hasRoute = this.routes.has(currentPage);
const decodePath = decodeURIComponent(path);
const id = decodePath.split(DEFAULT_SEGMENT).slice(PATH_SEGMENTS_TO_KEEP, -NEXT_SEGMENT)[NEXT_SEGMENT];
const searchParams = decodeURIComponent(decodePath).split(SEARCH_SEGMENT)[NEXT_SEGMENT];
const title = `${PROJECT_TITLE} | ${hasRoute ? formattedText(currentPage === PAGE_ID.DEFAULT_PAGE ? PAGE_ID.MAIN_PAGE.slice(PATH_SEGMENTS_TO_KEEP, -NEXT_SEGMENT) : currentPage.slice(PATH_SEGMENTS_TO_KEEP, -NEXT_SEGMENT)) : PAGE_ID.NOT_FOUND_PAGE.slice(PATH_SEGMENTS_TO_KEEP, -NEXT_SEGMENT)}`;
document.title = title;
const id = path.split(DEFAULT_SEGMENT)[NEXT_SEGMENT]?.split(SEARCH_SEGMENT)[PATH_SEGMENTS_TO_KEEP] || null;

setPageTitle(currentPage, hasRoute);
observeStore(selectCurrentLanguage, () => this.checkPageAndParams(currentPage, path));

if (!hasRoute) {
await this.routes.get(PAGE_ID.NOT_FOUND_PAGE)?.({});
Expand All @@ -68,12 +102,7 @@ class RouterModel {

return {
hasRoute,
params: {
[currentPage.slice(PATH_SEGMENTS_TO_KEEP, -NEXT_SEGMENT)]: {
id: id ?? null,
searchParams: searchParams ?? null,
},
},
params: { [currentPage.slice(PATH_SEGMENTS_TO_KEEP)]: { id } },
};
}

Expand All @@ -88,15 +117,14 @@ class RouterModel {
}

public navigateTo(path: string): void {
const currentPage = path.split(DEFAULT_SEGMENT)[PATH_SEGMENTS_TO_KEEP] + DEFAULT_SEGMENT || PAGE_ID.DEFAULT_PAGE;
this.checkPageAndParams(currentPage, path)
.then((check) => {
if (check) {
this.routes.get(currentPage)?.(check.params).catch(showErrorMessage);
history.pushState({ path }, '', `/${path}`);
}
})
.catch(showErrorMessage);
const currentPage =
path.split(DEFAULT_SEGMENT)[PATH_SEGMENTS_TO_KEEP].split(SEARCH_SEGMENT)[PATH_SEGMENTS_TO_KEEP] ||
PAGE_ID.DEFAULT_PAGE;

if (currentPage !== getStore().getState().currentPage || currentPage === PAGE_ID.DEFAULT_PAGE) {
this.handleRequest(currentPage, path);
}
history.pushState({ path }, '', `/${path}`);
}
}

Expand Down
Loading

0 comments on commit 0a0d5e1

Please sign in to comment.