-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b06696
commit 26c6af9
Showing
16 changed files
with
318 additions
and
29 deletions.
There are no files selected for viewing
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
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
30 changes: 30 additions & 0 deletions
30
front/src/core-logic/adapters/NafGateway/HttpNafGateway.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,30 @@ | ||
import { Observable, from } from "rxjs"; | ||
import { NafRoutes, NafSectionSuggestion } from "shared"; | ||
import { HttpClient } from "shared-routes"; | ||
import { | ||
otherwiseThrow, | ||
throwBadRequestWithExplicitMessage, | ||
} from "src/core-logic/adapters/otherwiseThrow"; | ||
import { NafGateway } from "src/core-logic/ports/NafGateway"; | ||
import { match } from "ts-pattern"; | ||
|
||
export class HttpNafGateway implements NafGateway { | ||
constructor(private readonly httpClient: HttpClient<NafRoutes>) {} | ||
|
||
getNafSuggestions$(searchText: string): Observable<NafSectionSuggestion[]> { | ||
return from( | ||
this.httpClient | ||
.sectionSuggestions({ | ||
queryParams: { | ||
searchText, | ||
}, | ||
}) | ||
.then((response) => | ||
match(response) | ||
.with({ status: 200 }, ({ body }) => body) | ||
.with({ status: 400 }, throwBadRequestWithExplicitMessage) | ||
.otherwise(otherwiseThrow), | ||
), | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
front/src/core-logic/adapters/NafGateway/SimulatedNafGateway.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,32 @@ | ||
import { Observable, Subject, delay, of } from "rxjs"; | ||
import { NafSectionSuggestion } from "shared"; | ||
import { NafGateway } from "src/core-logic/ports/NafGateway"; | ||
|
||
export class SimulatedNafGateway implements NafGateway { | ||
public nafSuggestions$ = new Subject<NafSectionSuggestion[]>(); | ||
|
||
constructor(private readonly simulatedLatency = 0) {} | ||
|
||
#simulatedResponse: NafSectionSuggestion[] = [ | ||
{ | ||
label: "Agriculture, sylviculture et pêche", | ||
nafCodes: ["1000A", "1000B"], | ||
}, | ||
{ | ||
label: "Industries extractives", | ||
nafCodes: ["1000C", "1000D"], | ||
}, | ||
{ | ||
label: "Industrie manufacturière", | ||
nafCodes: ["1000E", "1000F"], | ||
}, | ||
]; | ||
|
||
getNafSuggestions$(searchTerm: string): Observable<NafSectionSuggestion[]> { | ||
return of( | ||
this.#simulatedResponse.filter((suggestion) => | ||
suggestion.label.includes(searchTerm), | ||
), | ||
).pipe(delay(this.simulatedLatency)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
front/src/core-logic/adapters/NafGateway/TestNafGateway.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,11 @@ | ||
import { Observable, Subject } from "rxjs"; | ||
import { NafSectionSuggestion } from "shared"; | ||
import { NafGateway } from "src/core-logic/ports/NafGateway"; | ||
|
||
export class TestNafGateway implements NafGateway { | ||
public nafSuggestions$ = new Subject<NafSectionSuggestion[]>(); | ||
|
||
getNafSuggestions$(_searchTerm: string): Observable<NafSectionSuggestion[]> { | ||
return this.nafSuggestions$; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
debounceTime, | ||
distinctUntilChanged, | ||
filter, | ||
map, | ||
switchMap, | ||
} from "rxjs"; | ||
import { catchEpicError } from "src/core-logic/storeConfig/catchEpicError"; | ||
import { | ||
ActionOfSlice, | ||
AppEpic, | ||
} from "src/core-logic/storeConfig/redux.helpers"; | ||
import { nafSlice } from "./naf.slice"; | ||
|
||
type NafAction = ActionOfSlice<typeof nafSlice>; | ||
|
||
const queryMinLength = 3; | ||
const debounceDuration = 500; | ||
|
||
const queryHasChangedEpic: AppEpic<NafAction> = ( | ||
action$, | ||
_state$, | ||
{ scheduler }, | ||
) => | ||
action$.pipe( | ||
filter(nafSlice.actions.queryHasChanged.match), | ||
map((action) => ({ ...action, payload: action.payload.trim() })), | ||
filter((action) => action.payload.length >= queryMinLength), | ||
debounceTime(debounceDuration, scheduler), | ||
distinctUntilChanged(), | ||
map((action) => nafSlice.actions.searchSectionsRequested(action.payload)), | ||
); | ||
|
||
const searchSectionsEpic: AppEpic<NafAction> = ( | ||
action$, | ||
_state$, | ||
{ nafGateway }, | ||
) => | ||
action$.pipe( | ||
filter(nafSlice.actions.searchSectionsRequested.match), | ||
switchMap((action) => nafGateway.getNafSuggestions$(action.payload)), | ||
map((suggestions) => nafSlice.actions.searchSectionsSucceeded(suggestions)), | ||
catchEpicError(nafSlice.actions.searchSectionsFailed), | ||
); | ||
|
||
export const nafEpics = [queryHasChangedEpic, searchSectionsEpic]; |
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,16 @@ | ||
import { createSelector } from "@reduxjs/toolkit"; | ||
import { createRootSelector } from "src/core-logic/storeConfig/store"; | ||
|
||
const nafState = createRootSelector((state) => state.naf); | ||
|
||
const isLoading = createSelector(nafState, (state) => state.isLoading); | ||
|
||
const currentNafSections = createSelector( | ||
nafState, | ||
(state) => state.currentNafSections, | ||
); | ||
|
||
export const nafSelectors = { | ||
isLoading, | ||
currentNafSections, | ||
}; |
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,40 @@ | ||
import { PayloadAction, createSlice } from "@reduxjs/toolkit"; | ||
import { NafSectionSuggestion } from "shared"; | ||
|
||
export type NafState = { | ||
isLoading: boolean; | ||
currentNafSections: NafSectionSuggestion[]; | ||
}; | ||
|
||
export const initialState: NafState = { | ||
isLoading: false, | ||
currentNafSections: [], | ||
}; | ||
|
||
export const nafSlice = createSlice({ | ||
name: "naf", | ||
initialState, | ||
reducers: { | ||
queryHasChanged: (state, _action: PayloadAction<string>) => { | ||
state.currentNafSections = []; | ||
}, | ||
queryWasEmptied: (state) => { | ||
state.isLoading = false; | ||
state.currentNafSections = []; | ||
}, | ||
searchSectionsRequested: (state, _action: PayloadAction<string>) => { | ||
state.isLoading = true; | ||
}, | ||
searchSectionsSucceeded: ( | ||
state, | ||
action: PayloadAction<NafSectionSuggestion[]>, | ||
) => { | ||
state.currentNafSections = action.payload; | ||
state.isLoading = false; | ||
}, | ||
searchSectionsFailed: (state, _action) => { | ||
state.isLoading = false; | ||
state.currentNafSections = []; | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.