Skip to content

Commit

Permalink
chore: more adaptations for consumers of gn-ui-url-input
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Nov 4, 2024
1 parent e790506 commit 8491f90
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '@ng-icons/core'
import {
iconoirArrowLeft,
iconoirAttachment,
iconoirImport,
iconoirLightBulbOn,
} from '@ng-icons/iconoir'
Expand Down Expand Up @@ -106,10 +105,10 @@ export class ImportRecordComponent {
this.cdr.markForCheck()
}

importRecord(url: string) {
importRecord(url: URL) {
this.isRecordImportInProgress = true

this.recordsRepository.duplicateExternalRecord(url).subscribe({
this.recordsRepository.duplicateExternalRecord(url.toString()).subscribe({
next: (recordTempId) => {
if (recordTempId) {
this.notificationsService.showNotification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ export class FormFieldOnlineResourcesComponent {
}
}

handleServiceUrlChange(url: string) {
handleServiceUrlChange(url: URL) {
this.valueChange.emit([
...this.allResources,
{
...this.newService,
url: new URL(url),
url,
},
])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('FileInputComponent', () => {
it('emits the entered URL', () => {
let emitted
component.urlChange.subscribe((v) => (emitted = v))
component.handleUrlChange('http://hello.world')
expect(emitted).toEqual('http://hello.world')
component.handleUrlChange(new URL('http://hello.world'))
expect(emitted).toEqual('http://hello.world/')
})
})

Expand Down
4 changes: 2 additions & 2 deletions libs/ui/inputs/src/lib/file-input/file-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export class FileInputComponent {
this.handleDropFiles(Array.from((event.target as HTMLInputElement).files))
}

handleUrlChange(url: string) {
this.urlChange.emit(url)
handleUrlChange(url: URL) {
this.urlChange.emit(url.toString())
}

handleSecondaryTextClick(event: Event) {
Expand Down
10 changes: 5 additions & 5 deletions libs/ui/inputs/src/lib/image-input/image-input.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('ImageInputComponent', () => {
type: 'text/plain',
})
const someImageFile = new File([], 'someImageFile', { type: 'image/png' })
const result = component.filterTypeImage([
const result = component['filterTypeImage']([
someNonImageFile,
someImageFile,
])
Expand All @@ -58,7 +58,7 @@ describe('ImageInputComponent', () => {
it('should emit the downloaded file on nominal case', waitForAsync(() => {
jest.spyOn(component.fileChange, 'emit')

component.downloadUrl('http://test.com/image.png')
component.downloadUrl(new URL('http://test.com/image.png'))

const reqHead = httpTestingController.expectOne(testUrl)
expect(reqHead.request.method).toEqual('HEAD')
Expand All @@ -85,7 +85,7 @@ describe('ImageInputComponent', () => {
}))

it('should not download the file when content-type is not image', waitForAsync(() => {
component.downloadUrl('http://test.com/image.png')
component.downloadUrl(new URL('http://test.com/image.png'))

const reqHead = httpTestingController.expectOne(testUrl)
expect(reqHead.request.method).toEqual('HEAD')
Expand All @@ -103,7 +103,7 @@ describe('ImageInputComponent', () => {
}))

it('should not download the file when content-length is above limit', waitForAsync(() => {
component.downloadUrl('http://test.com/image.png')
component.downloadUrl(new URL('http://test.com/image.png'))

const reqHead = httpTestingController.expectOne(testUrl)
expect(reqHead.request.method).toEqual('HEAD')
Expand All @@ -123,7 +123,7 @@ describe('ImageInputComponent', () => {
it('should emit the file URL when encountering a download error', waitForAsync(() => {
jest.spyOn(component.urlChange, 'emit')

component.downloadUrl('http://test.com/image.png')
component.downloadUrl(new URL('http://test.com/image.png'))

const reqHead = httpTestingController.expectOne(testUrl)
expect(reqHead.request.method).toEqual('HEAD')
Expand Down
14 changes: 7 additions & 7 deletions libs/ui/inputs/src/lib/image-input/image-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import {
provideNgIconsConfig,
} from '@ng-icons/core'
import {
iconoirBin,
iconoirFramePlusIn,
iconoirLink,
iconoirMediaImage,
iconoirMediaImageXmark,
iconoirBin,
iconoirPlus,
iconoirLink,
} from '@ng-icons/iconoir'

@Component({
Expand Down Expand Up @@ -136,20 +136,20 @@ export class ImageInputComponent {
this.showUrlInput = true
}

async downloadUrl(url: string) {
async downloadUrl(url: URL) {
this.downloadError = false
const name = url.split('/').pop()
const name = url.toString().split('/').pop()

try {
const response = await firstValueFrom(
this.http.head(url, { observe: 'response' })
this.http.head(url.toString(), { observe: 'response' })
)
if (
response.headers.get('content-type')?.startsWith('image/') &&
parseInt(response.headers.get('content-length')) <
megabytesToBytes(this.maxSizeMB)
) {
this.http.get(url, { responseType: 'blob' }).subscribe({
this.http.get(url.toString(), { responseType: 'blob' }).subscribe({
next: (blob) => {
this.cd.markForCheck()
const file = new File([blob], name)
Expand All @@ -158,7 +158,7 @@ export class ImageInputComponent {
error: () => {
this.downloadError = true
this.cd.markForCheck()
this.urlChange.emit(url)
this.urlChange.emit(url.toString())
},
})
}
Expand Down

0 comments on commit 8491f90

Please sign in to comment.