-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate count badge into a separate component
- Loading branch information
1 parent
96d4841
commit 5cf9a86
Showing
11 changed files
with
119 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { Cart } from '@/shared/types/cart.ts'; | ||
|
||
import getCartModel from '@/shared/API/cart/model/CartModel.ts'; | ||
|
||
import CountBadgeView from '../view/CountBadgeView.ts'; | ||
|
||
class CountBadgeModel { | ||
private view = new CountBadgeView(); | ||
|
||
constructor() { | ||
this.init(); | ||
} | ||
|
||
private countChangeHandler(cart: Cart): boolean { | ||
this.view.updateBadgeCount(cart.products.reduce((acc, item) => acc + item.quantity, 0)); | ||
return true; | ||
} | ||
|
||
private init(): void { | ||
this.observeCartChange(); | ||
} | ||
|
||
private observeCartChange(): boolean { | ||
return getCartModel().observeCartChange(this.countChangeHandler.bind(this)); | ||
} | ||
|
||
public getHTML(): HTMLDivElement { | ||
return this.view.getHTML(); | ||
} | ||
} | ||
|
||
export default CountBadgeModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import createBaseElement from '@/shared/utils/createBaseElement.ts'; | ||
|
||
import styles from './countBadgeView.module.scss'; | ||
|
||
class CountBadgeView { | ||
private countBadge: HTMLSpanElement; | ||
|
||
private countBadgeWrap: HTMLDivElement; | ||
|
||
constructor() { | ||
this.countBadgeWrap = this.createHTML(); | ||
this.countBadge = this.createBadge(); | ||
} | ||
|
||
private createBadge(): HTMLSpanElement { | ||
this.countBadge = createBaseElement({ | ||
cssClasses: [styles.badge], | ||
tag: 'span', | ||
}); | ||
this.countBadgeWrap.append(this.countBadge); | ||
|
||
return this.countBadge; | ||
} | ||
|
||
private createHTML(): HTMLDivElement { | ||
this.countBadgeWrap = createBaseElement({ | ||
cssClasses: [styles.badgeWrap], | ||
tag: 'div', | ||
}); | ||
|
||
return this.countBadgeWrap; | ||
} | ||
|
||
public getHTML(): HTMLDivElement { | ||
return this.countBadgeWrap; | ||
} | ||
|
||
public updateBadgeCount(count?: number): void { | ||
if (!count) { | ||
this.countBadgeWrap.classList.add(styles.hidden); | ||
} else { | ||
this.countBadgeWrap.classList.remove(styles.hidden); | ||
} | ||
|
||
this.countBadge.textContent = count ? count.toString() : ''; | ||
} | ||
} | ||
|
||
export default CountBadgeView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.badgeWrap { | ||
position: absolute; | ||
right: -30%; | ||
top: -20%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
border: var(--one) solid var(--noble-gray-1000); | ||
border-radius: 100%; | ||
width: calc(var(--tiny-offset) * 2); | ||
height: calc(var(--tiny-offset) * 2); | ||
font: var(--regular-font); | ||
background-color: var(--steam-green-800); | ||
} | ||
|
||
.badge { | ||
display: block; | ||
color: var(--noble-gray-200); | ||
} | ||
|
||
.hidden { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5cf9a86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate count badge into a single component