Skip to content

Commit

Permalink
finish up logic of truncated addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
elpoelma committed Jan 23, 2025
1 parent 1263911 commit c89907b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 74 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-comics-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lblod/ember-rdfa-editor-lblod-plugins': minor
---

Add support for inserting truncated addresses
42 changes: 13 additions & 29 deletions addon/components/location-plugin/edit.gts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ export default class LocationPluginEditComponent extends Component<Signature> {
@trackedReset({
memo: 'currentAddress',
update(component: LocationPluginEditComponent) {
return component.currentTruncateAddressValue;
return component.currentTruncatedValue;
},
})
newTruncateAddressValue?: boolean;
newTruncatedValue?: boolean;

get message(): Message | undefined {
const value = this.newAddress.value as Address | undefined;
Expand Down Expand Up @@ -192,12 +192,8 @@ export default class LocationPluginEditComponent extends Component<Signature> {
}
}

get currentTruncateAddressValue() {
if (this.currentAddress instanceof Address) {
return this.currentAddress.truncated;
} else {
return undefined;
}
get currentTruncatedValue() {
return this.currentAddress?.truncated;
}

get canUpdateStreet() {
Expand Down Expand Up @@ -230,15 +226,15 @@ export default class LocationPluginEditComponent extends Component<Signature> {
newMunicipality,
newHousenumber,
newBusnumber,
newTruncateAddressValue,
newTruncatedValue,
} = this;
if (
this.currentAddress &&
newStreetName === this.currentAddress.street &&
newMunicipality === this.currentAddress.municipality &&
newHousenumber === this.currentAddress.housenumber &&
newBusnumber === this.currentAddress.busnumber &&
newTruncateAddressValue === this.currentAddress.truncated
newTruncatedValue === this.currentAddress.truncated
) {
// No need to re-search, nothing has changed
return;
Expand All @@ -252,7 +248,7 @@ export default class LocationPluginEditComponent extends Component<Signature> {
municipality: newMunicipality,
busnumber: newBusnumber,
housenumber: newHousenumber,
truncated: newTruncateAddressValue,
truncated: newTruncatedValue ?? false,
})
) {
return this.currentAddress;
Expand All @@ -265,7 +261,7 @@ export default class LocationPluginEditComponent extends Component<Signature> {
municipality: newMunicipality,
housenumber: newHousenumber,
busnumber: newBusnumber,
truncated: newTruncateAddressValue,
truncated: newTruncatedValue ?? false,
},
this.args.nodeContentsUtils,
);
Expand All @@ -276,7 +272,7 @@ export default class LocationPluginEditComponent extends Component<Signature> {
{
street: newStreetName,
municipality: newMunicipality,
truncated: newTruncateAddressValue,
truncated: newTruncatedValue ?? false,
},
this.args.nodeContentsUtils,
);
Expand All @@ -295,18 +291,7 @@ export default class LocationPluginEditComponent extends Component<Signature> {
this.args.setIsLoading?.(false);
}
} else {
const address = await resolveAddress(
{
street: newStreetName,
municipality: newMunicipality,
housenumber: newHousenumber,
busnumber: newBusnumber,
truncated: true,
},
this.args.nodeContentsUtils,
);
this.args.setAddressToInsert(address);
return address;
return;
}
});

Expand All @@ -318,7 +303,7 @@ export default class LocationPluginEditComponent extends Component<Signature> {
this.newStreetName,
this.newHousenumber,
this.newBusnumber,
this.newTruncateAddressValue,
this.newTruncatedValue,
],
);

Expand Down Expand Up @@ -355,8 +340,7 @@ export default class LocationPluginEditComponent extends Component<Signature> {

@action
truncateAddress(value: boolean) {
this.newTruncateAddressValue = value;
console.log('this.newTruncateAddressValue', this.newTruncateAddressValue);
this.newTruncatedValue = value;
}

searchMunicipality = restartableTask(async (term: string) => {
Expand Down Expand Up @@ -507,7 +491,7 @@ export default class LocationPluginEditComponent extends Component<Signature> {
</AuFormRow>
{{#if this.newStreetName}}
<AuCheckbox
@checked={{this.newTruncateAddressValue}}
@checked={{this.newTruncatedValue}}
@onChange={{this.truncateAddress}}
>{{t 'location-plugin.modal.checkbox-message'}}</AuCheckbox>
<AuHelptext @skin='tertiary'>{{t
Expand Down
121 changes: 81 additions & 40 deletions addon/plugins/location-plugin/node-contents/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ADRES,
ADRES_TYPO,
DCT,
EXT,
LOCN,
} from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { findChildWithRdfaAttribute } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/namespace';
Expand All @@ -12,45 +13,70 @@ import { constructGeometrySpec } from './point';
import { type NodeContentsUtils } from './';

export const constructAddressSpec = (address: Address) => {
const housenumberNode = address.housenumber
? [
' ',
span(
{ property: ADRES('Adresvoorstelling.huisnummer').full },
address.housenumber,
),
]
: [];
const busnumberNode = address.busnumber
? [
' bus ',
span(
{ property: ADRES('Adresvoorstelling.busnummer').full },
address.busnumber,
),
]
: [];
const belgianUriNode = address.belgianAddressUri
? [
span({
property: ADRES('verwijstNaar').full,
resource: address.belgianAddressUri,
}),
]
: [];
const streetNode = span(
{
property: LOCN('thoroughfare').full,
},
address.street,
);
const housenumberNode =
address.housenumber &&
span(
{ property: ADRES('Adresvoorstelling.huisnummer').full },
address.housenumber,
);
const busnumberNode =
address.busnumber &&
span(
{ property: ADRES('Adresvoorstelling.busnummer').full },
address.busnumber,
);
const zipcodeNode = address.truncated
? span({
property: LOCN('postcode').full,
content: address.zipcode,
})
: span(
{
property: LOCN('postcode').full,
},
address.zipcode,
);
const municipalityNode = address.truncated
? span({
property: ADRES('gemeentenaam').full,
language: 'nl',
content: address.municipality,
})
: span(
{
property: ADRES('gemeentenaam').full,
language: 'nl',
},
address.municipality,
);
const belgianUriNode =
address.belgianAddressUri &&
span({
property: ADRES('verwijstNaar').full,
resource: address.belgianAddressUri,
});
const truncatedNode = span({
property: EXT('truncated').full,
content: address.truncated,
});

return span(
{ resource: address.uri, typeof: LOCN('Address').full },
span(
{
property: LOCN('thoroughfare').full,
},
address.street,
),
...housenumberNode,
...busnumberNode,
', ',
...belgianUriNode,
streetNode,
...(housenumberNode ? [' ', housenumberNode] : []),
...(busnumberNode ? [' bus ', busnumberNode] : []),
address.truncated ? '' : ', ',
zipcodeNode,
address.truncated ? '' : ' ',
municipalityNode,
...(belgianUriNode ? [belgianUriNode] : []),
truncatedNode,
constructGeometrySpec(address.location, ADRES('positie')),
);
};
Expand Down Expand Up @@ -113,6 +139,7 @@ export const parseOldAddressElement =
municipality,
busnumber: busnumber ?? undefined,
location,
truncated: false,
});
} else {
return;
Expand Down Expand Up @@ -163,16 +190,29 @@ export const parseAddressElement =
'property',
ADRES('Adresvoorstelling.busnummer'),
)?.textContent;
const zipcode = findChildWithRdfaAttribute(
const zipCodeNode = findChildWithRdfaAttribute(
addressNode,
'property',
LOCN('postcode'),
)?.textContent;
const municipality = findChildWithRdfaAttribute(
);
const zipcode =
zipCodeNode?.getAttribute('content') ?? zipCodeNode?.textContent;
const municipalityNode = findChildWithRdfaAttribute(
addressNode,
'property',
ADRES('gemeentenaam'),
)?.textContent;
);

const municipality =
municipalityNode?.getAttribute('content') ??
municipalityNode?.textContent;

const truncated =
findChildWithRdfaAttribute(
addressNode,
'property',
EXT('truncated'),
)?.getAttribute('content') === 'true';

const pointNode = findChildWithRdfaAttribute(
addressNode,
Expand All @@ -191,6 +231,7 @@ export const parseAddressElement =
municipality,
busnumber: busnumber ?? undefined,
location,
truncated,
});
} else {
return;
Expand Down
12 changes: 8 additions & 4 deletions addon/plugins/location-plugin/utils/address-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,15 @@ export class Address {
sameAs(
other?: Pick<
Address,
'street' | 'housenumber' | 'busnumber' | 'municipality'
'street' | 'housenumber' | 'busnumber' | 'municipality' | 'truncated'
> | null,
) {
return (
this.street === other?.street &&
this.housenumber === other?.housenumber &&
this.busnumber === other?.busnumber &&
this.municipality === other?.municipality
this.municipality === other?.municipality &&
this.truncated === other?.truncated
);
}

Expand Down Expand Up @@ -256,6 +257,7 @@ export async function fetchStreets(term: string, municipality: string) {
type StreetInfo = {
municipality: string;
street: string;
truncated: boolean;
};

export async function resolveStreet(
Expand Down Expand Up @@ -283,7 +285,7 @@ export async function resolveStreet(
street: unwrap(streetinfo.Thoroughfarename),
municipality: streetinfo.Municipality,
zipcode: unwrap(streetinfo.Zipcode),
truncated: false,
truncated: info.truncated,
location: new Point({
uri: nodeContentsUtils.fallbackGeometryUri(),
location: {
Expand Down Expand Up @@ -317,6 +319,7 @@ type AddressInfo = {
street: string;
housenumber: string;
busnumber?: string;
truncated: boolean;
};

export async function resolveAddress(
Expand All @@ -340,7 +343,7 @@ export async function resolveAddress(
municipality: result.gemeente.gemeentenaam.geografischeNaam.spelling,
uri: nodeContentsUtils.fallbackAddressUri(),
belgianAddressUri: result.identificator.id,
truncated: false,
truncated: info.truncated,
location: new Point({
uri: `${result.identificator.id}/1`,
location: {
Expand All @@ -361,6 +364,7 @@ export async function resolveAddress(
{
street: info.street,
municipality: info.municipality,
truncated: info.truncated,
},
nodeContentsUtils,
);
Expand Down
2 changes: 1 addition & 1 deletion addon/utils/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function findChildWithRdfaAttribute(
attr: string,
value: Resource,
) {
return [...element.children].find((child) => {
return Array.from(element.children).find((child) => {
const result = child.getAttribute(attr)?.split(' ');
return result?.includes(value.full) || result?.includes(value.prefixed);
});
Expand Down

0 comments on commit c89907b

Please sign in to comment.