Skip to content

Commit

Permalink
Add has ticket function (#12)
Browse files Browse the repository at this point in the history
* add function definition

* add android implementation

* modify returned value Android

* add IOS version

* upgrade version

* update comment

* fix ios logic

* modify return type

* handle rejection
  • Loading branch information
debiff authored Dec 21, 2021
1 parent 1210e3e commit 946e86f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import android.util.Log;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.zendesk.logger.Logger;
import com.zendesk.service.ErrorResponse;
import com.zendesk.service.ZendeskCallback;

import java.lang.String;
import java.util.ArrayList;
Expand All @@ -33,6 +36,8 @@
import zendesk.messaging.MessagingActivity;
import zendesk.core.Zendesk;
import zendesk.support.CustomField;
import zendesk.support.Request;
import zendesk.support.RequestProvider;
import zendesk.support.Support;
import zendesk.support.guide.HelpCenterActivity;
import zendesk.support.guide.ViewArticleActivity;
Expand All @@ -49,6 +54,7 @@ public class RNZendeskChat extends ReactContextBaseJavaModule {
private final HashMap<String, CustomField> customFields;
// Contains the aggregate of all the logs sent by the app
private StringBuffer log;
private RequestProvider requestProvider;

public RNZendeskChat(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -206,14 +212,30 @@ public void openTicket(){
.show(activity);
}

@ReactMethod
public void hasOpenedTickets(final Promise promise){
requestProvider = Support.INSTANCE.provider().requestProvider();
requestProvider.getAllRequests(new ZendeskCallback<List<Request>>() {
@Override
public void onSuccess(List<Request> requests) {
// Handle success
promise.resolve(requests.size());
}
@Override
public void onError(ErrorResponse errorResponse) {
// Handle error
promise.reject(errorResponse.getReason());
}
});
}

@ReactMethod
public void showTickets(){
Activity activity = getCurrentActivity();
Activity activity = getCurrentActivity();

// Show the user's tickets
RequestListActivity.builder()
.withContactUsButtonVisible(false)
.show(activity);
// Show the user's tickets
RequestListActivity.builder()
.show(activity);
}

@ReactMethod
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ declare module 'io-react-native-zendesk' {
// function to shows all the tickets of the user
export function showTickets(): void;

// function the return the number of tickets created by the user
export function hasOpenedTickets(): Promise<number>;

// function to set visitor info in chat
export function setVisitorInfo(visitorInfo: UserInfo): void;

Expand Down
15 changes: 15 additions & 0 deletions ios/RNZendeskChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ - (void) addTicketCustomFieldFunction:(NSString *)key withValue:(NSString *)valu
[ZDKSupport initializeWithZendesk: [ZDKZendesk instance]];
[ZDKChat initializeWithAccountKey:options[@"key"] appId:options[@"appId"] queue:dispatch_get_main_queue()];
[ZDKAnswerBot initializeWithZendesk:[ZDKZendesk instance] support:[ZDKSupport instance]];

}
RCT_EXPORT_METHOD(initChat:(NSString *)key) {
[ZDKChat initializeWithAccountKey:key queue:dispatch_get_main_queue()];
Expand All @@ -137,6 +138,20 @@ - (void) addTicketCustomFieldFunction:(NSString *)key withValue:(NSString *)valu
[self registerForNotifications:deviceToken];
});
}

RCT_EXPORT_METHOD(hasOpenedTickets:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
ZDKRequestProvider *provider = [ZDKRequestProvider new];
[provider getAllRequestsWithCallback:^(ZDKRequestsWithCommentingAgents *requestsWithCommentingAgents, NSError *error) {
if(error != nil){
reject(@"event_failure", @"no response", nil);
return;
}
NSNumber *ticketsCount = [NSNumber numberWithInt:[requestsWithCommentingAgents requests].count];
resolve(@[ticketsCount]);
}];
}

- (UIColor *)colorFromHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-react-native-zendesk",
"version": "0.3.12",
"version": "0.3.13",
"description": "React native wrapper for Zendesk Unified SDK",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 946e86f

Please sign in to comment.