-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(sdk): Update examples to handle Content API (#28811)
## Proposed changes - Update `footer` component on Angular and NextJS Library to handle calls to content API and render content ## Screenshots ### NextJS <img width="1494" alt="Screenshot 2024-06-10 at 2 33 17 PM" src="https://github.com/dotCMS/core/assets/63567962/dccd92ed-0572-451c-b7a7-ae75502bcc48"> ### Angular <img width="1489" alt="Screenshot 2024-06-10 at 4 54 22 PM" src="https://github.com/dotCMS/core/assets/63567962/0ff2c82d-486b-45e0-bd4c-27f91f1c0632">
- Loading branch information
Showing
16 changed files
with
405 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
examples/angular/src/app/components/blogs/blogs.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Component, OnInit, inject, signal } from '@angular/core'; | ||
import { DOTCMS_CLIENT_TOKEN } from '../../client-token/dotcms-client'; | ||
import { GenericContentlet } from '../../utils'; | ||
import { ContentletsComponent } from '../contentlets/contentlets.component'; | ||
import { Contentlet } from '@dotcms/client/src/lib/client/content/shared/types'; | ||
|
||
@Component({ | ||
selector: 'app-blogs', | ||
standalone: true, | ||
imports: [ContentletsComponent], | ||
template: ` <div class="flex flex-col"> | ||
<h2 class="text-2xl font-bold mb-7 text-black">Latest Blog Posts</h2> | ||
@if(!!blogs().length) { <app-contentlets [contentlets]="blogs()" /> } | ||
</div>`, | ||
}) | ||
export class BlogsComponent implements OnInit { | ||
private readonly client = inject(DOTCMS_CLIENT_TOKEN); | ||
|
||
readonly blogs = signal<Contentlet<GenericContentlet>[]>([]); | ||
|
||
ngOnInit(): void { | ||
this.client.content | ||
.getCollection<GenericContentlet>('Blog') | ||
.limit(3) | ||
.sortBy([ | ||
{ | ||
field: 'modDate', | ||
order: 'desc', | ||
}, | ||
]) | ||
.then((response) => { | ||
this.blogs.set(response.contentlets); | ||
}) | ||
.catch((error) => { | ||
console.error('Error fetching Blogs', error); | ||
}); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
examples/angular/src/app/components/contentlets/contentlets.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { Contentlet } from '@dotcms/client/src/lib/client/content/shared/types'; | ||
import { GenericContentlet } from '../../utils'; | ||
import { environment } from '../../../environments/environment'; | ||
import { NgOptimizedImage } from '@angular/common'; | ||
|
||
@Component({ | ||
selector: 'app-contentlets', | ||
standalone: true, | ||
imports: [NgOptimizedImage], | ||
template: `<ul class="flex flex-col gap-7"> | ||
@for(contentlet of contentlets; track contentlet.identifier) { | ||
<li class="flex gap-7 min-h-16"> | ||
<a class="min-w-32 relative" [href]="contentlet.urlMap ?? contentlet.url"> | ||
<img | ||
[ngSrc]="getImageUrl(contentlet)" | ||
[fill]="true" | ||
[alt]="contentlet.urlTitle ?? contentlet.title" | ||
class="object-cover" | ||
/> | ||
</a> | ||
<div class="flex flex-col gap-1"> | ||
<a | ||
class="text-sm text-zinc-900 font-bold" | ||
[href]="contentlet.urlMap ?? contentlet.url" | ||
> | ||
{{ contentlet.title }} | ||
</a> | ||
<time class="text-zinc-600"> | ||
{{ getDateString(contentlet.modDate) }} | ||
</time> | ||
</div> | ||
</li> | ||
} | ||
</ul> `, | ||
}) | ||
export class ContentletsComponent { | ||
@Input() contentlets: Contentlet<GenericContentlet>[] = []; | ||
|
||
dateFormatOptions: Intl.DateTimeFormatOptions = { | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
}; | ||
|
||
getDateString(date: string): string { | ||
return new Date(date).toLocaleDateString('en-US', this.dateFormatOptions); | ||
} | ||
|
||
getImageUrl(contentlet: Contentlet<GenericContentlet>): string { | ||
const host = environment.dotcmsUrl; | ||
return `${host}${contentlet.image}?language_id=${ | ||
contentlet.languageId || 1 | ||
}`; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
examples/angular/src/app/components/destinations/destinations.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Component, OnInit, inject, signal } from '@angular/core'; | ||
import { DOTCMS_CLIENT_TOKEN } from '../../client-token/dotcms-client'; | ||
import { GenericContentlet } from '../../utils'; | ||
import { ContentletsComponent } from '../contentlets/contentlets.component'; | ||
import { Contentlet } from '@dotcms/client/src/lib/client/content/shared/types'; | ||
|
||
@Component({ | ||
selector: 'app-destinations', | ||
standalone: true, | ||
imports: [ContentletsComponent], | ||
template: ` <div class="flex flex-col"> | ||
<h2 class="text-2xl font-bold mb-7 text-black">Popular Destinations</h2> | ||
@if(!!destinations().length) { | ||
<app-contentlets [contentlets]="destinations()" /> } | ||
</div>`, | ||
}) | ||
export class DestinationsComponent implements OnInit { | ||
private readonly client = inject(DOTCMS_CLIENT_TOKEN); | ||
|
||
readonly destinations = signal<Contentlet<GenericContentlet>[]>([]); | ||
|
||
ngOnInit() { | ||
this.client.content | ||
.getCollection<GenericContentlet>('Destination') | ||
.limit(3) | ||
.sortBy([ | ||
{ | ||
field: 'modDate', | ||
order: 'desc', | ||
}, | ||
]) | ||
.then((response) => { | ||
this.destinations.set(response.contentlets); | ||
}) | ||
.catch((error) => { | ||
console.error('Error fetching Destinations', error); | ||
}); | ||
} | ||
} |
41 changes: 36 additions & 5 deletions
41
examples/angular/src/app/pages/components/footer/footer.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,43 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { environment } from '../../../../environments/environment'; | ||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
|
||
import { BlogsComponent } from '../../../components/blogs/blogs.component'; | ||
import { DestinationsComponent } from '../../../components/destinations/destinations.component'; | ||
import { NgOptimizedImage } from '@angular/common'; | ||
|
||
@Component({ | ||
selector: 'app-footer', | ||
standalone: true, | ||
imports: [], | ||
template: `<footer class="p-4 text-white bg-red-400">Footer</footer>`, | ||
imports: [BlogsComponent, DestinationsComponent, NgOptimizedImage], | ||
template: `<footer class="p-4 text-white bg-red-100 py-24"> | ||
<div | ||
class="grid md:grid-cols-3 sm:grid-cols-1 md:grid-rows-1 sm:grid-rows-3 gap-7 mx-24" | ||
> | ||
<div class="flex gap-7 flex-col"> | ||
<h2 class="text-2xl font-bold text-black">About us</h2> | ||
<p class="text-sm text-zinc-800"> | ||
We are TravelLux, a community of dedicated travel experts, | ||
journalists, and bloggers. Our aim is to offer you the best insight on | ||
where to go for your travel as well as to give you amazing | ||
opportunities with free benefits and bonuses for registered clients. | ||
</p> | ||
<img | ||
[ngSrc]=" | ||
environment.dotcmsUrl + | ||
'/contentAsset/image/82da90eb-044d-44cc-a71b-86f79820b61b/fileAsset' | ||
" | ||
height="53" | ||
width="221" | ||
alt="TravelLux logo" | ||
/> | ||
</div> | ||
<app-blogs /> | ||
<app-destinations /> | ||
</div> | ||
</footer>`, | ||
styleUrl: './footer.component.css', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class FooterComponent { } | ||
export class FooterComponent { | ||
protected readonly environment = environment; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { dotcmsClient } from "@dotcms/client"; | ||
|
||
// Client for content fetching | ||
export const client = dotcmsClient.init({ | ||
dotcmsUrl: process.env.NEXT_PUBLIC_DOTCMS_HOST, | ||
authToken: "NO_TOKEN", | ||
siteId: "59bb8831-6706-4589-9ca0-ff74016e02b2", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
examples/nextjs/src/components/layout/footer/components/aboutUs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from "react"; | ||
import Image from "next/image"; | ||
|
||
function AboutUs() { | ||
return ( | ||
<div className="flex gap-7 flex-col"> | ||
<h2 className="text-2xl font-bold text-black">About us</h2> | ||
<p className="text-sm text-zinc-800"> | ||
We are TravelLux, a community of dedicated travel experts, | ||
journalists, and bloggers. Our aim is to offer you the best | ||
insight on where to go for your travel as well as to give you | ||
amazing opportunities with free benefits and bonuses for | ||
registered clients. | ||
</p> | ||
<Image | ||
src={`${process.env.NEXT_PUBLIC_DOTCMS_HOST}/contentAsset/image/82da90eb-044d-44cc-a71b-86f79820b61b/fileAsset`} | ||
height={53} | ||
width={221} | ||
alt="TravelLux logo" | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default AboutUs; |
Oops, something went wrong.