Skip to content

Commit e8e5c3d

Browse files
committed
WIP
1 parent 6723cc8 commit e8e5c3d

File tree

8 files changed

+119
-298
lines changed

8 files changed

+119
-298
lines changed

examples/angular/template-hierarchy-data-fetching/example-app/src/app/components/blog/blog.component.ts

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Component, OnInit, signal } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import {
4-
GraphQLStateService,
5-
gql,
4+
GraphQLStateService
65
} from '../../utils/graphql.service';
76
import { Post, PostsResponse } from '../../interfaces/post.interface';
7+
import { POSTS_QUERY } from '../../utils/postQuery';
88

99
@Component({
1010
selector: 'app-blog',
@@ -28,66 +28,10 @@ export class BlogComponent implements OnInit {
2828

2929
ngOnInit() {
3030
this.postsQuery = this.graphqlState.createQuery<PostsResponse>(
31-
gql`
32-
query GetPosts(
33-
$first: Int = 9
34-
$after: String
35-
$category: String
36-
$tag: String
37-
) {
38-
posts(
39-
first: $first
40-
after: $after
41-
where: { categoryName: $category, tag: $tag }
42-
) {
43-
pageInfo {
44-
hasNextPage
45-
endCursor
46-
}
47-
edges {
48-
cursor
49-
node {
50-
id
51-
title
52-
date
53-
excerpt
54-
uri
55-
slug
56-
featuredImage {
57-
node {
58-
sourceUrl
59-
altText
60-
}
61-
}
62-
categories {
63-
nodes {
64-
name
65-
slug
66-
}
67-
}
68-
tags {
69-
nodes {
70-
name
71-
slug
72-
}
73-
}
74-
author {
75-
node {
76-
name
77-
avatar {
78-
url
79-
}
80-
}
81-
}
82-
}
83-
}
84-
}
85-
}
86-
`,
31+
POSTS_QUERY,
8732
{ first: 10 },
8833
);
8934

90-
9135
this.postsData = this.postsQuery.data;
9236
this.loading = this.postsQuery.loading;
9337
this.error = this.postsQuery.error;
@@ -109,21 +53,17 @@ export class BlogComponent implements OnInit {
10953
}
11054
};
11155

112-
// Update posts when data changes
113-
// For now, we'll call this periodically or on data updates
11456
updatePosts();
11557
}
11658

11759
loadMorePosts() {
118-
// Example of refetching with new variables
11960
this.postsQuery.setVariables({ first: 20 }).subscribe({
12061
next: (result: any) => console.log('Loaded more posts:', result),
12162
error: (err: any) => console.error('Error loading more posts:', err),
12263
});
12364
}
12465

12566
refreshPosts() {
126-
// Example of refetching with same variables
12767
this.postsQuery.refetch().subscribe({
12868
next: (result: any) => console.log('Refreshed posts:', result),
12969
error: (err: any) => console.error('Error refreshing posts:', err),

examples/angular/template-hierarchy-data-fetching/example-app/src/app/components/header/header.component.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -138,35 +138,6 @@ export class HeaderComponent implements OnInit {
138138
},
139139
});
140140
}
141-
// Convert flat list of menu items to hierarchical structure
142-
143-
144-
// private flatListToHierarchical(items: MenuItem[]): MenuItem[] {
145-
// const map = new Map<string, MenuItem>();
146-
// const roots: MenuItem[] = [];
147-
148-
// // First pass: create map of all items
149-
// items.forEach((item) => {
150-
// map.set(item.id, { ...item, children: [] });
151-
// });
152-
153-
// // Second pass: build hierarchy
154-
// items.forEach((item) => {
155-
// const mappedItem = map.get(item.id)!;
156-
157-
// if (item.parentId && map.has(item.parentId)) {
158-
// const parent = map.get(item.parentId)!;
159-
// if (!parent.children) {
160-
// parent.children = [];
161-
// }
162-
// parent.children.push(mappedItem);
163-
// } else {
164-
// roots.push(mappedItem);
165-
// }
166-
// });
167-
168-
// return roots;
169-
// }
170141
private flatListToHierarchical(items: MenuItem[]): MenuItem[] {
171142
return flatListToHierarchical(items, {
172143
idKey: 'id',
@@ -192,11 +163,9 @@ export class HeaderComponent implements OnInit {
192163

193164
onMenuItemClick(item: MenuItem) {
194165
if (item.uri) {
195-
// Handle external links
196166
if (item.target === '_blank' || item.uri.startsWith('http')) {
197167
window.open(item.uri, item.target || '_self');
198168
} else {
199-
// Navigate internally
200169
this.router.navigate([item.uri]);
201170
}
202171
}

examples/angular/template-hierarchy-data-fetching/example-app/src/app/components/wp-templates/front-page/front-page.component.html

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
<app-loading text="Loading site..."></app-loading>
55
</ng-container>
66

7-
<!-- Error state for settings -->
8-
<div *ngIf="settingsError() && !settingsLoading()">
9-
<h1>My WordPress Site</h1>
10-
<p>Welcome to my site</p>
11-
<small>Error loading site data: {{ settingsError()?.message }}</small>
12-
</div>
13-
14-
<!-- Success state for settings -->
157
<div *ngIf="!settingsLoading() && !settingsError()">
168
<h1>{{ siteInfo().title }}</h1>
179
<p>{{ siteInfo().description }}</p>
@@ -22,7 +14,6 @@ <h1>{{ siteInfo().title }}</h1>
2214
<section id="recent-posts">
2315
<h2>Recent Posts</h2>
2416

25-
<!-- Error state for blog posts -->
2617
<div *ngIf="blogError() && !blogLoading()">
2718
<p>Failed to load posts</p>
2819
<p>{{ blogError()?.message }}</p>
@@ -31,23 +22,20 @@ <h2>Recent Posts</h2>
3122
</button>
3223
</div>
3324

34-
<!-- Loading state for blog posts -->
3525
<ng-container *ngIf="blogLoading() && !blogError()">
3626
<app-loading text="Loading recent posts..."></app-loading>
3727
</ng-container>
3828

39-
<!-- Empty state -->
4029
<ng-container
41-
*ngIf="!blogLoading() && !blogError() && posts().length === 0"
30+
*ngIf="!blogLoading() && !blogError() && blogPosts().length === 0"
4231
>
43-
<app-empty-state></app-empty-state>
32+
<app-empty-state text="No recent posts available"></app-empty-state>
4433
</ng-container>
4534

46-
<!-- Posts listing -->
4735
<ng-container
48-
*ngIf="!blogLoading() && !blogError() && posts().length > 0"
36+
*ngIf="!blogLoading() && !blogError() && blogPosts().length > 0"
4937
>
50-
<app-post-listing [posts]="posts()" [loading]="false" [cols]="4">
38+
<app-post-listing [posts]="blogPosts()" [loading]="false" [cols]="4">
5139
</app-post-listing>
5240
</ng-container>
5341
</section>

0 commit comments

Comments
 (0)