Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
[PAY-1712] Implement purchases page (#3951)
Browse files Browse the repository at this point in the history
  • Loading branch information
schottra authored Aug 29, 2023
1 parent 314087b commit 7d2f718
Show file tree
Hide file tree
Showing 21 changed files with 631 additions and 73 deletions.
54 changes: 27 additions & 27 deletions packages/common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/AudiusProject/audius-client/issues"
},
"dependencies": {
"@audius/sdk": "3.0.7-beta.3",
"@audius/sdk": "3.0.8-beta.1",
"@fingerprintjs/fingerprintjs-pro": "3.5.6",
"@metaplex-foundation/mpl-token-metadata": "2.5.2",
"@optimizely/optimizely-sdk": "4.0.0",
Expand Down
54 changes: 53 additions & 1 deletion packages/common/src/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { full } from '@audius/sdk'

import { createApi } from 'audius-query'
import { ID, Kind } from 'models'
import {
USDCContentPurchaseType,
USDCPurchaseDetails
} from 'models/USDCTransactions'
import { StringUSDC } from 'models/Wallet'
import { encodeHashId } from 'utils/hashIds'
import { Nullable } from 'utils/typeUtils'

type GetPurchasesArgs = {
userId: Nullable<ID>
offset: number
limit: number
sortMethod?: full.GetPurchasesSortMethodEnum
sortDirection?: full.GetPurchasesSortDirectionEnum
}

const parsePurchase = (purchase: full.Purchase): USDCPurchaseDetails => {
const { contentId, contentType, amount, buyerUserId, sellerUserId, ...rest } =
purchase
return {
...rest,
contentType: contentType as USDCContentPurchaseType,
contentId: Number.parseInt(contentId),
amount: amount as StringUSDC,
buyerUserId: Number.parseInt(buyerUserId),
sellerUserId: Number.parseInt(sellerUserId)
}
}

const userApi = createApi({
reducerPath: 'userApi',
Expand All @@ -17,9 +47,31 @@ const userApi = createApi({
kind: Kind.USERS,
schemaKey: 'user'
}
},
getPurchases: {
fetch: async (
{ offset, limit, userId, sortDirection, sortMethod }: GetPurchasesArgs,
{ audiusSdk, audiusBackend }
) => {
const { data: encodedDataMessage, signature: encodedDataSignature } =
await audiusBackend.signDiscoveryNodeRequest()
const sdk = await audiusSdk()
const { data: purchases = [] } = await sdk.full.users.getPurchases({
limit,
offset,
sortDirection,
sortMethod,
id: encodeHashId(userId!),
userId: encodeHashId(userId!),
encodedDataMessage,
encodedDataSignature
})
return purchases.map(parsePurchase)
},
options: {}
}
}
})

export const { useGetUserById } = userApi.hooks
export const { useGetUserById, useGetPurchases } = userApi.hooks
export const userApiReducer = userApi.reducer
6 changes: 5 additions & 1 deletion packages/common/src/audius-query/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ const useCacheData = <Args, Data>(
endpoint.options.kind,
hookOptions?.shallow
)
return denormalize(nonNormalizedData, apiResponseSchema, entityMap) as Data
return (
endpoint.options.schemaKey
? denormalize(nonNormalizedData, apiResponseSchema, entityMap)
: nonNormalizedData
) as Data
}, isEqual)
}

Expand Down
5 changes: 4 additions & 1 deletion packages/common/src/audius-query/hooks/usePaginatedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const useAllPaginatedQuery = <
const [loadingMore, setLoadingMore] = useState(false)
const { pageSize, ...queryHookOptions } = options
const [page, setPage] = useState(0)
const [status, setStatus] = useState<Status>(Status.IDLE)
const [allData, setAllData] = useState<Data[]>([])
const args = {
...baseArgs,
Expand All @@ -80,7 +81,9 @@ export const useAllPaginatedQuery = <
if (!statusIsNotFinalized(result.status)) {
setLoadingMore(false)
}
setStatus(result.status)
if (result.status !== Status.SUCCESS) return

setAllData((allData) => [...allData, ...result.data])
},
[result.status, args],
Expand All @@ -106,7 +109,7 @@ export const useAllPaginatedQuery = <
return {
...result,
// TODO: add another status for reloading
status: allData?.length > 0 ? Status.SUCCESS : result.status,
status: allData?.length > 0 ? Status.SUCCESS : status,
data: allData,
loadMore,
hasMore
Expand Down
12 changes: 11 additions & 1 deletion packages/common/src/models/USDCTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ export type InAppUSDCPurchaseMetadata = {
}

export enum USDCContentPurchaseType {
TRACK = 'TRACK'
TRACK = 'track'
}

export type USDCPurchaseDetails = {
signature: string
sellerUserId: number
buyerUserId: number
amount: StringUSDC
contentType: USDCContentPurchaseType
contentId: number
createdAt: string
}

export type USDCPurchaseContentMetadata = {
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from './Timestamped'
export * from './Tipping'
export * from './Track'
export * from './TrackAvailabilityType'
export * from './USDCTransactions'
export * from './User'
export * from './Wallet'
export * from './Search'
6 changes: 3 additions & 3 deletions packages/embed/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/embed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"webpack-cli": "4.5.0"
},
"dependencies": {
"@audius/sdk": "3.0.7-beta.3",
"@audius/sdk": "3.0.8-beta.1",
"@audius/stems": "1.5.39",
"amplitude-js": "8.11.1",
"axios": "0.19.2",
Expand Down
38 changes: 19 additions & 19 deletions packages/mobile/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@amplitude/react-native": "2.6.0",
"@audius/common": "1.5.39",
"@audius/sdk": "3.0.7-beta.3",
"@audius/sdk": "3.0.8-beta.1",
"@fingerprintjs/fingerprintjs-pro-react-native": "2.0.0-test.2",
"@gorhom/portal": "1.0.9",
"@hcaptcha/react-native-hcaptcha": "1.3.4",
Expand Down
Loading

0 comments on commit 7d2f718

Please sign in to comment.