Skip to content

Commit

Permalink
fix: auto research navigation orientation to cartographie (#448)
Browse files Browse the repository at this point in the history
* fix: auto research navigation orientation to cartographie

* fix: delete import not use

* feat: use id of research instead of type for more accuracy

* fix: test with adresse ids
  • Loading branch information
abelkhay committed Feb 5, 2024
1 parent 9cfda14 commit eb5fd35
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const SEARCH_DEBOUNCE_TIME: number = 300;
const setZoomUserPosition = (defaultUserPosition: number, distance?: number): number =>
distance ? (distance >= 50000 && distance <= 100000 ? 8 : 10) : defaultUserPosition;

const findAddressById = (
addresses: ResultFoundPresentation<{ type: AddressType }>[],
id?: string
): ResultFoundPresentation<{ type: AddressType }> | undefined => addresses.find((address) => address.id === id);

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'app-user-location',
Expand All @@ -34,6 +39,8 @@ const setZoomUserPosition = (defaultUserPosition: number, distance?: number): nu
export class UserLocationComponent implements OnInit {
@Input() adresse?: string;

@Input() adresseId?: string;

@Input() fullWidth: boolean = false;

@Input() placeholder: string = 'Entrez une adresse';
Expand Down Expand Up @@ -82,9 +89,12 @@ export class UserLocationComponent implements OnInit {
map((resultsToCombine: ResultFoundPresentation<{ type: AddressType }>[][]) => resultsToCombine.flat()),
tap(
(addressesFound: ResultFoundPresentation<{ type: AddressType }>[]) =>
addressesFound[0] && this.onSelected(addressesFound[0])
addressesFound && this.onSelected(findAddressById(addressesFound, this.adresseId) || addressesFound[0])
),
map((addressesFound: ResultFoundPresentation<{ type: AddressType }>[]) => addressesFound[0]?.label)
map(
(addressesFound: ResultFoundPresentation<{ type: AddressType }>[]) =>
findAddressById(addressesFound, this.adresseId)?.label || addressesFound[0].label
)
);

@Output() public resultFound: EventEmitter<ResultFoundPresentation<{ type: AddressType | 'user' }>> = new EventEmitter<
Expand Down Expand Up @@ -129,7 +139,7 @@ export class UserLocationComponent implements OnInit {
localisation,
setZoomUserPosition(this._zoomLevel.userPosition, parseInt(this.route.snapshot.queryParams['distance']))
);
this.resultFound.emit({ context: '', label: '', localisation, payload: { type: 'user' } });
this.resultFound.emit({ id: '', context: '', label: '', localisation, payload: { type: 'user' } });

this._loadingState$.next(false);
this._displayGeolocation$.next(false);
Expand Down
2 changes: 2 additions & 0 deletions src/features/adresse/data/http/address/address.http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('address http', (): void => {
coordinates: [4.8375548, 45.7665478]
},
properties: {
id: 'addressTransferId',
context: '80, Somme, Hauts-de-France',
label: '8 Boulevard du Port 80000 Amiens',
type: 'housenumber'
Expand All @@ -32,6 +33,7 @@ describe('address http', (): void => {

expect(addresses).toStrictEqual([
{
id: 'addressTransferId',
context: '80, Somme, Hauts-de-France',
label: '8 Boulevard du Port 80000 Amiens',
localisation: Localisation({
Expand Down
2 changes: 2 additions & 0 deletions src/features/adresse/data/http/address/address.http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class AddressHttp extends AddressRepository {
map((addressTransfer: AddressTransfer): Address[] =>
addressTransfer.features.map(
(addressTransferFeature: Feature<Point, AddressTransferProperties>): Address => ({
id: addressTransferFeature.properties.id,
context: addressTransferFeature.properties.context,
label: addressTransferFeature.properties.label,
localisation: Localisation({
Expand All @@ -46,6 +47,7 @@ export class AddressHttp extends AddressRepository {
map((addressTransfer: AddressTransfer): Address[] =>
addressTransfer.features.map(
(addressTransferFeature: Feature<Point, AddressTransferProperties>): Address => ({
id: addressTransferFeature.properties.id,
context: addressTransferFeature.properties.context,
label: addressTransferFeature.properties.label,
localisation: Localisation({
Expand Down
1 change: 1 addition & 0 deletions src/features/adresse/data/transfers/address.transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FeatureCollection, Point } from 'geojson';
import { AddressType } from '../../models';

export type AddressTransferProperties = {
id: string;
label: string;
context: string;
type: AddressType;
Expand Down
1 change: 1 addition & 0 deletions src/features/adresse/models/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Localisation } from '@gouvfr-anct/lieux-de-mediation-numerique';
export type AddressType = 'housenumber' | 'street' | 'locality' | 'municipality';

export interface Address {
id: string;
context: string;
label: string;
localisation: Localisation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('address presenter', (): void => {
search$: (): Observable<Address[]> => {
return of([
{
id: 'testId-01',
context: '80, Somme, Hauts-de-France',
label: '8 Boulevard du Port 80000 Amiens',
localisation: Localisation({
Expand All @@ -24,6 +25,7 @@ describe('address presenter', (): void => {
reverse$: (): Observable<Address[]> => {
return of([
{
id: 'testId-01',
context: '80, Somme, Hauts-de-France',
label: '8 Boulevard du Port 80000 Amiens',
localisation: Localisation({
Expand All @@ -50,6 +52,7 @@ describe('address presenter', (): void => {

expect(addressesFound).toStrictEqual([
{
id: 'testId-01',
context: '80, Somme, Hauts-de-France',
label: '8 Boulevard du Port 80000 Amiens',
localisation: Localisation({
Expand All @@ -62,6 +65,7 @@ describe('address presenter', (): void => {

expect(addressesFoundReverse).toStrictEqual([
{
id: 'testId-01',
context: '80, Somme, Hauts-de-France',
label: '8 Boulevard du Port 80000 Amiens',
localisation: Localisation({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { map } from 'rxjs/operators';
const toResultFound = (addresses: Address[]): ResultFoundPresentation<{ type: AddressType }>[] =>
addresses.map(
(address: Address): ResultFoundPresentation<{ type: AddressType }> => ({
id: address.id,
context: address.context,
label: address.label,
localisation: address.localisation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Localisation } from '@gouvfr-anct/lieux-de-mediation-numerique';
import { WithType } from '../../configuration';

export interface ResultFoundPresentation<T extends WithType = WithType> {
id: string;
context: string;
label: string;
localisation: Localisation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="card border-0 shadow m-auto">
<div class="card-body p-5">
<div class="d-flex flex-column text-center">
<ng-container *ngIf="fromOrientation; else: welcome">
<ng-container *ngIf="(fromOrientation$ | async); else: welcome">
<p class="text-muted">Patientez quelques instants</p>
<h3 class="card-title text-primary">
Nous recherchons les lieux
Expand Down Expand Up @@ -35,7 +35,8 @@ <h3 class="card-title text-primary">
placeholder="Recherchez un lieu ou une adresse"
class="me-3 d-sm-inline-block d-none"
[adresse]="(defaultAddress$ | async) ?? ''"
[fromOrientation]="this.fromOrientation"
[adresseId]="(defaultAddressId$ | async) ?? ''"
[fromOrientation]="(this.fromOrientation$ | async) ?? false"
(resultFound)="onResultFound($event)"></app-user-location>
<app-location-breadcrumb
id="breadcrumb"
Expand Down Expand Up @@ -97,8 +98,9 @@ <h3 class="card-title text-primary">
placeholder="Recherchez un lieu ou une adresse"
class="m-3 d-sm-none d-block"
[adresse]="(defaultAddress$ | async) ?? ''"
[adresseId]="(defaultAddressId$ | async) ?? ''"
[fullWidth]="true"
[fromOrientation]="this.fromOrientation"
[fromOrientation]="(this.fromOrientation$ | async) ?? false"
(resultFound)="onResultFound($event)"></app-user-location>
<app-location-breadcrumb
class="d-lg-none d-block"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
import { ClustersPresenter } from '../../../core/presenters/clusters';
import { ifAny } from '../../../core/utilities';
import { AddressType, ResultFoundPresentation } from '../../../adresse';
import { LieuMediationNumeriqueCluster } from '../../components';
import { Cluster } from '../../models';
import {
DEPARTEMENT_ZOOM_LEVEL,
Expand Down Expand Up @@ -156,7 +155,14 @@ export class CartographieLayout {
map((paramMap: ParamMap) => paramMap.get('address'))
);

public fromOrientation: boolean = Object.keys(this.route.snapshot.queryParams).length > 0;
public defaultAddressId$: Observable<string | null> = this.route.queryParamMap.pipe(
map((paramMap: ParamMap) => paramMap.get('addressId'))
);

private _fromOrientation$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(
Object.keys(this.route.snapshot.queryParams).length > 0
);
public readonly fromOrientation$: Observable<boolean> = this._fromOrientation$.asObservable();

private readonly _userLocalisation$: BehaviorSubject<Localisation> = new BehaviorSubject<Localisation>(NO_LOCALISATION);
public readonly userLocalisation$: Observable<Localisation> = this._userLocalisation$.asObservable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const onlyMatchingConseillerNom =
conseiller.nom.toLowerCase().includes(searchTerm.toLowerCase());

const toResultFound = (conseiller: Conseiller): ResultFoundPresentation<{ type: 'conseiller' }> => ({
id: conseiller.id,
context: conseiller.structurePorteuse.adresse,
label: conseiller.nom,
localisation: Localisation({
Expand Down
3 changes: 3 additions & 0 deletions src/features/core/presenters/filter/filter.presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type FilterPresentation = {
export type FilterQueryParamsPresentation = {
service?: Service;
address?: string;
address_id?: string;
latitude?: `${number}`;
longitude?: `${number}`;
distance?: `${number}`;
Expand All @@ -61,6 +62,7 @@ export type FilterFormPresentation = FilterPresentation & {
address?: string;
latitude?: number;
longitude?: number;
addressId?: string;
};

const wrapInArray = <T>(params: string | string[]): T[] => (Array.isArray(params) ? params : [params]) as unknown as T[];
Expand Down Expand Up @@ -92,6 +94,7 @@ export const hasActiveFilter = (filterFormPresentation: FilterFormPresentation):
export const toFilterFormPresentationFromQuery = (queryParams?: FilterQueryParamsPresentation): FilterFormPresentation => ({
service: queryParams?.service,
address: queryParams?.address,
addressId: queryParams?.address_id,
latitude: queryParams?.latitude ? parseFloat(queryParams.latitude) : undefined,
longitude: queryParams?.longitude ? parseFloat(queryParams.longitude) : undefined,
distance: queryParams?.distance ? parseInt(queryParams.distance) : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,7 @@ describe('lieux-mediation-numerique-list presenter', (): void => {

expect<ResultFoundPresentation<{ id: string; type: string }>[]>(searchResults).toStrictEqual([
{
id: '288b41b0-20ef-426c-9cac-0af99203365d',
context: '12 BIS RUE DE LECLERCQ 51100, Reims',
label: 'Anonymal',
localisation: Localisation({ latitude: 46.2814605, longitude: 4.468874 }),
Expand Down Expand Up @@ -2419,6 +2420,7 @@ describe('lieux-mediation-numerique-list presenter', (): void => {

expect<ResultFoundPresentation<{ id: string; type: string }>[]>(searchResults).toStrictEqual([
{
id: '288b41b0-20ef-426c-9cac-0af99203365d',
context: '12 BIS RUE DE LECLERCQ 51100, Reims',
label: 'Anonymal',
localisation: Localisation({ latitude: 46.2814605, longitude: 4.468874 }),
Expand Down Expand Up @@ -2466,6 +2468,7 @@ describe('lieux-mediation-numerique-list presenter', (): void => {

expect<ResultFoundPresentation<{ id: string; type: string }>[]>(searchResults).toStrictEqual([
{
id: '288b41b0-20ef-426c-9cac-0af99203365d',
context: '12 BIS RUE DE LECLERCQ 51100, Reims',
label: 'Anonymal Reims',
localisation: Localisation({ latitude: 46.2814605, longitude: 4.468874 }),
Expand All @@ -2475,6 +2478,7 @@ describe('lieux-mediation-numerique-list presenter', (): void => {
}
},
{
id: 'd27dc31b-cb32-488f-9cfd-01baa4dcbae4',
context: '17 rue Paul Bellamy 44000, Nantes',
label: 'Anonymal Nantes',
localisation: Localisation({ latitude: 46.2814605, longitude: 4.468874 }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ const toResultFound = ({
}: LieuMediationNumerique & {
localisation: Localisation;
}): ResultFoundPresentation<{ id: string; type: 'place' }> => ({
id: id,
context: `${adresse.voie} ${adresse.code_postal}, ${adresse.commune}`,
label: nom,
payload: { id, type: 'place' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class LocalisationPage {
}

public onSelectAddress(address: ResultFoundPresentation): void {
this.orientationLayout.filterForm.get('addressId')?.setValue(address.id);
this.orientationLayout.filterForm.get('address')?.setValue(address.label);
this.orientationLayout.filterForm.get('latitude')?.setValue(address.localisation.latitude);
this.orientationLayout.filterForm.get('longitude')?.setValue(address.localisation.longitude);
Expand Down

0 comments on commit eb5fd35

Please sign in to comment.