Skip to content

Commit

Permalink
allow using a library field when adding a new field to the form
Browse files Browse the repository at this point in the history
  • Loading branch information
karel kremer committed Jan 7, 2025
1 parent 662c5ec commit 960760f
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 23 deletions.
38 changes: 38 additions & 0 deletions app/components/generate-custom-field-button.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,44 @@
{{on "input" this.updateName}}
/>
</div>
<div>
<AuLabel @required={{true}} for="library-entry">Uit bibliotheek</AuLabel>
<PowerSelect
@allowClear={{false}}
@renderInPlace={{false}}
@searchEnabled={{true}}
@loadingMessage="Aan het laden..."
@noMatchesMessage="Geen velden gevonden"
@searchMessage="Typ om te zoeken"
@searchField="name"
@options={{this.libraryFieldOptions}}
@selected={{this.selectedLibraryFieldType}}
@onChange={{this.selectLibraryFieldType}}
id="library-entry"
as |selected|
>
{{selected.name}}
</PowerSelect>
</div>
<div>
<AuLabel @required={{true}} for="display-type">Type</AuLabel>
<PowerSelect
@allowClear={{false}}
@renderInPlace={{false}}
@searchEnabled={{true}}
@loadingMessage="Aan het laden..."
@noMatchesMessage="Geen types gevonden"
@searchMessage="Typ om te zoeken"
@searchField="label"
@options={{this.displayTypes}}
@selected={{this.selectedDisplayType}}
@onChange={{this.selectDisplayType}}
id="display-type"
as |selected|
>
{{selected.label}}
</PowerSelect>
</div>
<AuToolbar class="au-u-margin-top" as |Group|>
<Group>
<AuButtonGroup>
Expand Down
53 changes: 50 additions & 3 deletions app/components/generate-custom-field-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@ import { tracked } from '@glimmer/tracking';
import { JSON_API_TYPE } from 'frontend-lmb/utils/constants';
import { showErrorToast } from 'frontend-lmb/utils/toasts';

import { TEXT_CUSTOM_DISPLAY_TYPE_ID } from 'frontend-lmb/utils/well-known-ids';

export default class GenerateCustomFieldButtonComponent extends Component {
@service formReplacements;
@service store;
@service toaster;

@tracked showModal = false;
@tracked loading = false;
@tracked fieldName = '';
customFieldEntry = this.store.createRecord('library-entry', {
name: 'Eigen veld',
});
@tracked selectedLibraryFieldType = this.customFieldEntry;
@tracked selectedDisplayType = null;

constructor() {
super(...arguments);
this.displayTypes.then((displayTypes) => {
this.selectedDisplayType = displayTypes.findBy(
'id',
TEXT_CUSTOM_DISPLAY_TYPE_ID
);
});
}

get invalidName() {
return !this.fieldName || this.fieldName.trim().length < 1;
Expand All @@ -21,6 +39,20 @@ export default class GenerateCustomFieldButtonComponent extends Component {
return this.invalidName;
}

get libraryFieldOptions() {
return this.store
.findAll('library-entry', { include: 'display-type' })
.then((entries) => {
return entries.sortBy('id').reverse();
});
}

get displayTypes() {
return this.store.findAll('display-type').then((entries) => {
return entries.sortBy('label');
});
}

@action
async onAddField() {
this.showModal = true;
Expand All @@ -32,17 +64,32 @@ export default class GenerateCustomFieldButtonComponent extends Component {
this.showModal = false;
}

@action
selectLibraryFieldType(libraryEntry) {
this.selectedLibraryFieldType = libraryEntry;
this.displayTypes.then((types) => {
this.selectedDisplayType =
types.findBy('uri', libraryEntry.get('displayType.uri')) ||
types.findBy('id', TEXT_CUSTOM_DISPLAY_TYPE_ID);
});
}

@action
selectDisplayType(displayType) {
this.selectedDisplayType = displayType;
}

@action async onSaveField() {
this.loading = true;
try {
this.loading = true;
const result = await fetch(`/form-content/${this.args.form.id}/fields`, {
method: 'POST',
headers: {
'Content-Type': JSON_API_TYPE,
},
body: JSON.stringify({
displayType:
'http://lblod.data.gift/display-types/lmb/custom-string-input',
displayType: this.selectedDisplayType.uri,
libraryEntryUri: this.selectedLibraryFieldType.uri,
order: 9000,
name: this.fieldName,
}),
Expand Down
19 changes: 19 additions & 0 deletions app/components/rdf-input-fields/custom-address-input.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<RdfInputFields::CustomFieldWrapper
@field={{@field}}
@form={{@form}}
@formStore={{@formStore}}
>
<div class="au-o-box au-u-background-gray-100 au-u-margin-top-tiny">
<RdfInputFields::AddressSelector
@inTable={{true}}
@field={{@field}}
@form={{@form}}
@formStore={{@formStore}}
@graphs={{@graphs}}
@sourceNode={{@sourceNode}}
@forceShowErrors={{@forceShowErrors}}
@cacheConditionals={{@cacheConditionals}}
@show={{@show}}
/>
</div>
</RdfInputFields::CustomFieldWrapper>
64 changes: 44 additions & 20 deletions app/components/rdf-input-fields/custom-field-wrapper.hbs
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
{{#unless this.removed}}
<AuLabel
for={{this.inputId}}
@error={{this.hasErrors}}
@warning={{this.hasWarnings}}
@required={{this.isRequired}}
>{{this.title}}</AuLabel>
<div
class="au-u-flex au-u-flex--between au-u-flex--vertical-start au-u-flex--spaced-tiny"
>
<div class="flex-grow">
{{yield}}
{{#if @inline}}
<AuLabel
for={{this.inputId}}
@error={{this.hasErrors}}
@warning={{this.hasWarnings}}
@required={{this.isRequired}}
>{{this.title}}</AuLabel>
<div
class="au-u-flex au-u-flex--between au-u-flex--vertical-start au-u-flex--spaced-tiny"
>
<div class="flex-grow">
{{yield}}
</div>
<AuButton
@skin="secondary"
@alert={{true}}
@icon="trash"
@loading={{this.removing}}
{{on "click" this.onRemove}}
>
Verwijder
</AuButton>
</div>
<AuButton
@skin="secondary"
@alert={{true}}
@icon="trash"
@loading={{this.removing}}
{{on "click" this.onRemove}}
{{else}}
<div
class="au-u-flex au-u-flex--between au-u-flex--vertical-center au-u-flex--spaced-tiny"
>
Verwijder
</AuButton>
</div>
<AuLabel
class="flex-grow au-u-margin-bottom-none"
for={{this.inputId}}
@error={{this.hasErrors}}
@warning={{this.hasWarnings}}
@required={{this.isRequired}}
>{{this.title}}</AuLabel>
<AuButton
@skin="secondary"
@alert={{true}}
@icon="trash"
@loading={{this.removing}}
{{on "click" this.onRemove}}
>
Verwijder
</AuButton>
</div>
{{yield}}
{{/if}}
{{#each this.errors as |error|}}
<AuHelpText @error={{true}}>{{error.resultMessage}}</AuHelpText>
{{/each}}
Expand Down
1 change: 1 addition & 0 deletions app/components/rdf-input-fields/custom-string-input.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<RdfInputFields::CustomFieldWrapper
@inline={{true}}
@field={{@field}}
@form={{@form}}
@formStore={{@formStore}}
Expand Down
2 changes: 2 additions & 0 deletions app/config/custom-inflector-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ inflector.plural(/ief$/, 'ieven');
inflector.plural(/or$/, 'oren');
inflector.plural(/ie$/, 'ies');
inflector.plural(/eid$/, 'eden');
inflector.plural(/y$/, 'ies');
inflector.plural(/aa([a-z])$/, 'a$1en');
inflector.plural(/uu([a-z])$/, 'u$1en');
inflector.plural(/oo([a-z])$/, 'o$1en');
Expand All @@ -37,6 +38,7 @@ inflector.irregular('rechtsgrond-besluit', 'rechtsgronden-besluit');
inflector.irregular('editor-document', 'editor-documents');
inflector.irregular('editor-document-status', 'editor-document-statuses');
inflector.irregular('export', 'exports');
inflector.irregular('library-entry', 'library-entries');
inflector.irregular('account', 'accounts');
inflector.irregular('identificator', 'identificatoren');
inflector.irregular('file', 'files');
Expand Down
6 changes: 6 additions & 0 deletions app/models/display-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Model, { attr } from '@ember-data/model';

export default class DisplayTypeModel extends Model {
@attr label;
@attr uri;
}
14 changes: 14 additions & 0 deletions app/models/library-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Model, { attr, belongsTo } from '@ember-data/model';

export default class LibraryEntryModel extends Model {
@attr uri;
@attr name;
@attr path;
@attr options;

@belongsTo('displayType', {
async: true,
inverse: null,
})
displayType;
}
6 changes: 6 additions & 0 deletions app/utils/register-form-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import RdfDateInputComponent from 'frontend-lmb/components/rdf-input-fields/rdf-
import RDFGeboorteInput from 'frontend-lmb/components/rdf-input-fields/geboorte-input';
import RDFGenderSelector from 'frontend-lmb/components/rdf-input-fields/gender-selector';
import RdfInputFieldsCustomStringInputComponent from 'frontend-lmb/components/rdf-input-fields/custom-string-input';
import RdfInputFieldsCustomAddressInputComponent from 'frontend-lmb/components/rdf-input-fields/custom-address-input';

export const registerCustomFormFields = () => {
registerFormFields([
Expand Down Expand Up @@ -115,5 +116,10 @@ export const registerCustomFormFields = () => {
'http://lblod.data.gift/display-types/lmb/custom-string-input',
edit: RdfInputFieldsCustomStringInputComponent,
},
{
displayType:
'http://lblod.data.gift/display-types/lmb/custom-address-input',
edit: RdfInputFieldsCustomAddressInputComponent,
},
]);
};
2 changes: 2 additions & 0 deletions app/utils/well-known-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export const MANDAAT_TOEGEVOEGDE_SCHEPEN_CODE_ID =
'59a90e03-4f22-4bb9-8c91-132618db4b38';
export const MANDAAT_LID_RMW_CODE_ID = '5ab0e9b8a3b2ca7c5e000015';
export const MANDAAT_LID_VAST_BUREAU_CODE_ID = '5ab0e9b8a3b2ca7c5e000017';
export const TEXT_CUSTOM_DISPLAY_TYPE_ID =
'220427c5-65c7-4be6-b84a-fc7401b465ec';
14 changes: 14 additions & 0 deletions tests/unit/models/display-type-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { module, test } from 'qunit';

import { setupTest } from 'frontend-lmb/tests/helpers';

module('Unit | Model | display type', function (hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function (assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('display-type', {});
assert.ok(model);
});
});
14 changes: 14 additions & 0 deletions tests/unit/models/library-entry-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { module, test } from 'qunit';

import { setupTest } from 'frontend-lmb/tests/helpers';

module('Unit | Model | library entry', function (hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function (assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('library-entry', {});
assert.ok(model);
});
});

0 comments on commit 960760f

Please sign in to comment.