-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
State refactor: edit receiver details (#26)
- Loading branch information
Showing
9 changed files
with
258 additions
and
240 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { API_URL } from "constants/settings"; | ||
import { fetchApi } from "helpers/fetchApi"; | ||
import { formatPaymentReceiver } from "helpers/formatPaymentReceiver"; | ||
import { formatReceiver } from "helpers/formatReceiver"; | ||
import { ApiReceiver, AppError } from "types"; | ||
|
||
export const useReceiversReceiverId = (receiverId: string | undefined) => { | ||
export const useReceiversReceiverId = <T>({ | ||
receiverId, | ||
dataFormat, | ||
receiverWalletId, | ||
}: { | ||
receiverId: string | undefined; | ||
dataFormat: "receiver" | "paymentReceiver"; | ||
receiverWalletId?: string; | ||
}) => { | ||
const query = useQuery<ApiReceiver, AppError>({ | ||
queryKey: ["receivers", receiverId], | ||
queryKey: ["receivers", dataFormat, receiverId, { receiverWalletId }], | ||
queryFn: async () => { | ||
return await fetchApi(`${API_URL}/receivers/${receiverId}`); | ||
}, | ||
enabled: !!receiverId, | ||
}); | ||
|
||
return query; | ||
const formatData = (data: ApiReceiver) => { | ||
return dataFormat === "receiver" | ||
? formatReceiver(data) | ||
: formatPaymentReceiver(data, receiverWalletId); | ||
}; | ||
|
||
return { | ||
...query, | ||
data: query.data ? (formatData(query.data) as T) : undefined, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { API_URL } from "constants/settings"; | ||
import { fetchApi } from "helpers/fetchApi"; | ||
import { sanitizeObject } from "helpers/sanitizeObject"; | ||
import { AppError } from "types"; | ||
|
||
export const useUpdateReceiverDetails = (receiverId: string | undefined) => { | ||
const mutation = useMutation({ | ||
mutationFn: (fields: { email: string; externalId: string }) => { | ||
const fieldsToSubmit = sanitizeObject({ | ||
email: fields.email, | ||
external_id: fields.externalId, | ||
}); | ||
|
||
if (Object.keys(fieldsToSubmit).length === 0) { | ||
return new Promise((_, reject) => | ||
reject({ | ||
message: | ||
"Update receiver info requires at least one field to submit", | ||
}), | ||
); | ||
} | ||
|
||
return fetchApi( | ||
`${API_URL}/receivers/${receiverId}`, | ||
{ | ||
method: "PATCH", | ||
body: JSON.stringify(fieldsToSubmit), | ||
}, | ||
{ omitContentType: true }, | ||
); | ||
}, | ||
cacheTime: 0, | ||
}); | ||
|
||
return { | ||
...mutation, | ||
error: mutation.error as AppError, | ||
data: mutation.data as { message: string }, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const LoadingContent = () => { | ||
return <div className="Note">Loading…</div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ApiReceiver, ReceiverDetails } from "types"; | ||
|
||
export const formatReceiver = (receiver: ApiReceiver): ReceiverDetails => ({ | ||
id: receiver.id, | ||
phoneNumber: receiver.phone_number, | ||
email: receiver.email, | ||
orgId: receiver.external_id, | ||
// TODO: how to handle multiple | ||
assetCode: receiver.received_amounts?.[0].asset_code, | ||
totalReceived: receiver.received_amounts?.[0].received_amount, | ||
stats: { | ||
paymentsTotalCount: Number(receiver.total_payments), | ||
paymentsSuccessfulCount: Number(receiver.successful_payments), | ||
paymentsFailedCount: Number(receiver.failed_payments), | ||
paymentsRemainingCount: Number(receiver.remaining_payments), | ||
}, | ||
wallets: receiver.wallets.map((w) => ({ | ||
id: w.id, | ||
stellarAddress: w.stellar_address, | ||
provider: w.wallet.name, | ||
invitedAt: w.invited_at, | ||
createdAt: w.created_at, | ||
smsLastSentAt: w.last_sms_sent, | ||
totalPaymentsCount: Number(w.payments_received), | ||
// TODO: how to handle multiple | ||
assetCode: w.received_amounts[0].asset_code, | ||
totalAmountReceived: w.received_amounts[0].received_amount, | ||
// TODO: withdrawn amount | ||
withdrawnAmount: "", | ||
})), | ||
verifications: receiver.verifications.map((v) => ({ | ||
verificationField: v.VerificationField, | ||
value: v.HashedValue, | ||
})), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.