-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): support updateFrequency field in record form
- Loading branch information
1 parent
6c22fd8
commit a251c8e
Showing
7 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
15 changes: 15 additions & 0 deletions
15
...rd-form/form-field/form-field-update-frequency/form-field-update-frequency.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,15 @@ | ||
<p>form-field-update-frequency works!</p> | ||
<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> |
63 changes: 63 additions & 0 deletions
63
...form/form-field/form-field-update-frequency/form-field-update-frequency.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,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() | ||
}) | ||
}) | ||
}) |
143 changes: 143 additions & 0 deletions
143
...cord-form/form-field/form-field-update-frequency/form-field-update-frequency.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,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, | ||
} | ||
), | ||
}, | ||
] | ||
} |
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