Fetch all pages of translations

pull/6565/head
Karl Hobley 2020-11-13 14:21:30 +00:00 zatwierdzone przez Matt Westcott
rodzic 41b0abc28c
commit 62ccde9c09
2 zmienionych plików z 28 dodań i 3 usunięć

Wyświetl plik

@ -68,6 +68,7 @@ export const getPageChildren: GetPageChildren = (id, options = {}) => {
interface GetPageTranslationsOptions {
fields?: string[];
onlyWithChildren?: boolean;
offset?: number;
}
type GetPageTranslations = (id: number, options: GetPageTranslationsOptions) => Promise<WagtailPageListAPI>;
export const getPageTranslations: GetPageTranslations = (id, options = {}) => {
@ -83,5 +84,29 @@ export const getPageTranslations: GetPageTranslations = (id, options = {}) => {
url += '&has_children=1';
}
if (options.offset) {
url += `&offset=${options.offset}`;
}
return get(url);
};
interface GetAllPageTranslationsOptions {
fields?: string[];
onlyWithChildren?: boolean;
}
export const getAllPageTranslations = async (id: number, options: GetAllPageTranslationsOptions) => {
const items: WagtailPageAPI[] = [];
let iterLimit = 100;
for (;;) {
const page = await getPageTranslations(id, { offset: items.length, ...options });
page.items.forEach(item => items.push(item));
if (items.length >= page.meta.total_count || iterLimit-- <= 0) {
return items;
}
}
};

Wyświetl plik

@ -54,7 +54,7 @@ function getChildren(id: number, offset = 0): ThunkActionType {
}
const getTranslationsStart = createAction('GET_TRANSLATIONS_START', id => ({ id }));
const getTranslationsSuccess = createAction('GET_TRANSLATIONS_SUCCESS', (id, items, meta) => ({ id, items, meta }));
const getTranslationsSuccess = createAction('GET_TRANSLATIONS_SUCCESS', (id, items) => ({ id, items }));
const getTranslationsFailure = createAction('GET_TRANSLATIONS_FAILURE', (id, error) => ({ id, error }));
/**
@ -64,8 +64,8 @@ function getTranslations(id) {
return (dispatch) => {
dispatch(getTranslationsStart(id));
return admin.getPageTranslations(id, { onlyWithChildren: true }).then(({ items, meta }) => {
dispatch(getTranslationsSuccess(id, items, meta));
return admin.getAllPageTranslations(id, { onlyWithChildren: true }).then(items => {
dispatch(getTranslationsSuccess(id, items));
}, (error) => {
dispatch(getTranslationsFailure(id, error));
});