Skip to content

Commit

Permalink
Merge pull request #868 from geonetwork/ME/record-field-update-frequency
Browse files Browse the repository at this point in the history
Me/record field update frequency
  • Loading branch information
LHBruneton-C2C authored May 23, 2024
2 parents 6b7e19c + 15b62fc commit 8224939
Show file tree
Hide file tree
Showing 28 changed files with 364 additions and 10 deletions.
13 changes: 13 additions & 0 deletions libs/api/metadata-converter/src/lib/iso19139/read-parts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import GEOCAT_CH_DATASET from '../fixtures/geocat-ch.iso19139.dataset.xml'
// @ts-ignore
import GEOCAT_CH_SERVICE from '../fixtures/geocat-ch.iso19139.service.xml'
import {
getUpdateFrequencyFromCustomPeriod,
readContacts,
readDistributions,
readOnlineResources,
Expand Down Expand Up @@ -91,6 +92,18 @@ describe('read parts', () => {
})
})
})
describe('getUpdateFrequencyFromCustomPeriod', () => {
it('keeps a partial weekly period', () => {
expect(getUpdateFrequencyFromCustomPeriod('P0Y0M2D')).toEqual({
updatedTimes: 3,
per: 'week',
})
expect(getUpdateFrequencyFromCustomPeriod('P0Y0M3D')).toEqual({
updatedTimes: 2,
per: 'week',
})
})
})
})

describe('dataset record', () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/api/metadata-converter/src/lib/iso19139/read-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export function getUpdateFrequencyFromCustomPeriod(
} else if (days <= 7) {
return {
per: 'week',
updatedTimes: Math.round(7 / days),
updatedTimes: Math.round(7 / days - 0.0001), // this is to make sure that 'every 2 days' = '3 times per week'
}
} else if (days) {
return {
Expand Down
23 changes: 22 additions & 1 deletion libs/api/metadata-converter/src/lib/iso19139/write-parts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
parseXmlString,
xmlToString,
} from '../xml-utils'
import { writeDistributions, writeKeywords } from './write-parts'
import {
getISODuration,
writeDistributions,
writeKeywords,
} from './write-parts'
import { GENERIC_DATASET_RECORD } from '../fixtures/generic.records'
import { DatasetRecord } from '@geonetwork-ui/common/domain/model/record'

Expand Down Expand Up @@ -512,4 +516,21 @@ describe('write parts', () => {
</root>`)
})
})

describe('getISODuration', () => {
it('keeps a partial weekly period', () => {
expect(
getISODuration({
updatedTimes: 3,
per: 'week',
})
).toEqual('P0Y0M2D')
expect(
getISODuration({
updatedTimes: 2,
per: 'week',
})
).toEqual('P0Y0M3D')
})
})
})
5 changes: 1 addition & 4 deletions libs/api/metadata-converter/src/lib/iso19139/write-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import {
findChildOrCreate,
findChildrenElement,
findNestedChildOrCreate,
findNestedElement,
findNestedElements,
readAttribute,
readText,
removeAllChildren,
removeChildren,
removeChildrenByName,
Expand All @@ -36,7 +34,6 @@ import {
} from '../xml-utils'
import {
ChainableFunction,
combine,
fallback,
filterArray,
getAtIndex,
Expand Down Expand Up @@ -220,7 +217,7 @@ export function getISODuration(updateFrequency: UpdateFrequencyCustom): string {
else duration.hours = Math.round(24 / updateFrequency.updatedTimes)
break
case 'week':
duration.days = Math.round(7 / updateFrequency.updatedTimes)
duration.days = Math.round(7 / updateFrequency.updatedTimes - 0.0001) // this is to make sure that '2 times per week' = 'every 3 days'
break
case 'month':
if (updateFrequency.updatedTimes <= 1) duration.months = 1
Expand Down
4 changes: 4 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ describe('Editor Selectors', () => {
config: DEFAULT_FIELDS[5],
value: DATASET_RECORDS[0].resourceUpdated,
},
{
config: DEFAULT_FIELDS[6],
value: DATASET_RECORDS[0].updateFrequency,
},
])
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<gn-ui-check-toggle
[label]="'editor.record.form.updateFrequency.planned' | translate"
[value]="planned"
(toggled)="onPlannedToggled()"
></gn-ui-check-toggle>
<gn-ui-dropdown-selector
title="updateFrequency"
[showTitle]="false"
[choices]="choices"
[selected]="selectedFrequency"
(selectValue)="onSelectFrequencyValue($event)"
[disabled]="!planned"
>
</gn-ui-dropdown-selector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'

import { FormFieldUpdateFrequencyComponent } from './form-field-update-frequency.component'
import { TranslateModule } from '@ngx-translate/core'
import { FormControl } from '@angular/forms'

describe('FormFieldUpdateFrequencyComponent', () => {
let component: FormFieldUpdateFrequencyComponent
let fixture: ComponentFixture<FormFieldUpdateFrequencyComponent>

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [FormFieldUpdateFrequencyComponent, TranslateModule.forRoot()],
}).compileComponents()

fixture = TestBed.createComponent(FormFieldUpdateFrequencyComponent)
component = fixture.componentInstance
const control = new FormControl()
control.setValue({
updatedTimes: 3,
per: 'week',
})
component.control = control
fixture.detectChanges()
})

it('should create', () => {
expect(component).toBeTruthy()
})

it('should parse the updatedTimes and per values', () => {
component.onSelectFrequencyValue('day.1')
expect(component.control.value).toEqual({
updatedTimes: 1,
per: 'day',
})
})

it('should be recognized as planned', () => {
expect(component.planned).toBeTruthy()
})

it('should add the custom frequency to the dropdown choices', () => {
expect(component.choices).toContainEqual({
value: 'week.3',
label: 'domain.record.updateFrequency.week',
})
})

describe('Switch to not planned', () => {
beforeEach(async () => {
component.onPlannedToggled()
})

it('should set the value as notPlanned', () => {
expect(component.control.value).toBe('notPlanned')
})

it('should be recognized as not planned', () => {
expect(component.planned).toBeFalsy()
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
ChangeDetectionStrategy,
Component,
Input,
OnInit,
} from '@angular/core'
import { FormControl } from '@angular/forms'
import { marker } from '@biesbjerg/ngx-translate-extract-marker'
import {
CheckToggleComponent,
DropdownSelectorComponent,
} from '@geonetwork-ui/ui/inputs'
import { TranslateModule, TranslateService } from '@ngx-translate/core'

@Component({
selector: 'gn-ui-form-field-update-frequency',
templateUrl: './form-field-update-frequency.component.html',
styleUrls: ['./form-field-update-frequency.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [CheckToggleComponent, DropdownSelectorComponent, TranslateModule],
})
export class FormFieldUpdateFrequencyComponent implements OnInit {
@Input() control: FormControl

get planned() {
return this.control.value !== 'notPlanned'
}

constructor(private translateService: TranslateService) {}

ngOnInit() {
const updatedTimes = this.control.value?.updatedTimes
const per = this.control.value?.per
if (updatedTimes && updatedTimes !== 1 && updatedTimes !== 2) {
this.choices = [
{
value: `${per}.${updatedTimes}`,
label: this.translateService.instant(
`domain.record.updateFrequency.${per}`,
{
count: updatedTimes,
}
),
},
...this.choices,
]
}
}

onPlannedToggled() {
if (this.planned) {
this.control.setValue('notPlanned')
} else {
this.control.setValue({ updatedTimes: 1, per: 'day' })
}
}

get selectedFrequency() {
const { updatedTimes, per } = this.control.value
return `${per}.${updatedTimes}`
}

onSelectFrequencyValue(value: unknown) {
const split = (value as string).split('.')
this.control.setValue({ updatedTimes: Number(split[1]), per: split[0] })
}

choices = [
{
value: 'day.1',
label: this.translateService.instant(
'domain.record.updateFrequency.day',
{
count: 1,
}
),
},
{
value: 'day.2',
label: this.translateService.instant(
'domain.record.updateFrequency.day',
{
count: 2,
}
),
},
{
value: 'week.1',
label: this.translateService.instant(
'domain.record.updateFrequency.week',
{
count: 1,
}
),
},
{
value: 'week.2',
label: this.translateService.instant(
'domain.record.updateFrequency.week',
{
count: 2,
}
),
},
{
value: 'month.1',
label: this.translateService.instant(
'domain.record.updateFrequency.month',
{
count: 1,
}
),
},
{
value: 'month.2',
label: this.translateService.instant(
'domain.record.updateFrequency.month',
{
count: 2,
}
),
},
{
value: 'year.1',
label: this.translateService.instant(
'domain.record.updateFrequency.year',
{
count: 1,
}
),
},
{
value: 'year.2',
label: this.translateService.instant(
'domain.record.updateFrequency.year',
{
count: 2,
}
),
},
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
[control]="formControl"
></gn-ui-form-field-resource-updated>
</ng-container>
<ng-container *ngIf="isUpdateFrequency">
<gn-ui-form-field-update-frequency
[control]="formControl"
></gn-ui-form-field-update-frequency>
</ng-container>
<ng-container *ngIf="isSimpleField">
<gn-ui-form-field-simple
[type]="simpleType"
Expand Down
Loading

0 comments on commit 8224939

Please sign in to comment.