Skip to content

Commit 13812c2

Browse files
authored
Merge pull request #45 from devgateway/add-year-range-api
add year range api and improve consumers
2 parents f3559a1 + 85f46ab commit 13812c2

20 files changed

+804
-269
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"@devgateway/wp-react-lib": minor
3+
---
4+
5+
# Changelog
6+
7+
## Changes
8+
9+
- Update Media Consumer to include type definitions.
10+
- Update Page Consumer to include type definitions.
11+
- Update Post Consumer to include type definitions.
12+
- Update Search Consumer to include type definitions.
13+
- Update Settings Consumer to include type definitions.
14+
- Update Taxonomy Consumer to include type definitions.
15+
16+
## New Features
17+
18+
- Add Categories consumer.
19+
- Add date range API.
20+
- Add Categories provider.

wp-react-lib/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
"types": "./dist/types/api/index.d.ts",
7070
"import": "./dist/esm/api/index.js",
7171
"require": "./dist/cjs/api/index.js"
72+
},
73+
"./hooks": {
74+
"types": "./dist/types/hooks/index.d.ts",
75+
"import": "./dist/esm/hooks/index.js",
76+
"require": "./dist/cjs/hooks/index.js"
7277
}
7378
}
7479
}

wp-react-lib/src/api/index.ts

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ const URL_SEARCH = API_ROOT + (process.env.VITE_REACT_APP_WP_SEARCH_END_POINT ??
1212

1313
const URL_MEDIA = API_ROOT + '/wp/v2/media'
1414

15-
const URL_SETTINGS = API_ROOT + '/dg/v1/settings'
15+
const URL_SETTINGS = API_ROOT + '/dg/v1/settings';
16+
17+
const URL_CATEGORIES = API_ROOT + '/wp/v2/categories'
18+
19+
const URL_YEAR_RANGE = API_ROOT + '/util-api/v1/year-range'
1620

1721

1822
export const post = (url: string, params: Record<string, unknown>, isBlob?: boolean) => {
@@ -120,7 +124,8 @@ export const getPosts = ({
120124
locale,
121125
previewNonce,
122126
previewId,
123-
search
127+
search,
128+
after
124129
}: {
125130
slug?: string;
126131
type?: string;
@@ -134,31 +139,44 @@ export const getPosts = ({
134139
previewNonce?: string;
135140
previewId?: string;
136141
search?: string;
142+
after?: Date;
137143
}) => {
138-
//language , categories id, date before, record per page, number of page, fields to be included, post type
139-
//const {lang, slug, wType: type, taxonomy, categories, before, perPage, page, fields} = params
140144

141145
let url = URL_API_BASE + (type ?? 'posts')
146+
142147
if (previewId) {
143148
url += '/' + previewId + '/revisions'
144149
+ (previewNonce ? '?_wpnonce=' + previewNonce + '&' : '')
145150
} else {
146151
url += "?"
147152
}
148-
url += '_embed=true&lang=' + locale
153+
154+
url += 'lang=' + locale
149155
+ (slug ? '&slug=' + slug : '')
150156
if (!slug) {
151157
url += (categories ? (taxonomy ? '&' + taxonomy : '&categories')
152-
+ "=" + (categories ?? "") : '') //ids
153-
+ (before ? "&before=" + before.toISOString() : "")
158+
+ "=" + (categories ? categories : "") : '') //ids
154159
+ (perPage ? '&per_page=' + perPage : '')
155160
+ (page ? '&page=' + page : '')
156161
+ (fields ? '&_fields=' + fields : '')
157162
+ (search ? '&search=' + search : '')
158-
}
159163

160-
//url += "&lang=" + locale
161-
return get(url);
164+
if (before !== null && before !== undefined) {
165+
if (before instanceof Date) {
166+
url += "&before=" + before.toISOString();
167+
} else {
168+
url += "&before=" + before;
169+
}
170+
}
171+
if (after !== null && after !== undefined) {
172+
if (after instanceof Date) {
173+
url += "&after=" + after.toISOString();
174+
} else {
175+
url += "&after=" + after;
176+
}
177+
}
178+
}
179+
return get(url)
162180
}
163181

164182
export const getPages = ({
@@ -232,10 +250,51 @@ export const getMedia = (slug: string, locale: string) : Promise<Media> => {
232250
return get(URL_MEDIA + '/' + slug + '?lang=' + locale) as Promise<Media>;
233251
}
234252

253+
export const getCategories = ({
254+
context = 'view',
255+
page = 1,
256+
perPage = 10,
257+
search,
258+
exclude,
259+
include,
260+
order = 'asc',
261+
orderby = 'name',
262+
hideEmpty,
263+
parent,
264+
post,
265+
slug,
266+
locale
267+
}: {
268+
context?: string;
269+
page?: number;
270+
perPage?: number;
271+
search?: string;
272+
exclude?: string;
273+
include?: string;
274+
order?: string;
275+
orderby?: string;
276+
hideEmpty?: boolean;
277+
parent?: string;
278+
post?: string;
279+
slug?: string;
280+
locale?: string;
281+
})=> {
282+
let url = URL_CATEGORIES + '?lang=' + locale
283+
+ (context ? '&context=' + context : '')
284+
+ (page ? '&page=' + page : '')
285+
+ (perPage ? '&per_page=' + perPage : '')
286+
+ (search ? '&search=' + search : '')
287+
+ (exclude ? '&exclude=' + exclude : '')
288+
+ (include ? '&include=' + include : '')
289+
+ (order ? '&order=' + order : '')
290+
+ (orderby ? '&orderby=' + orderby : '')
291+
+ (hideEmpty ? '&hide_empty=' + hideEmpty : '')
292+
+ (parent ? '&parent=' + parent : '')
293+
+ (post ? '&post=' + post : '')
294+
+ (slug ? '&slug=' + slug : '')
295+
return get(url)
296+
}
235297

236-
237-
238-
/*
239-
export const getSettings = (slug, locale) => {
240-
return get(URL_SETTINGS)
241-
}*/
298+
export const getYearRange = () => {
299+
return get(URL_YEAR_RANGE)
300+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import { CategoriesContext } from '../providers/Context';
3+
4+
interface CategoriesConsumerProps {
5+
children: React.ReactNode | React.ReactNode[] | React.ReactElement | React.ReactElement[];
6+
}
7+
8+
/**
9+
* @deprecated Use the `CategoriesContext.Consumer` instead.
10+
* CategoriesConsumer is a component that provides the categories, loading, error, and meta to its children.
11+
* @param props - The props for the CategoriesConsumer component.
12+
* @returns The CategoriesConsumer component.
13+
*/
14+
export const CategoriesConsumer = ({ children }: CategoriesConsumerProps) => {
15+
return (
16+
<React.Fragment>
17+
<CategoriesContext.Consumer>
18+
{({ categories, loading, error, meta }) => {
19+
if (loading) {
20+
return (
21+
<React.Fragment>
22+
{React.Children.map(children, (child => React.cloneElement(child as React.ReactElement<any>, { categories: [], loading, error: null, meta })))}
23+
</React.Fragment>
24+
);
25+
}
26+
27+
if (error) {
28+
return (
29+
<React.Fragment>
30+
{React.Children.map(children, (child => React.cloneElement(child as React.ReactElement<any>, { categories: [], loading: false, error, meta })))}
31+
</React.Fragment>
32+
);
33+
}
34+
35+
return (
36+
<React.Fragment>
37+
{React.Children.map(children, (child => React.cloneElement(child as React.ReactElement<any>, { categories: categories || [], loading: false, error: null, meta })))}
38+
</React.Fragment>
39+
);
40+
}}
41+
</CategoriesContext.Consumer>
42+
</React.Fragment>
43+
44+
);
45+
};
46+
47+
export default CategoriesConsumer;

wp-react-lib/src/consumers/MediaConsumer.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
2-
import {MediaContext} from '../providers/MediaProvider'
2+
import { MediaContext } from '../providers/MediaProvider'
3+
import type { Media } from '../types';
34

45
interface MediaConsumerProps {
56
children: React.ReactNode | React.ReactNode[] | React.ReactElement | React.ReactElement[];
@@ -9,10 +10,15 @@ const MediaConsumer = (props: MediaConsumerProps) => {
910
return (
1011
<MediaContext.Consumer>
1112
{
12-
({media, locale}) => {
13-
return media && <React.Fragment>
14-
{React.Children.map(props.children, (child => React.cloneElement(child as React.ReactElement, {media, locale})))}
13+
({ media, locale }) => {
14+
return media ? <React.Fragment>
15+
{React.Children.map(props.children, (child) =>
16+
React.isValidElement(child) && 'props' in child && child.props && typeof child.type !== 'string'
17+
? React.cloneElement(child as React.ReactElement<any>, { media, locale })
18+
: child
19+
)}
1520
</React.Fragment>
21+
: null;
1622
}
1723
}
1824
</MediaContext.Consumer>

wp-react-lib/src/consumers/MenuConsumer.jsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@ import React from 'react'
22

33
import {MenuContext} from '../providers/Context'
44

5-
const PageConsumer = (props) => {
5+
/**
6+
* @deprecated Use the `MenuContext.Consumer` instead.
7+
* MenuConsumer is a component that provides the menu and locale to its children.
8+
* @param props - The props for the MenuConsumer component.
9+
* @returns The MenuConsumer component.
10+
*/
11+
const MenuConsumer = (props) => {
612
return (
713
<React.Fragment>
814
<MenuContext.Consumer>
915
{
1016
({menu, locale}) => {
11-
return menu && <React.Fragment>
12-
{React.Children.map(props.children, (child => React.cloneElement(child, {...props, menu, locale})))}
13-
</React.Fragment>
17+
return menu ? <React.Fragment>
18+
{React.Children.map(props.children, (child) =>
19+
React.isValidElement(child) && 'props' in child && child.props && typeof child.type !== 'string'
20+
? React.cloneElement(child, { menu, locale })
21+
: child
22+
)}
23+
</React.Fragment> : null;
1424
}
1525
}
1626
</MenuContext.Consumer>
1727
</React.Fragment>)
1828
}
1929

20-
21-
export default PageConsumer
30+
export default MenuConsumer;
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
import React from 'react'
2-
3-
import { PageContext } from '../providers/Context'
1+
import React from 'react';
2+
import { PageContext } from '../providers/Context';
43

54
interface PageConsumerProps {
65
children: React.ReactNode | React.ReactNode[] | React.ReactElement | React.ReactElement[];
76
}
87

8+
/**
9+
* @deprecated Use the `PageContext.Consumer` instead.
10+
* PageConsumer is a component that provides the pages, meta, and locale to its children.
11+
* @param props - The props for the PageConsumer component.
12+
* @returns The PageConsumer component.
13+
*/
914
const PageConsumer = (props: PageConsumerProps) => {
1015

1116
return (
1217
<PageContext.Consumer>
1318
{
1419

1520
({ pages, meta, locale }) => {
16-
return pages && <React.Fragment>
17-
{React.Children.map(props.children, (child => React.cloneElement(child as React.ReactElement, {
18-
pages,
19-
meta,
20-
locale
21-
})))}
21+
return pages ? <React.Fragment>
22+
{React.Children.map(props.children, (child) =>
23+
React.isValidElement(child) && 'props' in child && child.props && typeof child.type !== 'string'
24+
? React.cloneElement(child as React.ReactElement<any>, { pages, meta, locale })
25+
: child
26+
)}
2227
</React.Fragment>
28+
: null;
2329
}
2430
}
2531
</PageContext.Consumer>
2632
)
2733
}
2834

29-
3035
export default PageConsumer

wp-react-lib/src/consumers/PostConsumer.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ interface PostConsumerProps {
55
children: React.ReactNode | React.ReactNode[] | React.ReactElement | React.ReactElement[];
66
}
77

8+
/**
9+
* @deprecated Use the `PostContext.Consumer` instead.
10+
* PostConsumer is a component that provides the posts, meta, and locale to its children.
11+
* @param props - The props for the PostConsumer component.
12+
* @returns The PostConsumer component.
13+
*/
814
const PostConsumer = (props: PostConsumerProps) => {
915
return (
1016
<PostContext.Consumer>
1117
{({ posts, meta, locale }) => {
12-
if (!posts) return null;
13-
return (
14-
<React.Fragment>
15-
{React.Children.map(props.children, (child => React.cloneElement(child as React.ReactElement, {posts, meta, locale})))}
16-
</React.Fragment>
17-
);
18+
return posts ? <React.Fragment>
19+
{React.Children.map(props.children, (child) =>
20+
React.isValidElement(child) && 'props' in child && child.props && typeof child.type !== 'string'
21+
? React.cloneElement(child as React.ReactElement<any>, { posts, meta, locale })
22+
: child
23+
)}
24+
</React.Fragment> : null;
1825
}}
1926
</PostContext.Consumer>
2027
)
2128
}
2229

23-
24-
export default PostConsumer
30+
export default PostConsumer;

wp-react-lib/src/consumers/SearchConsumer.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@ interface SearchConsumerProps {
55
children: React.ReactNode | React.ReactNode[] | React.ReactElement | React.ReactElement[];
66
}
77

8+
/**
9+
* @deprecated Use the `SearchContext.Consumer` instead.
10+
* SearchConsumer is a component that provides the search results, meta, and locale to its children.
11+
* @param props - The props for the SearchConsumer component.
12+
* @returns The SearchConsumer component.
13+
*/
814
const SearchConsumer = (props: SearchConsumerProps) => {
915
return (
1016
<SearchContext.Consumer>
1117
{({results, meta, locale}) => {
12-
return results && <React.Fragment>
13-
{React.Children.map(props.children, (child => React.cloneElement(child as React.ReactElement, {results, meta, locale})))}
14-
</React.Fragment>
18+
return results
19+
? React.Children.map(props.children, (child) =>
20+
React.isValidElement(child)
21+
? React.cloneElement(child as React.ReactElement<any>, { results, meta, locale })
22+
: child
23+
)
24+
: null;
1525
}}
1626
</SearchContext.Consumer>
1727
)
1828
}
1929

20-
2130
export default SearchConsumer

0 commit comments

Comments
 (0)