Skip to content

Commit a57b8ef

Browse files
(PC-31577)[PRO] fix: Dont translate searched text in offers search input.
1 parent 824b3b6 commit a57b8ef

File tree

2 files changed

+144
-3
lines changed

2 files changed

+144
-3
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import {
2+
CollectiveOfferDisplayedStatus,
3+
EacFormat,
4+
OfferStatus,
5+
} from 'apiClient/v1'
6+
import { Audience } from 'core/shared/types'
7+
import {
8+
translateApiParamsToQueryParams,
9+
translateQueryParamsToApiParams,
10+
} from 'utils/translate'
11+
12+
describe('translate', () => {
13+
it('should translate between query params to api params for collective offers', () => {
14+
expect(
15+
translateQueryParamsToApiParams(
16+
{
17+
'nom-ou-isbn': 'input de recherche',
18+
structure: '883',
19+
lieu: '891',
20+
format: 'Concert',
21+
'periode-evenement-debut': '2024-08-08',
22+
'periode-evenement-fin': '2024-08-24',
23+
statut: ['en-attente', 'active'],
24+
},
25+
Audience.COLLECTIVE
26+
)
27+
).toEqual(
28+
expect.objectContaining({
29+
nameOrIsbn: 'input de recherche',
30+
offererId: '883',
31+
venueId: '891',
32+
format: 'Concert',
33+
periodBeginningDate: '2024-08-08',
34+
periodEndingDate: '2024-08-24',
35+
status: ['PENDING', 'ACTIVE'],
36+
})
37+
)
38+
39+
expect(
40+
translateApiParamsToQueryParams(
41+
{
42+
nameOrIsbn: 'input de recherche',
43+
offererId: '883',
44+
venueId: '891',
45+
format: EacFormat.CONCERT,
46+
periodBeginningDate: '2024-08-08',
47+
periodEndingDate: '2024-08-24',
48+
status: [
49+
CollectiveOfferDisplayedStatus.PENDING,
50+
CollectiveOfferDisplayedStatus.ACTIVE,
51+
],
52+
},
53+
Audience.COLLECTIVE
54+
)
55+
).toEqual(
56+
expect.objectContaining({
57+
'nom-ou-isbn': 'input de recherche',
58+
structure: '883',
59+
lieu: '891',
60+
format: 'Concert',
61+
'periode-evenement-debut': '2024-08-08',
62+
'periode-evenement-fin': '2024-08-24',
63+
statut: ['en-attente', 'active'],
64+
})
65+
)
66+
})
67+
68+
it('should translate query params to api params for individual offers', () => {
69+
expect(
70+
translateQueryParamsToApiParams(
71+
{
72+
'nom-ou-isbn': 'input de recherche',
73+
structure: '883',
74+
lieu: '891',
75+
'periode-evenement-debut': '2024-08-08',
76+
'periode-evenement-fin': '2024-08-24',
77+
statut: 'draft',
78+
},
79+
Audience.INDIVIDUAL
80+
)
81+
).toEqual(
82+
expect.objectContaining({
83+
nameOrIsbn: 'input de recherche',
84+
offererId: '883',
85+
venueId: '891',
86+
periodBeginningDate: '2024-08-08',
87+
periodEndingDate: '2024-08-24',
88+
status: 'DRAFT',
89+
})
90+
)
91+
92+
expect(
93+
translateApiParamsToQueryParams(
94+
{
95+
nameOrIsbn: 'input de recherche',
96+
offererId: '883',
97+
venueId: '891',
98+
periodBeginningDate: '2024-08-08',
99+
periodEndingDate: '2024-08-24',
100+
status: OfferStatus.DRAFT,
101+
},
102+
Audience.INDIVIDUAL
103+
)
104+
).toEqual(
105+
expect.objectContaining({
106+
'nom-ou-isbn': 'input de recherche',
107+
structure: '883',
108+
lieu: '891',
109+
'periode-evenement-debut': '2024-08-08',
110+
'periode-evenement-fin': '2024-08-24',
111+
statut: 'draft',
112+
})
113+
)
114+
})
115+
116+
it('should not translate user inputs for collective offers', () => {
117+
expect(
118+
translateQueryParamsToApiParams(
119+
{
120+
'nom-ou-isbn': 'en-attente',
121+
},
122+
Audience.COLLECTIVE
123+
)
124+
).toEqual(expect.objectContaining({ nameOrIsbn: 'en-attente' }))
125+
})
126+
127+
it('should not translate user inputs for individual offers', () => {
128+
expect(
129+
translateQueryParamsToApiParams(
130+
{
131+
'nom-ou-isbn': 'remboursements',
132+
},
133+
Audience.INDIVIDUAL
134+
)
135+
).toEqual(expect.objectContaining({ nameOrIsbn: 'remboursements' }))
136+
})
137+
})

pro/src/utils/translate.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ const translateObjectKeysAndValues = (
88
originalObject: Record<string, any>,
99
translationsMap: Record<string, string>
1010
) => {
11+
const nonTranlatedKeys = ['nom-ou-isbn', 'nameOrIsbn'] // These keys should not have their values translated since it's user inputs
12+
1113
const result: Record<string, string> = {}
1214
Object.entries(originalObject).forEach(([originalKey, originalValue]) => {
1315
const translatedKey = translationsMap[originalKey] ?? originalKey
1416

15-
const translatedValue = Object.keys(translationsMap).includes(originalValue)
16-
? translationsMap[originalValue]
17-
: originalValue
17+
const translatedValue =
18+
Object.keys(translationsMap).includes(originalValue) &&
19+
!nonTranlatedKeys.includes(originalKey)
20+
? translationsMap[originalValue]
21+
: originalValue
1822

1923
result[translatedKey] = Array.isArray(translatedValue)
2024
? translatedValue.map((value) => translationsMap[value])

0 commit comments

Comments
 (0)