Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 668e6ee

Browse files
feat: infinite gifs scroll
1 parent c29ef46 commit 668e6ee

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

studio/src/app/modals/editor/app-gif/app-gif.tsx

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export class AppGif {
2929
@State()
3030
private searchTerm: string;
3131

32+
@State()
33+
disableInfiniteScroll = false;
34+
35+
private paginationNext: string | number = 0;
36+
3237
constructor() {
3338
this.gifService = GifService.getInstance();
3439
}
@@ -87,28 +92,67 @@ export class AppGif {
8792
return;
8893
}
8994

90-
const gifs: TenorGif[] = await this.gifService.getGifs(this.searchTerm);
95+
const tenorResponse: TenorSearchResponse = await this.gifService.getGifs(this.searchTerm, this.paginationNext);
96+
97+
if (!tenorResponse) {
98+
this.emptyGifs();
99+
100+
resolve();
101+
return;
102+
}
103+
104+
this.paginationNext = tenorResponse.next;
105+
106+
const gifs: TenorGif[] = tenorResponse.results;
91107

92108
if (!gifs || gifs.length <= 0) {
93-
this.gifsOdd = [];
94-
this.gifsEven = [];
109+
this.emptyGifs();
95110

96111
resolve();
97112
return;
98113
}
99114

100-
this.gifsOdd = gifs.filter((_a, i) => i % 2);
101-
this.gifsEven = gifs.filter((_a, i) => !(i % 2));
115+
if (!this.gifsOdd) {
116+
this.gifsOdd = [];
117+
}
118+
119+
if (!this.gifsEven) {
120+
this.gifsEven = [];
121+
}
122+
123+
this.gifsOdd = [...this.gifsOdd, ...gifs.filter((_a, i) => i % 2)];
124+
this.gifsEven = [...this.gifsEven, ...gifs.filter((_a, i) => !(i % 2))];
102125

103126
resolve();
104127
});
105128
}
106129

130+
private searchNext(e: CustomEvent<void>): Promise<void> {
131+
return new Promise<void>(async (resolve) => {
132+
await this.search();
133+
134+
(e.target as HTMLIonInfiniteScrollElement).complete();
135+
136+
resolve();
137+
});
138+
}
139+
140+
private emptyGifs() {
141+
this.gifsOdd = [];
142+
this.gifsEven = [];
143+
144+
this.disableInfiniteScroll = true;
145+
}
146+
107147
private clear(): Promise<void> {
108148
return new Promise<void>((resolve) => {
109149
this.gifsOdd = null;
110150
this.gifsEven = null;
111151

152+
this.disableInfiniteScroll = false
153+
154+
this.paginationNext = 0;
155+
112156
resolve();
113157
});
114158
}
@@ -140,6 +184,13 @@ export class AppGif {
140184
{this.renderGifs(this.gifsEven)}
141185
</div>
142186
</div>
187+
188+
<ion-infinite-scroll threshold="100px" disabled={this.disableInfiniteScroll} onIonInfinite={(e: CustomEvent<void>) => this.searchNext(e)}>
189+
<ion-infinite-scroll-content
190+
loadingSpinner="bubbles"
191+
loadingText="Loading more data...">
192+
</ion-infinite-scroll-content>
193+
</ion-infinite-scroll>
143194
</ion-content>,
144195
<ion-footer>
145196
<ion-toolbar>

studio/src/app/services/gif/gif.service.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,27 @@ export class GifService {
4949
});
5050
}
5151

52-
getGifs(searchTerm: string): Promise<TenorGif[]> {
53-
return new Promise<TenorGif[]>(async (resolve) => {
52+
getGifs(searchTerm: string, next: string | number): Promise<TenorSearchResponse> {
53+
return new Promise<TenorSearchResponse>(async (resolve) => {
5454
const config: EnvironmentTenorConfig = EnvironmentConfigService.getInstance().get('tenor');
5555

5656
const anonymousId: string = await this.getAnonymousId();
5757

5858
const searchUrl = config.url + 'search?tag=' + searchTerm + '&key=' +
59-
config.key + '&ar_range=wide&limit=' + 8 + '&anon_id=' + anonymousId + '&media_filter=minimal';
59+
config.key + '&ar_range=wide&limit=' + 8 + '&anon_id=' + anonymousId + '&media_filter=minimal&pos=' + next;
6060

6161
try {
6262
const rawResponse: Response = await fetch(searchUrl);
6363

64-
const response: TenorTrendingResponse = JSON.parse(await rawResponse.text());
64+
const response: TenorSearchResponse = JSON.parse(await rawResponse.text());
6565

6666
if (!response) {
6767
this.errorService.error('Tenor trending could not be fetched');
6868
resolve();
6969
return;
7070
}
7171

72-
resolve(response.results);
72+
resolve(response);
7373
} catch (err) {
7474
this.errorService.error(err.message);
7575
resolve();

studio/src/app/utils/tenor.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ interface TenorGif {
3636
media: TenorMedia[];
3737
}
3838

39-
interface TenorTrendingResponse {
39+
interface TenorSearchResponse {
4040
results: TenorGif[];
41+
next: string | number;
4142
}
4243

4344
interface TenorCategory {

0 commit comments

Comments
 (0)