Skip to content

Commit

Permalink
docs: add remote debug port to gn, document
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow authored and Romuald Caplier committed Aug 1, 2024
1 parent 46dd4e8 commit 2a38a00
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface MetadataResourceApiModel {
metadataResourceExternalManagementProperties?: MetadataResourceExternalManagementPropertiesApiModel
lastModification?: string
version?: string
url?: URL
url?: string
filename?: string
id?: string
size?: number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<gn-ui-image-input
[maxSizeMB]="5"
[previewUrl]="resourceUrl"
[altText]="imageAltText"
[altText]="resourceAltText"
[formControl]="formControl"
(fileChange)="handleFileChange($event)"
(urlChange)="handleUrlChange($event)"
(altTextChange)="handleAltTextChange($event)"
(delete)="handleDelete()"
></gn-ui-image-input>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ import { TranslateModule } from '@ngx-translate/core'
import { of } from 'rxjs'
import { OverviewUploadComponent } from './overview-upload.component'

const imageFileName = 'doge.jpg'
const imageUrl =
'http://localhost:8080/geonetwork/srv/api/records/8698bf0b-fceb-4f0f-989b-111e7c4af0a4/attachments/doge.jpg'

class RecordsApiServiceMock {
getAllResources = jest.fn(() =>
of([{ filename: 'filenameGet', url: 'urlGet' }])
of([
{
filename: imageFileName,
url: imageUrl,
},
])
)
putResource = jest.fn(() =>
of({
filename: imageFileName,
url: imageUrl,
})
)
putResource = jest.fn(() => of({ filename: 'filenamePut', url: 'urlPut' }))
putResourceFromURL = jest.fn(() =>
of({ filename: 'filenamePutUrl', url: 'urlPutUrl' })
of({ filename: imageFileName, url: imageUrl })
)
delResource = jest.fn(() => of(void 0))
}
Expand Down Expand Up @@ -50,9 +64,11 @@ describe('OverviewUploadComponent', () => {
})

it('should get all resources corresponding to the metadata UUID on init', () => {
fixture.detectChanges()
expect(recordsApiService.getAllResources).toHaveBeenCalledWith(metadataUuid)
expect(component.imageAltText).toEqual('filenameGet')
expect(component.resourceUrl).toEqual('urlGet')
expect(component.resourceAltText).toEqual(imageFileName)
expect(component.resourceFileName).toEqual(imageFileName)
expect(component.resourceUrl.href).toEqual(imageUrl)
})

it('should put the file resource on file change', () => {
Expand All @@ -63,8 +79,22 @@ describe('OverviewUploadComponent', () => {
someFile,
'public'
)
expect(component.imageAltText).toEqual('filenamePut')
expect(component.resourceUrl).toEqual('urlPut')
expect(component.resourceAltText).toEqual(imageFileName)
expect(component.resourceUrl.href).toEqual(imageUrl)
})

it('should put the file resource on alt text change', () => {
const altTextChangeSpy = jest.spyOn(component.overviewChange, 'emit')

const newAltText = 'newAltText'
const newImageUrl = new URL(imageUrl)

component.handleAltTextChange(newAltText)
expect(altTextChangeSpy).toHaveBeenCalledWith({
description: newAltText,
url: newImageUrl,
})
expect(component.resourceAltText).toEqual('newAltText')
})

it('should put the resource from URL on URL change', () => {
Expand All @@ -74,18 +104,18 @@ describe('OverviewUploadComponent', () => {
'someUrl',
'public'
)
expect(component.imageAltText).toEqual('filenamePutUrl')
expect(component.resourceUrl).toEqual('urlPutUrl')
expect(component.resourceAltText).toEqual(imageFileName)
expect(component.resourceUrl.href).toEqual(imageUrl)
})

it('should delete the resource corresponding to the metadata UUID on delete', () => {
component.imageAltText = 'filenameDelete'
component.resourceAltText = 'filenameDelete'
component.handleDelete()
expect(recordsApiService.delResource).toHaveBeenCalledWith(
metadataUuid,
'filenameDelete'
imageFileName
)
expect(component.imageAltText).toBeNull()
expect(component.resourceAltText).toBeNull()
expect(component.resourceUrl).toBeNull()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ import {
Component,
EventEmitter,
Input,
OnDestroy,
OnChanges,
OnInit,
Output,
SimpleChanges,
} from '@angular/core'
import { CommonModule } from '@angular/common'
import { RecordsApiService } from '@geonetwork-ui/data-access/gn4'
import { UiInputsModule } from '@geonetwork-ui/ui/inputs'
import { FormControl } from '@angular/forms'
import { GraphicOverview } from '@geonetwork-ui/common/domain/model/record'
import { Subject, takeUntil } from 'rxjs'

const extractFileNameFormUrl = (url: URL, metadataUuid: string): string => {
const pattern = new RegExp(
`records/${metadataUuid}/attachments/([^/?#]+)(?:[/?#]|$)`,
'i'
)
const match = url.href.match(pattern)

const extractFileNameFromUrl = (url: string): string => {
const pattern = new RegExp(`attachments/([^/?#]+)(?:[/?#]|$)`, 'i')
const match = url.match(pattern)
return match ? match[1] : ''
}

Expand All @@ -32,52 +29,50 @@ const extractFileNameFormUrl = (url: URL, metadataUuid: string): string => {
styleUrls: ['./overview-upload.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OverviewUploadComponent implements OnInit, OnDestroy {
export class OverviewUploadComponent implements OnInit, OnChanges {
@Input() metadataUuid: string
@Input() formControl!: FormControl
@Output() overviewChange = new EventEmitter<GraphicOverview | null>()
@Output() altTextChange: EventEmitter<string> = new EventEmitter()

imageAltText: string
resourceAltText = '' // = ressourceFileName by default
resourceFileName = ''
resourceUrl: URL

private destroy$ = new Subject<void>()

constructor(
private recordsApiService: RecordsApiService,
private cd: ChangeDetectorRef
) {}

ngOnInit(): void {
this.recordsApiService
.getAllResources(this.metadataUuid)
.pipe(takeUntil(this.destroy$))
.subscribe({
next: (resources) => {
if (resources && resources.length > 0) {
this.resourceUrl = new URL(resources[0]?.url)
this.imageAltText = resources[0].filename
} else if (this.formControl.value[0]) {
this.resourceUrl = new URL(this.formControl.value[0].url.href)
this.imageAltText = this.formControl.value[0].description
} else {
this.resourceUrl = null
this.imageAltText = ''
}

this.cd.markForCheck()
},
error: this.errorHandle,
})
this.recordsApiService.getAllResources(this.metadataUuid).subscribe({
next: (resources) => {
if (resources && resources.length > 0) {
this.resourceUrl = new URL(resources[0]?.url)
this.resourceAltText =
this.resourceAltText === ''
? resources[0].filename
: this.resourceAltText
this.resourceFileName = extractFileNameFromUrl(resources[0]?.url)
} else {
this.resourceUrl = null
this.resourceAltText = ''
this.resourceFileName = ''
}

this.cd.markForCheck()
},
error: this.errorHandle,
})
}

handleFileChange(file: File) {
this.recordsApiService
.putResource(this.metadataUuid, file, 'public')
.pipe(takeUntil(this.destroy$))
.subscribe({
next: (resource) => {
this.resourceUrl = new URL(resource.url)
this.imageAltText = resource.filename
this.resourceAltText = resource.filename

this.overviewChange.emit({
url: new URL(resource.url),
Expand All @@ -93,11 +88,10 @@ export class OverviewUploadComponent implements OnInit, OnDestroy {
handleUrlChange(url: string) {
this.recordsApiService
.putResourceFromURL(this.metadataUuid, url, 'public')
.pipe(takeUntil(this.destroy$))
.subscribe({
next: (resource) => {
this.resourceUrl = new URL(resource.url)
this.imageAltText = resource.filename
this.resourceAltText = resource.filename

this.overviewChange.emit({
url: new URL(resource.url),
Expand All @@ -110,15 +104,22 @@ export class OverviewUploadComponent implements OnInit, OnDestroy {
})
}

handleDelete() {
const fileName = extractFileNameFormUrl(this.resourceUrl, this.metadataUuid)
handleAltTextChange(newAltText: string) {
this.resourceAltText = newAltText

this.overviewChange.emit({
url: this.resourceUrl,
description: this.resourceAltText,
})
this.cd.markForCheck()
}

handleDelete() {
this.recordsApiService
.delResource(this.metadataUuid, fileName)
.pipe(takeUntil(this.destroy$))
.delResource(this.metadataUuid, this.resourceFileName)
.subscribe({
next: () => {
this.imageAltText = null
this.resourceAltText = null
this.resourceUrl = null

this.overviewChange.emit(null)
Expand All @@ -133,15 +134,33 @@ export class OverviewUploadComponent implements OnInit, OnDestroy {
console.error(error)

this.resourceUrl = null
this.imageAltText = ''
this.resourceAltText = ''
this.resourceFileName = ''

this.overviewChange.emit(null)

this.cd.markForCheck()
}

ngOnDestroy(): void {
this.destroy$.next()
this.destroy$.complete()
ngOnChanges(changes: SimpleChanges): void {
const overviewChanges = changes['formControl']
if (
overviewChanges &&
overviewChanges.currentValue !== overviewChanges.previousValue
) {
let overview: GraphicOverview
if (
overviewChanges.currentValue.value &&
overviewChanges.currentValue.value.length > 0
) {
overview = overviewChanges.currentValue.value[0] as GraphicOverview
} else {
return
}
if (overview.description && overview.description !== '') {
this.resourceAltText = overview.description
this.cd.markForCheck()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<gn-ui-overview-upload
[metadataUuid]="metadataUuid"
[formControl]="control"
(overviewChange)="handleOverViewChange($event)"
(overviewChange)="handleOverviewChange($event)"
></gn-ui-overview-upload>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class FormFieldOverviewsComponent {
@Input() metadataUuid: string
@Input() control!: FormControl

handleOverViewChange(overView: GraphicOverview | null) {
handleOverviewChange(overView: GraphicOverview | null) {
this.control.setValue(overView ? [overView] : [])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe('FormFieldComponent', () => {
By.directive(FormFieldOverviewsComponent)
).componentInstance
})
it('creates an array form field', () => {
it('creates an overview upload form field', () => {
expect(formField).toBeTruthy()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { FormFieldKeywordsComponent } from './form-field-keywords/form-field-key
import { FormFieldOverviewsComponent } from './form-field-overviews/form-field-overviews.component'
import { map, take } from 'rxjs/operators'
import { EditorFacade } from '../../../+state/editor.facade'
import { FormFieldConfig } from '../../../models'

@Component({
selector: 'gn-ui-form-field',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class RecordFormComponent {
if (!model) {
return
}
console.log(newValue)
this.facade.updateRecordField(model, newValue)
}

Expand Down
25 changes: 12 additions & 13 deletions libs/feature/editor/src/lib/fields.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,25 @@ export const RECORD_ABSTRACT_FIELD: EditorField = {
},
}

export const RECORD_GRAPHICAL_OVERVIEW_FIELD: EditorField = {
model: 'overviews',
formFieldConfig: {
labelKey: marker('editor.record.form.field.overviews'),
},
}

/************************************************************
*************** SECTIONS *****************
************************************************************
*/

export const TITLE_SECTION: EditorSection = {
hidden: false,
fields: [RECORD_TITLE_FIELD, RECORD_ABSTRACT_FIELD],
fields: [
RECORD_TITLE_FIELD,
RECORD_ABSTRACT_FIELD,
RECORD_GRAPHICAL_OVERVIEW_FIELD,
],
}

export const ABOUT_SECTION: EditorSection = {
Expand Down Expand Up @@ -167,18 +178,6 @@ export const DEFAULT_CONFIGURATION: EditorConfig = {
{
labelKey: marker('editor.record.form.page.description'),
sections: [TITLE_SECTION, ABOUT_SECTION, GEOGRAPHICAL_COVERAGE_SECTION],
{
model: 'overviews',
formFieldConfig: {
labelKey: marker('editor.record.form.overviews'),
type: 'list',
},
},
{
model: 'keywords',
formFieldConfig: {
labelKey: marker('editor.record.form.keywords'),
type: 'list',
},
{
labelKey: marker('editor.record.form.page.ressources'),
Expand Down
Loading

0 comments on commit 2a38a00

Please sign in to comment.