Skip to content

Commit f318160

Browse files
author
piotr pietruszewski
committed
feat(android): added help center fetch methods
1 parent c56bd67 commit f318160

File tree

4 files changed

+209
-17
lines changed

4 files changed

+209
-17
lines changed

android/src/main/java/com/intercom/reactnative/IntercomErrorCodes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ public class IntercomErrorCodes {
2121
public static final String DISPLAY_HELP_CENTER_COLLECTIONS = "210";
2222
public static final String HANDLE_PUSH_MESSAGE = "301";
2323
public static final String SEND_TOKEN_TO_INTERCOM = "302";
24+
public static final String FETCH_HELP_CENTER_COLLECTIONS = "901";
25+
public static final String FETCH_HELP_CENTER_COLLECTION = "902";
26+
public static final String SEARCH_HELP_CENTER = "903";
2427
}
2528

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.intercom.reactnative;
2+
3+
4+
import com.facebook.react.bridge.Arguments;
5+
import com.facebook.react.bridge.ReadableArray;
6+
import com.facebook.react.bridge.ReadableMap;
7+
import com.facebook.react.bridge.WritableArray;
8+
import com.facebook.react.bridge.WritableMap;
9+
10+
11+
import java.util.List;
12+
13+
import io.intercom.android.sdk.helpcenter.api.HelpCenterArticleSearchResult;
14+
import io.intercom.android.sdk.helpcenter.collections.HelpCenterCollection;
15+
import io.intercom.android.sdk.helpcenter.sections.HelpCenterArticle;
16+
import io.intercom.android.sdk.helpcenter.sections.HelpCenterCollectionContent;
17+
import io.intercom.android.sdk.helpcenter.sections.HelpCenterSection;
18+
19+
public class IntercomHelpCenterHelpers {
20+
21+
public static ReadableArray parseArticlesToReadableArray(List<HelpCenterArticle> itemArticles) {
22+
WritableArray articles = Arguments.createArray();
23+
24+
HelpCenterArticle[] articlesArray = new HelpCenterArticle[itemArticles.size()];
25+
articlesArray = itemArticles.toArray(articlesArray);
26+
27+
for (HelpCenterArticle article : articlesArray) {
28+
WritableMap item = Arguments.createMap();
29+
item.putString("id", article.getArticleId());
30+
item.putString("title", article.getTitle());
31+
articles.pushMap(item);
32+
}
33+
return articles;
34+
}
35+
36+
public static ReadableArray parseHelpCenterArticleSearchToReadableArray(List<HelpCenterArticleSearchResult> helpCenterArticleSearchResultList) {
37+
WritableArray articles = Arguments.createArray();
38+
39+
HelpCenterArticleSearchResult[] articlesArray = new HelpCenterArticleSearchResult[helpCenterArticleSearchResultList.size()];
40+
articlesArray = helpCenterArticleSearchResultList.toArray(articlesArray);
41+
42+
for (HelpCenterArticleSearchResult article : articlesArray) {
43+
WritableMap item = Arguments.createMap();
44+
item.putString("id", article.getArticleId());
45+
item.putString("title", article.getTitle());
46+
item.putString("matchingSnippet", article.getMatchingSnippet());
47+
item.putString("summary", article.getSummary());
48+
articles.pushMap(item);
49+
}
50+
return articles;
51+
}
52+
53+
public static ReadableMap parseHelpCenterCollectionsContentToReadableMap(HelpCenterCollectionContent helpCenterCollectionContent) {
54+
WritableMap helpCenterCollection = Arguments.createMap();
55+
helpCenterCollection.putString("id", helpCenterCollectionContent.getCollectionId());
56+
helpCenterCollection.putString("title", helpCenterCollectionContent.getTitle());
57+
helpCenterCollection.putString("summary", helpCenterCollectionContent.getSummary());
58+
59+
ReadableArray articles = parseArticlesToReadableArray(helpCenterCollectionContent.getHelpCenterArticles());
60+
helpCenterCollection.putArray("articles", articles);
61+
62+
ReadableArray sections = parseHelpCenterSectionsToReadableArray(helpCenterCollectionContent.getHelpCenterSections());
63+
helpCenterCollection.putArray("sections", sections);
64+
65+
66+
return helpCenterCollection;
67+
}
68+
69+
public static ReadableMap parseHelpCenterSectionToReadableMap(HelpCenterSection helpCenterSection) {
70+
WritableMap section = Arguments.createMap();
71+
section.putString("id", helpCenterSection.getTitle());
72+
73+
74+
ReadableArray articles = parseArticlesToReadableArray(helpCenterSection.getHelpCenterArticles());
75+
section.putArray("articles", articles);
76+
77+
78+
return section;
79+
}
80+
81+
public static ReadableArray parseHelpCenterSectionsToReadableArray(List<HelpCenterSection> helpCenterSectionList) {
82+
WritableArray sections = Arguments.createArray();
83+
84+
HelpCenterSection[] sectionsArray = new HelpCenterSection[helpCenterSectionList.size()];
85+
sectionsArray = helpCenterSectionList.toArray(sectionsArray);
86+
;
87+
88+
for (HelpCenterSection section : sectionsArray) {
89+
sections.pushMap(parseHelpCenterSectionToReadableMap(section));
90+
}
91+
return sections;
92+
}
93+
94+
public static ReadableArray parseHelpCenterCollectionsToReadableArray(List<HelpCenterCollection> helpCenterCollections) {
95+
HelpCenterCollection[] collectionsArray = new HelpCenterCollection[helpCenterCollections.size()];
96+
collectionsArray = helpCenterCollections.toArray(collectionsArray);
97+
WritableArray collections = Arguments.createArray();
98+
for (HelpCenterCollection helpCenterCollection : collectionsArray) {
99+
WritableMap item = Arguments.createMap();
100+
item.putString("id", helpCenterCollection.getId());
101+
item.putString("title", helpCenterCollection.getTitle());
102+
item.putString("summary", helpCenterCollection.getSummary());
103+
collections.pushMap(item);
104+
}
105+
return collections;
106+
}
107+
108+
109+
110+
}

android/src/main/java/com/intercom/reactnative/IntercomModule.java

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
import androidx.annotation.NonNull;
88
import androidx.annotation.Nullable;
99

10-
import com.facebook.react.bridge.Arguments;
1110
import com.facebook.react.bridge.Promise;
1211
import com.facebook.react.bridge.ReactApplicationContext;
1312
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1413
import com.facebook.react.bridge.ReactMethod;
1514
import com.facebook.react.bridge.ReadableArray;
1615
import com.facebook.react.bridge.ReadableMap;
1716
import com.facebook.react.module.annotations.ReactModule;
18-
import com.facebook.react.bridge.WritableMap;
19-
import com.facebook.react.bridge.WritableArray;
2017
import com.google.firebase.messaging.RemoteMessage;
2118

2219
import org.jetbrains.annotations.NotNull;
@@ -27,8 +24,12 @@
2724
import io.intercom.android.sdk.Intercom;
2825
import io.intercom.android.sdk.UserAttributes;
2926
import io.intercom.android.sdk.api.ReactNativeHeaderInterceptor;
27+
import io.intercom.android.sdk.helpcenter.api.CollectionContentRequestCallback;
3028
import io.intercom.android.sdk.helpcenter.api.CollectionRequestCallback;
29+
import io.intercom.android.sdk.helpcenter.api.HelpCenterArticleSearchResult;
30+
import io.intercom.android.sdk.helpcenter.api.SearchRequestCallback;
3131
import io.intercom.android.sdk.helpcenter.collections.HelpCenterCollection;
32+
import io.intercom.android.sdk.helpcenter.sections.HelpCenterCollectionContent;
3233
import io.intercom.android.sdk.identity.Registration;
3334
import io.intercom.android.sdk.push.IntercomPushClient;
3435

@@ -303,28 +304,19 @@ public void fetchHelpCenterCollections(Promise promise) {
303304
CollectionRequestCallback collectionRequestCallback = new CollectionRequestCallback() {
304305
@Override
305306
public void onComplete(@NotNull List<HelpCenterCollection> list) {
306-
HelpCenterCollection[] returnArray = new HelpCenterCollection[list.size()];
307-
308-
returnArray = list.toArray(returnArray);
309-
WritableArray promiseArray = Arguments.createArray();
310-
for (HelpCenterCollection helpCenterCollection : returnArray) {
311-
WritableMap item = Arguments.createMap();
312-
item.putString("id", helpCenterCollection.getId());
313-
item.putString("title", helpCenterCollection.getTitle());
314-
item.putString("summary", helpCenterCollection.getSummary());
315-
promiseArray.pushMap(item);
316-
}
317-
promise.resolve(promiseArray);
307+
promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsToReadableArray(list));
318308
}
319309

320310
@Override
321311
public void onError(int i) {
312+
Log.e(NAME, "fetchHelpCenterCollections error");
322313
promise.reject(String.valueOf(i), "fetchHelpCenterCollections error");
323314
}
324315

325316
@Override
326317
public void onFailure() {
327-
promise.reject("901", "fetchHelpCenterCollections faliure");
318+
Log.e(NAME, "fetchHelpCenterCollections failure");
319+
promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, "fetchHelpCenterCollections failure");
328320
}
329321
};
330322

@@ -333,10 +325,75 @@ public void onFailure() {
333325
} catch (Exception err) {
334326
Log.e(NAME, "fetchHelpCenterCollections error:");
335327
Log.e(NAME, err.toString());
336-
promise.reject(IntercomErrorCodes.DISPLAY_HELP_CENTER_COLLECTIONS, err.toString());
328+
promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, err.toString());
329+
}
330+
}
331+
332+
@ReactMethod
333+
public void fetchHelpCenterCollection(String collectionId, Promise promise) {
334+
try {
335+
336+
CollectionContentRequestCallback collectionContentCallback = new CollectionContentRequestCallback() {
337+
@Override
338+
public void onComplete(@NotNull HelpCenterCollectionContent helpCenterCollectionContent) {
339+
promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsContentToReadableMap(helpCenterCollectionContent));
340+
}
341+
342+
@Override
343+
public void onError(int i) {
344+
Log.e(NAME, "fetchHelpCenterCollection error");
345+
promise.reject(String.valueOf(i), "fetchHelpCenterCollection error");
346+
}
347+
348+
@Override
349+
public void onFailure() {
350+
Log.e(NAME, "fetchHelpCenterCollection failure");
351+
promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "fetchHelpCenterCollection failure");
352+
}
353+
};
354+
355+
Intercom.client().fetchHelpCenterCollection(collectionId, collectionContentCallback);
356+
357+
} catch (Exception err) {
358+
Log.e(NAME, "fetchHelpCenterCollection error:");
359+
Log.e(NAME, err.toString());
360+
promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, err.toString());
337361
}
338362
}
339363

364+
@ReactMethod
365+
public void searchHelpCenter(String searchTerm, Promise promise) {
366+
try {
367+
368+
SearchRequestCallback collectionContentCallback = new SearchRequestCallback() {
369+
@Override
370+
public void onComplete(@NotNull List<HelpCenterArticleSearchResult> helpCenterArticleSearchResult) {
371+
promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterArticleSearchToReadableArray(helpCenterArticleSearchResult));
372+
}
373+
374+
@Override
375+
public void onError(int i) {
376+
Log.e(NAME, "searchHelpCenter error");
377+
promise.reject(String.valueOf(i), "searchHelpCenter error");
378+
}
379+
380+
@Override
381+
public void onFailure() {
382+
Log.e(NAME, "searchHelpCenter failure");
383+
promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchHelpCenter failure");
384+
}
385+
};
386+
387+
Intercom.client().searchHelpCenter(searchTerm, collectionContentCallback);
388+
389+
} catch (Exception err) {
390+
Log.e(NAME, "searchHelpCenter error:");
391+
Log.e(NAME, err.toString());
392+
promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, err.toString());
393+
}
394+
}
395+
396+
340397
@ReactMethod
341398
public void displayCarousel(String carouselId, Promise promise) {
342399
try {

src/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,33 @@ export type Company = {
7777
plan?: string;
7878
};
7979

80+
export type HelpCenterArticle = { it: string; title: string };
81+
export type HelpCenterSection = { name: string; articles: HelpCenterArticle };
8082
export type HelpCenterCollectionItem = {
8183
id: string;
8284
title: string;
8385
summary: string;
8486
};
87+
export type HelpCenterCollectionContent = {
88+
id: string;
89+
name: string;
90+
summary: string;
91+
articles: HelpCenterArticle[];
92+
sections: HelpCenterSection[];
93+
};
94+
export type HelpCenterArticleSearchResult = {
95+
id: string;
96+
title: string;
97+
matchingSnippet: string;
98+
summary: string;
99+
};
85100

86101
export type IntercomType = {
87102
fetchHelpCenterCollections: () => Promise<HelpCenterCollectionItem>;
103+
searchHelpCenter: (term: string) => Promise<HelpCenterArticleSearchResult>;
104+
fetchHelpCenterCollection: (
105+
id: string
106+
) => Promise<HelpCenterCollectionContent>;
88107
displayArticle(articleId: string): Promise<boolean>;
89108
displayCarousel(carouselId: string): Promise<boolean>;
90109
displayHelpCenter(): Promise<boolean>;
@@ -112,7 +131,10 @@ export type IntercomType = {
112131
};
113132

114133
const Intercom = {
134+
searchHelpCenter: (term) => IntercomModule.searchHelpCenter(term),
115135
fetchHelpCenterCollections: () => IntercomModule.fetchHelpCenterCollections(),
136+
fetchHelpCenterCollection: (id) =>
137+
IntercomModule.fetchHelpCenterCollection(id),
116138
displayArticle: (articleId: string) =>
117139
IntercomModule.displayArticle(articleId),
118140
displayCarousel: (carouselId: string) =>

0 commit comments

Comments
 (0)