-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
admin.ts
98 lines (79 loc) · 2.27 KB
/
admin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { get } from '../api/client';
import { ADMIN_API } from '../config/wagtailConfig';
export interface WagtailPageAPI {
id: number;
meta: {
status: {
status: string;
live: boolean;
/* eslint-disable-next-line camelcase */
has_unpublished_changes: boolean;
}
children: any;
parent: {
id: number;
} | null;
locale?: string;
translations?: any;
};
/* eslint-disable-next-line camelcase */
admin_display_title?: string;
}
interface WagtailPageListAPI {
meta: {
/* eslint-disable-next-line camelcase */
total_count: number;
};
items: WagtailPageAPI[];
}
export const getPage: (id: number) => Promise<WagtailPageAPI> = (id) => {
const url = `${ADMIN_API.PAGES}${id}/`;
return get(url);
};
interface GetPageChildrenOptions {
fields?: string[];
onlyWithChildren?: boolean;
offset?: number;
}
type GetPageChildren = (id: number | string, options: GetPageChildrenOptions) => Promise<WagtailPageListAPI>;
export const getPageChildren: GetPageChildren = (id, options = {}) => {
let realId = id;
let locale: string | null = null;
if (typeof id === 'string') {
[realId, locale] = id.split('-');
}
let url = `${ADMIN_API.PAGES}?child_of=${realId}&for_explorer=1`;
if (wagtailConfig.I18N_ENABLED && locale) {
url += `&locale=${locale}`;
}
if (options.fields) {
url += `&fields=parent,translations,${window.encodeURIComponent(options.fields.join(','))}`;
} else {
url += '&fields=parent,translations';
}
if (options.onlyWithChildren) {
url += '&has_children=1';
}
if (options.offset) {
url += `&offset=${options.offset}`;
}
url += ADMIN_API.EXTRA_CHILDREN_PARAMETERS;
return get(url);
};
interface GetPageTranslationsOptions {
fields?: string[];
onlyWithChildren?: boolean;
}
type GetPageTranslations = (id: number, options: GetPageTranslationsOptions) => Promise<WagtailPageListAPI>;
export const getPageTranslations: GetPageTranslations = (id, options = {}) => {
let url = `${ADMIN_API.PAGES}?translation_of=${id}&limit=20`;
if (options.fields) {
url += `&fields=parent,${global.encodeURIComponent(options.fields.join(','))}`;
} else {
url += '&fields=parent';
}
if (options.onlyWithChildren) {
url += '&has_children=1';
}
return get(url);
};