-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: display product attachments at the product detail page (#840)
- Loading branch information
1 parent
ba895e7
commit d275b95
Showing
19 changed files
with
221 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Link } from 'ish-core/models/link/link.model'; | ||
|
||
export interface AttachmentData { | ||
name: string; | ||
type: string; | ||
key: string; | ||
description?: string; | ||
link: Link; | ||
} |
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,48 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
|
||
import { getICMServerURL } from 'ish-core/store/core/configuration'; | ||
|
||
import { AttachmentData } from './attachment.interface'; | ||
import { AttachmentMapper } from './attachment.mapper'; | ||
|
||
describe('Attachment Mapper', () => { | ||
let attachmentMapper: AttachmentMapper; | ||
|
||
const attachmentsMockData = [ | ||
{ | ||
name: 'attachment1', | ||
type: 'Information', | ||
key: 'key1', | ||
description: 'descr1', | ||
link: { | ||
type: 'Link', | ||
uri: 'inSPIRED-inTRONICS-Site/rest;loc=en_US/attachments/attachment1.pdf', | ||
title: 'attachment1', | ||
}, | ||
}, | ||
] as AttachmentData[]; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [provideMockStore({ selectors: [{ selector: getICMServerURL, value: 'http://example.org' }] })], | ||
}); | ||
attachmentMapper = TestBed.inject(AttachmentMapper); | ||
}); | ||
|
||
describe('fromAttachments', () => { | ||
it('should map attachment data to client array object', () => { | ||
expect(attachmentMapper.fromAttachments(attachmentsMockData)).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"description": "descr1", | ||
"key": "key1", | ||
"name": "attachment1", | ||
"type": "Information", | ||
"url": "http://example.org/inSPIRED-inTRONICS-Site/rest;loc=en_US/attachments/attachment1.pdf", | ||
}, | ||
] | ||
`); | ||
}); | ||
}); | ||
}); |
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,33 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Store, select } from '@ngrx/store'; | ||
|
||
import { Attachment } from 'ish-core/models/attachment/attachment.model'; | ||
import { getICMServerURL } from 'ish-core/store/core/configuration'; | ||
|
||
import { AttachmentData } from './attachment.interface'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class AttachmentMapper { | ||
private icmServerURL: string; | ||
|
||
constructor(store: Store) { | ||
store.pipe(select(getICMServerURL)).subscribe(url => (this.icmServerURL = url)); | ||
} | ||
|
||
fromAttachments(attachments: AttachmentData[]): Attachment[] { | ||
if (!attachments || attachments.length === 0) { | ||
return; | ||
} | ||
return attachments.map(attachment => this.fromAttachment(attachment)); | ||
} | ||
|
||
private fromAttachment(attachment: AttachmentData): Attachment { | ||
return { | ||
name: attachment.name, | ||
type: attachment.type, | ||
key: attachment.key, | ||
description: attachment.description, | ||
url: `${this.icmServerURL}/${attachment.link.uri}`, | ||
}; | ||
} | ||
} |
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,7 @@ | ||
export interface Attachment { | ||
name: string; | ||
type: string; | ||
key: string; | ||
description?: string; | ||
url: string; | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/app/shared/components/product/product-attachments/product-attachments.component.html
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,11 @@ | ||
<ng-container *ngIf="attachments$ | async as attachments"> | ||
<div *ngIf="attachments?.length" class="product-attachments"> | ||
<div *ngFor="let attachment of attachments" class="product-attachment"> | ||
<strong>{{ attachment.name }}</strong> | ||
<a class="download-link" [href]="attachment.url" rel="enclosure">{{ | ||
'product.attachments.download.link' | translate | ||
}}</a> | ||
<p *ngIf="attachment.description" class="product-attachment-description">{{ attachment.description }}</p> | ||
</div> | ||
</div> | ||
</ng-container> |
45 changes: 45 additions & 0 deletions
45
src/app/shared/components/product/product-attachments/product-attachments.component.spec.ts
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,45 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { instance, mock } from 'ts-mockito'; | ||
|
||
import { ProductContextFacade } from 'ish-core/facades/product-context.facade'; | ||
import { Product } from 'ish-core/models/product/product.model'; | ||
|
||
import { ProductAttachmentsComponent } from './product-attachments.component'; | ||
|
||
describe('Product Attachments Component', () => { | ||
let component: ProductAttachmentsComponent; | ||
let fixture: ComponentFixture<ProductAttachmentsComponent>; | ||
let element: HTMLElement; | ||
let product: Product; | ||
|
||
beforeEach(async () => { | ||
product = { sku: 'sku' } as Product; | ||
product.attachments = [ | ||
{ | ||
name: 'A', | ||
type: 'typeA', | ||
key: 'keyA', | ||
description: 'descriptionA', | ||
url: 'urlA', | ||
}, | ||
]; | ||
await TestBed.configureTestingModule({ | ||
imports: [TranslateModule.forRoot()], | ||
declarations: [ProductAttachmentsComponent], | ||
providers: [{ provide: ProductContextFacade, useFactory: () => instance(mock(ProductContextFacade)) }], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ProductAttachmentsComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(element).toBeTruthy(); | ||
expect(() => fixture.detectChanges()).not.toThrow(); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
src/app/shared/components/product/product-attachments/product-attachments.component.ts
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,20 @@ | ||
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { ProductContextFacade } from 'ish-core/facades/product-context.facade'; | ||
import { Attachment } from 'ish-core/models/attachment/attachment.model'; | ||
|
||
@Component({ | ||
selector: 'ish-product-attachments', | ||
templateUrl: './product-attachments.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class ProductAttachmentsComponent implements OnInit { | ||
attachments$: Observable<Attachment[]>; | ||
|
||
constructor(private context: ProductContextFacade) {} | ||
|
||
ngOnInit() { | ||
this.attachments$ = this.context.select('product', 'attachments'); | ||
} | ||
} |
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