[Front-end] Address model refactoring

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-04-21 11:43:24 +02:00
parent 0c49ddc65b
commit dc75a9beb3
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
1 changed files with 82 additions and 69 deletions

View File

@ -65,26 +65,40 @@ export class Address implements IAddress {
} }
get poiInfos(): IPoiInfo { get poiInfos(): IPoiInfo {
return addressToPoiInfos(this);
}
get fullName(): string {
return addressFullName(this);
}
get iconForPOI(): IPOIIcon {
return iconForAddress(this);
}
}
export function addressToPoiInfos(address: IAddress): IPoiInfo {
/* generate name corresponding to poi type */ /* generate name corresponding to poi type */
let name = ""; let name = "";
let alternativeName = ""; let alternativeName = "";
let poiIcon: IPOIIcon = poiIcons.default; let poiIcon: IPOIIcon = poiIcons.default;
let addressType = address.type;
// Google Maps doesn't have a type // Google Maps doesn't have a type
if (this.type == null && this.description === this.street) { if (address.type == null && address.description === address.street) {
this.type = "house"; addressType = "house";
} }
switch (this.type) { switch (addressType) {
case "house": case "house":
name = this.description; name = address.description;
alternativeName = [this.postalCode, this.locality, this.country] alternativeName = [address.postalCode, address.locality, address.country]
.filter((zone) => zone) .filter((zone) => zone)
.join(", "); .join(", ");
poiIcon = poiIcons.defaultAddress; poiIcon = poiIcons.defaultAddress;
break; break;
case "street": case "street":
case "secondary": case "secondary":
name = this.description; name = address.description;
alternativeName = [this.postalCode, this.locality, this.country] alternativeName = [address.postalCode, address.locality, address.country]
.filter((zone) => zone) .filter((zone) => zone)
.join(", "); .join(", ");
poiIcon = poiIcons.defaultStreet; poiIcon = poiIcons.defaultStreet;
@ -92,38 +106,47 @@ export class Address implements IAddress {
case "zone": case "zone":
case "city": case "city":
case "administrative": case "administrative":
name = this.postalCode name = address.postalCode
? `${this.description} (${this.postalCode})` ? `${address.description} (${address.postalCode})`
: this.description; : address.description;
alternativeName = [this.region, this.country] alternativeName = [address.region, address.country]
.filter((zone) => zone) .filter((zone) => zone)
.join(", "); .join(", ");
poiIcon = poiIcons.defaultAdministrative; poiIcon = poiIcons.defaultAdministrative;
break; break;
default: default:
// POI // POI
name = this.description; name = address.description;
alternativeName = ""; alternativeName = "";
if (this.street && this.street.trim()) { if (address.street && address.street.trim()) {
alternativeName = `${this.street}`; alternativeName = `${address.street}`;
if (this.locality) { if (address.locality) {
alternativeName += ` (${this.locality})`; alternativeName += ` (${address.locality})`;
} }
} else if (this.locality && this.locality.trim()) { } else if (address.locality && address.locality.trim()) {
alternativeName = `${this.locality}, ${this.region}, ${this.country}`; alternativeName = `${address.locality}, ${address.region}, ${address.country}`;
} else if (this.region && this.region.trim()) { } else if (address.region && address.region.trim()) {
alternativeName = `${this.region}, ${this.country}`; alternativeName = `${address.region}, ${address.country}`;
} else if (this.country && this.country.trim()) { } else if (address.country && address.country.trim()) {
alternativeName = this.country; alternativeName = address.country;
} }
poiIcon = this.iconForPOI; poiIcon = iconForAddress(address);
break; break;
} }
return { name, alternativeName, poiIcon }; return { name, alternativeName, poiIcon };
} }
get fullName(): string { export function iconForAddress(address: IAddress): IPOIIcon {
const { name, alternativeName } = this.poiInfos; if (address.type == null) {
return poiIcons.default;
}
const type = address.type.split(":").pop() || "";
if (poiIcons[type]) return poiIcons[type];
return poiIcons.default;
}
export function addressFullName(address: IAddress): string {
const { name, alternativeName } = addressToPoiInfos(address);
if (name && alternativeName) { if (name && alternativeName) {
return `${name}, ${alternativeName}`; return `${name}, ${alternativeName}`;
} }
@ -131,14 +154,4 @@ export class Address implements IAddress {
return name; return name;
} }
return ""; return "";
}
get iconForPOI(): IPOIIcon {
if (this.type == null) {
return poiIcons.default;
}
const type = this.type.split(":").pop() || "";
if (poiIcons[type]) return poiIcons[type];
return poiIcons.default;
}
} }