Skip to content

Commit 53ce415

Browse files
authored
Show only locales available in current project in Copy from another locale batch action (#4435)
1 parent c0dce2d commit 53ce415

6 files changed

Lines changed: 124 additions & 22 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { fetchAllLocales } from './other-locales';
2+
import { GET } from './utils/base';
3+
4+
vi.mock('./utils/base', () => ({
5+
GET: vi.fn(),
6+
}));
7+
8+
describe('fetchAllLocales', () => {
9+
afterEach(() => {
10+
vi.mocked(GET).mockReset();
11+
});
12+
13+
describe('all-projects', () => {
14+
it('hits the global locale list endpoint, not a project endpoint', async () => {
15+
vi.mocked(GET).mockResolvedValueOnce({ results: [], next: null });
16+
17+
await fetchAllLocales();
18+
19+
expect(GET).toHaveBeenCalledWith(
20+
expect.stringMatching(/^\/api\/v2\/locales\/\?/),
21+
);
22+
expect(GET).not.toHaveBeenCalledWith(
23+
expect.stringContaining('/api/v2/projects/'),
24+
);
25+
});
26+
27+
it('follows pagination until next is null', async () => {
28+
vi.mocked(GET)
29+
.mockResolvedValueOnce({
30+
results: [{ code: 'a', name: 'Locale A' }],
31+
next: '/api/v2/locales/?page=2',
32+
})
33+
.mockResolvedValueOnce({
34+
results: [{ code: 'b', name: 'Locale B' }],
35+
next: null,
36+
});
37+
38+
const result = await fetchAllLocales();
39+
40+
expect(GET).toHaveBeenCalledTimes(2);
41+
expect(result).toEqual([
42+
{ code: 'a', name: 'Locale A' },
43+
{ code: 'b', name: 'Locale B' },
44+
]);
45+
});
46+
47+
it('stops looping and does not hang if next is missing rather than null', async () => {
48+
vi.mocked(GET).mockResolvedValueOnce({
49+
results: [{ code: 'a', name: 'A' }],
50+
});
51+
const result = await fetchAllLocales();
52+
expect(result).toEqual([{ code: 'a', name: 'A' }]);
53+
});
54+
});
55+
});

‎translate/src/api/project.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GET } from './utils/base';
2+
import type { LocaleOption } from './other-locales';
23

34
export type Tag = {
45
readonly slug: string;
@@ -11,8 +12,16 @@ export type Project = {
1112
name: string;
1213
info: string;
1314
tags: Tag[];
15+
locales: LocaleOption[];
1416
};
1517

1618
export async function fetchProject(slug: string): Promise<Project> {
17-
return await GET(`/api/v2/projects/${slug}`);
19+
const result = await GET(`/api/v2/projects/${slug}`);
20+
21+
return {
22+
...result,
23+
locales: (result?.localizations ?? []).map(
24+
(l: { locale: LocaleOption }) => l.locale,
25+
),
26+
};
1827
}

‎translate/src/modules/batchactions/components/BatchActions.test.jsx‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React from 'react';
22

33
import * as Hooks from '~/hooks';
44
import * as Actions from '../actions';
5-
import * as OtherLocales from '~/api/other-locales';
65
import { BATCHACTIONS } from '../reducer';
76

87
import { BatchActions } from './BatchActions';
98
import { vi } from 'vitest';
109
import { fireEvent, render } from '@testing-library/react';
1110
import { MockLocalizationProvider } from '~/test/utils';
11+
import { PROJECT } from '~/modules/project';
1212

1313
const DEFAULT_BATCH_ACTIONS = {
1414
entities: [],
@@ -17,20 +17,28 @@ const DEFAULT_BATCH_ACTIONS = {
1717
response: null,
1818
};
1919

20-
vi.mock('~/hooks', () => ({
21-
useAppDispatch: vi.fn(() => vi.fn()),
22-
useAppSelector: vi.fn((selector) =>
23-
selector({ [BATCHACTIONS]: DEFAULT_BATCH_ACTIONS }),
24-
),
25-
}));
26-
2720
vi.mock('../actions', () => ({
2821
resetSelection: vi.fn(() => ({ type: 'whatever' })),
2922
selectAll: vi.fn(() => ({ type: 'whatever' })),
3023
}));
3124

32-
vi.mock('~/api/other-locales', () => ({
33-
fetchAllLocales: vi.fn(() => Promise.resolve([])),
25+
const DEFAULT_PROJECT_STATE = {
26+
fetching: false,
27+
slug: '',
28+
name: '',
29+
info: '',
30+
tags: [],
31+
locales: [],
32+
};
33+
34+
vi.mock('~/hooks', () => ({
35+
useAppDispatch: vi.fn(() => vi.fn()),
36+
useAppSelector: vi.fn((selector) =>
37+
selector({
38+
[BATCHACTIONS]: DEFAULT_BATCH_ACTIONS,
39+
[PROJECT]: DEFAULT_PROJECT_STATE,
40+
}),
41+
),
3442
}));
3543

3644
describe('<BatchActions>', () => {
@@ -39,7 +47,6 @@ describe('<BatchActions>', () => {
3947
Hooks.useAppSelector.mockRestore();
4048
Actions.resetSelection.mockRestore();
4149
Actions.selectAll.mockRestore();
42-
OtherLocales.fetchAllLocales.mockClear();
4350
});
4451

4552
const WrapBatchAction = () => {

‎translate/src/modules/batchactions/components/BatchActions.tsx‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import './BatchActions.css';
1919
import { RejectAll } from './RejectAll';
2020
import { ReplaceAll } from './ReplaceAll';
2121
import { CopyFromLocale } from './CopyFromLocale';
22-
import { fetchAllLocales } from '~/api/other-locales';
23-
import type { LocaleOption } from '~/api/other-locales';
2422
import LocaleMenu from '~/modules/locale/components/LocaleMenu';
23+
import { useProject } from '~/modules/project';
24+
import { fetchAllLocales, LocaleOption } from '~/api/other-locales';
25+
2526
/**
2627
* Renders batch editor, used for performing mass actions on translations.
2728
*/
@@ -35,8 +36,27 @@ export function BatchActions(): React.ReactElement<'div'> {
3536
const replace = useRef<HTMLInputElement>(null);
3637

3738
const [otherLocale, setOtherLocale] = useState('');
39+
40+
const { slug, locales: projectLocales } = useProject();
3841
const [locales, setLocales] = useState<LocaleOption[]>([]);
3942

43+
useEffect(() => {
44+
let cancelled = false;
45+
46+
if (slug === 'all-projects') {
47+
fetchAllLocales().then((result) => {
48+
if (!cancelled) {
49+
setLocales(result);
50+
}
51+
});
52+
} else {
53+
setLocales(projectLocales);
54+
}
55+
return () => {
56+
cancelled = true;
57+
};
58+
}, [slug, projectLocales]);
59+
4060
const quitBatchActions = useCallback(() => dispatch(resetSelection()), []);
4161

4262
useEffect(() => {
@@ -50,12 +70,6 @@ export function BatchActions(): React.ReactElement<'div'> {
5070
return () => document.removeEventListener('keydown', handleShortcuts);
5171
}, []);
5272

53-
useEffect(() => {
54-
fetchAllLocales().then((all) => {
55-
setLocales(all);
56-
});
57-
}, []);
58-
5973
const selectAllEntities = useCallback(
6074
() => dispatch(selectAll(location)),
6175
[location],

‎translate/src/modules/project/actions.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LocaleOption } from '~/api/other-locales';
12
import { fetchProject, Tag } from '~/api/project';
23
import type { AppDispatch } from '~/store';
34

@@ -9,6 +10,7 @@ export type Action = ReceiveAction | RequestAction;
910
/** Notify that project data is being fetched. */
1011
type RequestAction = {
1112
readonly type: typeof REQUEST;
13+
readonly slug: string;
1214
};
1315

1416
/** Receive project data. */
@@ -18,22 +20,24 @@ type ReceiveAction = {
1820
readonly name: string;
1921
readonly info: string;
2022
readonly tags: Tag[];
23+
readonly locales: LocaleOption[];
2124
};
2225

2326
/**
2427
* Get data about the current project.
2528
*/
2629
export const getProject = (slug: string) => async (dispatch: AppDispatch) => {
2730
// When 'all-projects' are selected, we do not fetch data.
31+
dispatch({ type: REQUEST, slug });
2832
if (slug !== 'all-projects') {
29-
dispatch({ type: REQUEST });
30-
const { info, name, slug: slug_, tags } = await fetchProject(slug);
33+
const { info, name, slug: slug_, tags, locales } = await fetchProject(slug);
3134
dispatch({
3235
type: RECEIVE,
3336
slug: slug_,
3437
name: name,
3538
info: info,
3639
tags: tags.sort((a, b) => b.priority - a.priority),
40+
locales: locales,
3741
});
3842
}
3943
};

‎translate/src/modules/project/reducer.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Tag } from '~/api/project';
22

33
import { Action, RECEIVE, REQUEST } from './actions';
4+
import { LocaleOption } from '~/api/other-locales';
45

56
// Name of this module.
67
// Used as the key to store this module's reducer.
@@ -12,6 +13,7 @@ export type ProjectState = {
1213
readonly name: string;
1314
readonly info: string;
1415
readonly tags: Tag[];
16+
readonly locales: LocaleOption[];
1517
};
1618

1719
const initial: ProjectState = {
@@ -20,6 +22,7 @@ const initial: ProjectState = {
2022
name: '',
2123
info: '',
2224
tags: [],
25+
locales: [],
2326
};
2427

2528
export function reducer(
@@ -28,9 +31,18 @@ export function reducer(
2831
): ProjectState {
2932
switch (action.type) {
3033
case REQUEST:
34+
if (action.slug === 'all-projects') {
35+
return {
36+
...initial,
37+
slug: action.slug,
38+
};
39+
}
40+
3141
return {
3242
...state,
3343
fetching: true,
44+
slug: action.slug,
45+
locales: [],
3446
};
3547
case RECEIVE:
3648
return {
@@ -40,6 +52,7 @@ export function reducer(
4052
name: action.name,
4153
info: action.info,
4254
tags: action.tags,
55+
locales: action.locales,
4356
};
4457
default:
4558
return state;

0 commit comments

Comments
 (0)