Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97c9998

Browse files
committed
Fixed events and refactored quantity options pipe
1 parent 5bec1d9 commit 97c9998

11 files changed

+57
-16
lines changed

src/app/molecules/quantity-picker/quantity-picker.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<option value="" selected disabled hidden>
88
{{ quantity.min }}
99
</option>
10-
<option *ngFor="let option of quantity | toQuantityOptions">
10+
<option *ngFor="let option of quantity | quantityOptions">
1111
{{ option }}
1212
</option>
1313
</select>

src/app/molecules/quantity-picker/quantity-picker.component.mocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Quantity } from './quantity-picker.component';
22

3-
type MockKeys = 'PRIMARY' | 'WITH_STEPS' | 'MIN_QUANTITY';
3+
type MockKeys = 'PRIMARY' | 'WITH_STEPS' | 'MIN_QUANTITY' | 'WITH_MAX';
44

55
export const QuantityPickerMocks: Record<MockKeys, Quantity> = {
66
PRIMARY: {
@@ -14,4 +14,5 @@ export const QuantityPickerMocks: Record<MockKeys, Quantity> = {
1414
max: 100,
1515
},
1616
MIN_QUANTITY: { min: 1, step: 1, max: 1 },
17+
WITH_MAX: { min: 4, step: 4, max: 12 },
1718
};

src/app/molecules/quantity-picker/quantity-picker.component.stories.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ WithSteps.args = {
2727
quantity: QuantityPickerMocks.WITH_STEPS,
2828
};
2929

30+
export const WithMax = Template.bind({});
31+
WithMax.args = {
32+
quantity: QuantityPickerMocks.WITH_MAX,
33+
};
34+
3035
export const WithInput = Template.bind({});
3136
WithInput.args = {
3237
quantity: QuantityPickerMocks.PRIMARY,

src/app/molecules/quantity-picker/quantity-picker.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { ReactiveFormsModule } from '@angular/forms';
44
import { ButtonModule } from 'src/app/atoms/button/button.module';
55
import { IconModule } from 'src/app/atoms/icon/icon.module';
66
import { QuantityPickerComponent } from './quantity-picker.component';
7-
import { ToQuantityOptionsPipe } from './shared/to-quantity-options.pipe';
7+
import { QuantityOptionsPipe } from './shared/quantity-options.pipe';
88

99
@NgModule({
10-
declarations: [QuantityPickerComponent, ToQuantityOptionsPipe],
10+
declarations: [QuantityPickerComponent, QuantityOptionsPipe],
1111
imports: [CommonModule, ButtonModule, IconModule, ReactiveFormsModule],
1212
exports: [QuantityPickerComponent],
1313
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ToQuantityOptionsPipe } from './to-quantity-options.pipe';
1+
import { QuantityOptionsPipe } from './quantity-options.pipe';
22

33
describe('ToQuantityOptionsPipe', () => {
44
it('create an instance', () => {
5-
const pipe = new ToQuantityOptionsPipe();
5+
const pipe = new QuantityOptionsPipe();
66
expect(pipe).toBeTruthy();
77
});
88
});

src/app/molecules/quantity-picker/shared/to-quantity-options.pipe.ts renamed to src/app/molecules/quantity-picker/shared/quantity-options.pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const MAX_AMOUNT_OF_OPTIONS = 8;
66
export type Option = number | 'More';
77

88
@Pipe({
9-
name: 'toQuantityOptions',
9+
name: 'quantityOptions',
1010
})
11-
export class ToQuantityOptionsPipe implements PipeTransform {
11+
export class QuantityOptionsPipe implements PipeTransform {
1212
transform({ min, max, step }: Quantity): Option[] {
1313
const options: Option[] = [];
1414
for (let i = min; i <= max; i += step) {

src/app/organisms/products-list-dynamic/products-list-dynamic.component.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {
22
ChangeDetectionStrategy,
33
Component,
4+
EventEmitter,
45
Input,
56
OnInit,
7+
Output,
68
QueryList,
79
ViewChildren,
810
ViewContainerRef,
@@ -23,6 +25,11 @@ import {
2325
import { ProductUnion } from '../products-list/products-list.component';
2426
import { ProductsHostDirective } from './shared/products-host.directive';
2527

28+
interface AddToCartEvent {
29+
product: ProductDefault;
30+
quantity: number;
31+
}
32+
2633
@Component({
2734
selector: 'ov-products-list-dynamic',
2835
templateUrl: './products-list-dynamic.component.html',
@@ -32,6 +39,8 @@ import { ProductsHostDirective } from './shared/products-host.directive';
3239
export class ProductsListDynamicComponent implements OnInit {
3340
@Input() products!: ProductUnion[];
3441

42+
@Output() addToCart = new EventEmitter<AddToCartEvent>();
43+
3544
@ViewChildren(ProductsHostDirective)
3645
productsHosts!: QueryList<ProductsHostDirective>;
3746

@@ -89,9 +98,12 @@ export class ProductsListDynamicComponent implements OnInit {
8998
* Handle outputs
9099
*/
91100
this.subscriptions.add(
92-
instance.addToCart.subscribe((quantity) => {
93-
console.log(product, quantity);
94-
})
101+
instance.addToCart.subscribe((quantity) =>
102+
this.addToCart.emit({
103+
product,
104+
quantity,
105+
})
106+
)
95107
);
96108
}
97109

src/app/organisms/products-list/products-list.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<ov-product-default
2222
*ngIf="asProduct(product)! as product"
2323
[product]="product"
24-
(addToCart)="addToCart($event, product)"
24+
(addToCart)="addProductToCart($event, product)"
2525
></ov-product-default>
2626
</ng-container>
2727
</li>

src/app/organisms/products-list/products-list.component.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import {
33
Component,
44
Input,
55
OnInit,
6+
Output,
7+
EventEmitter,
68
} from '@angular/core';
79
import { ProductDefault } from '../../molecules/product-default/product-default.component';
810
import { ProductOutOfStock } from '../../molecules/product-out-of-stock/product-out-of-stock.component';
911
import { ProductReplaced } from '../../molecules/product-replaced/product-replaced.component';
1012

1113
export type ProductUnion = ProductDefault | ProductReplaced | ProductOutOfStock;
14+
15+
export interface AddToCartEvent {
16+
product: ProductDefault;
17+
quantity: number;
18+
}
19+
1220
@Component({
1321
selector: 'ov-products-list',
1422
templateUrl: './products-list.component.html',
@@ -18,6 +26,8 @@ export type ProductUnion = ProductDefault | ProductReplaced | ProductOutOfStock;
1826
export class ProductsListComponent implements OnInit {
1927
@Input() products!: ProductUnion[];
2028

29+
@Output() addToCart = new EventEmitter<AddToCartEvent>();
30+
2131
productIntersectionMargin = '-200px 0px -350px 0px';
2232

2333
ngOnInit(): void {}
@@ -34,8 +44,11 @@ export class ProductsListComponent implements OnInit {
3444
return product.type === 'product-replaced' ? product : undefined;
3545
}
3646

37-
addToCart(quantity: number, product: ProductDefault) {
38-
console.log(product, quantity);
47+
addProductToCart(quantity: number, product: ProductDefault) {
48+
this.addToCart.emit({
49+
product,
50+
quantity,
51+
});
3952
}
4053

4154
productIntersect({ target, isIntersecting }: IntersectionObserverEntry) {

src/app/pages/page-products/page-products.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<ng-template ovSide>Some side stuff</ng-template>
33
<section main>
44
<h2>Products</h2>
5-
<ov-products-list [products]="products"></ov-products-list>
5+
<ov-products-list
6+
[products]="products"
7+
(addToCart)="addProductToCart($event)"
8+
></ov-products-list>
69
</section>
710
</ov-template-default>

0 commit comments

Comments
 (0)