Skip to content

Commit

Permalink
WIP on a pagination mechanism for dynamic fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-Ag committed Jul 16, 2024
1 parent 151d26c commit 1e9d616
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/destination-kit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ export interface Result {
data?: JSONObject | null
}

export interface DynamicFieldPagination {
previousPage?: string
nextPage?: string
}

export interface DynamicFieldContext {
/** The index of the item in an array of objects that we are requesting data for */
selectedArrayIndex?: number
/** The key within a dynamic object for which we are requesting values */
selectedKey?: string

paging?: DynamicFieldPagination
}

export interface ExecuteInput<
Expand Down Expand Up @@ -66,7 +73,7 @@ export interface ExecuteInput<

export interface DynamicFieldResponse {
choices: DynamicFieldItem[]
nextPage?: string
paging?: DynamicFieldPagination
error?: DynamicFieldError
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DynamicFieldItem, DynamicFieldError, RequestClient } from '@segment/actions-core'
import { DynamicFieldItem, DynamicFieldError, DynamicFieldPagination, RequestClient } from '@segment/actions-core'

const FACEBOOK_API_VERSION = 'v20.0'

Expand Down Expand Up @@ -43,9 +43,16 @@ export default class FacebookClient {
})
}

getAllAudiences = async (): Promise<[DynamicFieldItem[], DynamicFieldError | undefined]> => {
getAllAudiences = async (
paging: DynamicFieldPagination | undefined
): Promise<{ choices: DynamicFieldItem[]; error: DynamicFieldError | undefined; nextPage: string }> => {
let nextPageParam = ''
if (paging?.next) {
nextPageParam = `&after=${paging.next}`
}

const { data } = await this.request<GetAllAudienceResponse>(
`${this.baseUrl}${this.adAccountId}/customaudiences?fields=id,name`
`${this.baseUrl}${this.adAccountId}/customaudiences?fields=id,name${nextPageParam}`
)

// console.log('data.paging', data.paging)
Expand All @@ -54,7 +61,11 @@ export default class FacebookClient {
label: name
}))

return [choices, undefined]
return {
choices,
error: undefined,
nextPage: data.paging.next
}
}

private formatAdAccount(adAccountId: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ const action: ActionDefinition<Settings, Payload> = {
type: 'string',
label: 'Existing Audience ID',
description: 'The ID of the audience in Facebook.',
dynamic: async (request, { settings }) => {
dynamic: async (request, { settings, dynamicFieldContext }) => {
const fbClient = new FacebookClient(request, settings.adAccountId)
const [choices, error] = await fbClient.getAllAudiences()
const { choices, error, nextPage } = await fbClient.getAllAudiences(dynamicFieldContext?.paging)

if (error) {
return { error, choices: [] }
}

return {
choices
choices,
paging: {
nextPage,
previousPage: dynamicFieldContext?.paging?.previousPage
}
}
}
}
Expand Down

0 comments on commit 1e9d616

Please sign in to comment.