@elementor/editor-site-navigation 0.19.11 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/index.js +128 -50
  3. package/dist/index.mjs +124 -44
  4. package/package.json +5 -5
  5. package/src/api/post.ts +32 -4
  6. package/src/api/settings.ts +5 -9
  7. package/src/api/user.ts +20 -0
  8. package/src/components/panel/actions-menu/actions/__tests__/delete.test.tsx +8 -0
  9. package/src/components/panel/actions-menu/actions/__tests__/set-home.test.tsx +52 -0
  10. package/src/components/panel/actions-menu/actions/__tests__/view.test.tsx +4 -0
  11. package/src/components/panel/actions-menu/actions/delete.tsx +4 -1
  12. package/src/components/panel/actions-menu/actions/duplicate.tsx +5 -1
  13. package/src/components/panel/actions-menu/actions/rename.tsx +1 -0
  14. package/src/components/panel/actions-menu/actions/set-home.tsx +9 -1
  15. package/src/components/panel/add-new-button.tsx +3 -0
  16. package/src/components/panel/posts-list/__tests__/post-list-item.test.tsx +20 -0
  17. package/src/components/panel/posts-list/__tests__/posts-collapsible-list.test.tsx +56 -8
  18. package/src/components/panel/posts-list/collapsible-list.tsx +1 -1
  19. package/src/components/panel/posts-list/list-items/list-item-view.tsx +50 -29
  20. package/src/components/panel/posts-list/posts-collapsible-list.tsx +14 -7
  21. package/src/components/top-bar/__tests__/add-new-page.test.tsx +36 -0
  22. package/src/components/top-bar/__tests__/recently-edited.test.tsx +68 -0
  23. package/src/components/top-bar/create-post-list-item.tsx +3 -1
  24. package/src/components/top-bar/post-list-item.tsx +1 -0
  25. package/src/hooks/__tests__/use-homepage.test.ts +1 -1
  26. package/src/hooks/__tests__/use-posts.test.ts +44 -10
  27. package/src/hooks/use-posts.ts +25 -4
  28. package/src/hooks/use-user.ts +11 -0
  29. package/src/types.ts +8 -1
@@ -2,42 +2,76 @@ import { waitFor } from '@testing-library/react';
2
2
  import apiFetch from '@wordpress/api-fetch';
3
3
  import { renderHookWithQuery } from 'test-utils';
4
4
  import { usePosts } from '../use-posts';
5
+ import { POST_PER_PAGE } from '../../api/post';
5
6
 
6
- jest.mock( '@wordpress/api-fetch' );
7
+ const MOCK_POST_PER_PAGE = 2;
7
8
 
8
- describe( '@elementor/site-settings/use-posts', () => {
9
- beforeEach( () => {
10
- jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( [] ) );
11
- } );
9
+ jest.mock( '@wordpress/api-fetch', () => ( {
10
+ default: jest.fn( ( param ) => {
11
+ const pages = [
12
+ { id: 1, type: 'page', title: { rendered: 'Home' }, status: 'draft', link: 'www.test.demo' },
13
+ { id: 2, type: 'page', title: { rendered: 'About' }, status: 'publish', link: 'www.test.demo' },
14
+ { id: 3, type: 'page', title: { rendered: 'Services' }, status: 'publish', link: 'www.test.demo', isHome: true },
15
+ ];
16
+
17
+ const url = new URL( 'http:/example.com' + param.path );
18
+
19
+ const page = Number( url.searchParams.get( 'page' ) );
12
20
 
21
+ const startIndex = ( page - 1 ) * MOCK_POST_PER_PAGE;
22
+ const endIndex = startIndex + MOCK_POST_PER_PAGE;
23
+ const paginatedPages = pages.slice( startIndex, endIndex );
24
+
25
+ return {
26
+ json: () => Promise.resolve( paginatedPages ),
27
+ headers: {
28
+ get: ( header: string ) => {
29
+ switch ( header ) {
30
+ case 'x-wp-totalpages':
31
+ return Math.ceil( pages.length / MOCK_POST_PER_PAGE );
32
+ case 'x-wp-total':
33
+ return pages.length;
34
+ default:
35
+ return null;
36
+ }
37
+ },
38
+ },
39
+ };
40
+ } ),
41
+ __esModule: true,
42
+ } ) );
43
+
44
+ describe( '@elementor/site-settings/use-posts', () => {
13
45
  afterEach( () => {
14
46
  jest.clearAllMocks();
15
47
  } );
16
48
 
17
49
  it( 'usePosts hook should return posts list by type', async () => {
18
- // Arrange.
50
+ //Arrange
19
51
  const pages = [
20
52
  { id: 1, type: 'page', title: { rendered: 'Home' }, status: 'draft', link: 'www.test.demo' },
21
53
  { id: 2, type: 'page', title: { rendered: 'About' }, status: 'publish', link: 'www.test.demo' },
22
54
  { id: 3, type: 'page', title: { rendered: 'Services' }, status: 'publish', link: 'www.test.demo', isHome: true },
23
55
  ];
24
- jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( pages ) );
25
56
 
26
57
  // Act.
27
58
  const { component } = renderHookWithQuery( () => usePosts( 'page' ) );
28
59
 
29
60
  // Assert.
30
- const expectedPath = `/wp/v2/pages?status=any&per_page=-1&order=asc&_fields=${ encodeURIComponent( 'id,type,title,link,status' ) }`;
61
+ const expectedPath = `/wp/v2/pages?status=any&order=asc&page=1&per_page=${ POST_PER_PAGE }&_fields=${ encodeURIComponent( 'id,type,title,link,status,user_can' ) }`;
31
62
 
32
63
  expect( apiFetch ).toHaveBeenCalledWith( {
64
+ parse: false,
33
65
  path: expectedPath,
34
66
  } );
35
67
  expect( apiFetch ).toHaveBeenCalledTimes( 1 );
36
68
 
37
69
  await waitFor( () => {
38
- return component.result.current.isSuccess;
70
+ expect( component.result.current.isLoading ).toBeFalsy();
39
71
  } );
40
72
 
41
- expect( component.result.current.data ).toBe( pages );
73
+ expect( component.result.current.data.posts ).toContainEqual( pages[ 0 ] );
74
+ expect( component.result.current.data.posts ).toContainEqual( pages[ 1 ] );
75
+ expect( component.result.current.data.posts ).not.toContainEqual( pages[ 2 ] );
42
76
  } );
43
77
  } );
@@ -1,11 +1,32 @@
1
- import { useQuery } from '@elementor/query';
2
- import { getRequest, Slug } from '../api/post';
1
+ import { useInfiniteQuery } from '@elementor/query';
2
+ import { getRequest, PostsResponse, Slug } from '../api/post';
3
+ import { Post } from '../types';
3
4
 
4
5
  export const postsQueryKey = ( postTypeSlug: string ) => [ 'site-navigation', 'posts', postTypeSlug ];
5
6
 
7
+ const flattenData = ( data?: {pages: PostsResponse[]} ) => {
8
+ if ( ! data ) {
9
+ return data;
10
+ }
11
+
12
+ const flattened: Post[] = [];
13
+
14
+ data.pages.forEach( ( page ) => {
15
+ flattened.push( ...page.data );
16
+ } );
17
+
18
+ return flattened;
19
+ };
20
+
6
21
  export function usePosts( postTypeSlug: Slug ) {
7
- return useQuery( {
22
+ const query = useInfiniteQuery( {
8
23
  queryKey: postsQueryKey( postTypeSlug ),
9
- queryFn: () => getRequest( postTypeSlug ),
24
+ queryFn: ( { pageParam = 1 } ) => getRequest( postTypeSlug, pageParam ),
25
+ initialPageParam: 1,
26
+ getNextPageParam: ( lastPage ) => {
27
+ return lastPage.currentPage < lastPage.totalPages ? lastPage.currentPage + 1 : undefined;
28
+ },
10
29
  } );
30
+
31
+ return { ...query, data: { posts: flattenData( query.data ), total: query.data?.pages[ 0 ]?.totalPosts ?? 0 } };
11
32
  }
@@ -0,0 +1,11 @@
1
+ import { useQuery } from '@elementor/query';
2
+ import { getUser } from '../api/user';
3
+
4
+ export const userQueryKey = () => [ 'site-navigation', 'user' ];
5
+
6
+ export default function useUser( ) {
7
+ return useQuery( {
8
+ queryKey: userQueryKey(),
9
+ queryFn: () => getUser(),
10
+ } );
11
+ }
package/src/types.ts CHANGED
@@ -6,7 +6,11 @@ export type Post = {
6
6
  type: string;
7
7
  title: {
8
8
  rendered: string;
9
- }
9
+ };
10
+ user_can: {
11
+ edit: boolean;
12
+ delete: boolean;
13
+ };
10
14
  };
11
15
 
12
16
  export type RecentPost = {
@@ -19,4 +23,7 @@ export type RecentPost = {
19
23
  label: string,
20
24
  },
21
25
  date_modified: number,
26
+ user_can: {
27
+ edit: boolean,
28
+ },
22
29
  }