-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user): implement subscription business logics
- Loading branch information
Showing
15 changed files
with
368 additions
and
122 deletions.
There are no files selected for viewing
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
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
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
File renamed without changes.
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
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,93 @@ | ||
import type { Consent, Customer, CustomerConsent, UpdateCustomerConsentsPayload, UpdateCustomerPayload } from 'types/account'; | ||
import { useMutation, useQuery } from 'react-query'; | ||
import type { CustomerFormErrors, CustomerFormValues, FormErrors } from 'types/form'; | ||
|
||
import { ConfigStore } from '../../stores/ConfigStore'; | ||
import { AccountStore } from '../../stores/AccountStore'; | ||
import { getCustomerConsents, getPublisherConsents, updateCustomer, updateCustomerConsents } from '../../services/account.service'; | ||
|
||
type ChildrenParams = { | ||
customer: Customer; | ||
errors: FormErrors<UpdateCustomerPayload>; | ||
isLoading: boolean; | ||
consentsLoading: boolean; | ||
publisherConsents?: Consent[]; | ||
customerConsents?: CustomerConsent[]; | ||
onUpdateEmailSubmit: (values: CustomerFormValues) => void; | ||
onUpdateInfoSubmit: (values: CustomerFormValues) => void; | ||
onUpdateConsentsSubmit: (consents: CustomerConsent[]) => void; | ||
onReset: () => void; | ||
}; | ||
|
||
type Props = { | ||
children: (data: ChildrenParams) => JSX.Element; | ||
fetchConsents?: boolean; | ||
}; | ||
|
||
const AccountContainer = ({ children, fetchConsents = true }: Props): JSX.Element => { | ||
const customer = AccountStore.useState((state) => state.user); | ||
const auth = AccountStore.useState((state) => state.auth); | ||
const { config } = ConfigStore.getRawState(); | ||
const { cleengId, cleengSandbox } = config; | ||
const jwt = auth?.jwt || ''; | ||
const publisherId = cleengId || ''; | ||
const customerId = customer?.id || ''; | ||
|
||
const customerMutation = useMutation((values: CustomerFormValues) => updateCustomer(values, cleengSandbox, jwt)); | ||
const { mutate: mutateCustomer, isLoading: isMutateCustomerLoading, data: mutateCustomerData, reset } = customerMutation; | ||
|
||
const mutation = useMutation((values: UpdateCustomerConsentsPayload) => updateCustomerConsents(values, cleengSandbox, jwt)); | ||
const { mutate: mutateConsents, isLoading: isMutateConsentsLoading, data: mutateConsentsData } = mutation; | ||
|
||
const enabled = fetchConsents && !!publisherId && !!customer?.id; | ||
const fetchPublicherConsents = useQuery(['publisherConsents'], () => getPublisherConsents({ publisherId }, cleengSandbox), { enabled }); | ||
const { data: publisherConsents, isLoading: publisherConsentsLoading } = fetchPublicherConsents; | ||
|
||
const fetchCustomerConsents = useQuery(['customerConsents'], () => getCustomerConsents({ customerId }, cleengSandbox, jwt), { enabled }); | ||
const { data: customerConsents, isLoading: customerConsentsLoading } = fetchCustomerConsents; | ||
|
||
const onUpdateEmailSubmit = ({ id, email, confirmationPassword }: CustomerFormValues) => mutateCustomer({ id, email, confirmationPassword }); | ||
const onUpdateInfoSubmit = ({ id, firstName, lastName }: CustomerFormValues) => mutateCustomer({ id, firstName, lastName }); | ||
const onUpdateConsentsSubmit = (consents: CustomerConsent[]) => mutateConsents({ id: customerId, consents }); | ||
|
||
const translateErrors = (errors?: string[]) => { | ||
const formErrors: CustomerFormErrors = {}; | ||
|
||
errors?.map((error) => { | ||
switch (error) { | ||
case 'Invalid param email': | ||
formErrors.email = 'Invalid email address!'; | ||
break; | ||
case 'Customer email already exists': | ||
formErrors.email = 'Email already exists!'; | ||
break; | ||
case 'Please enter a valid e-mail address.': | ||
formErrors.email = 'Please enter a valid e-mail address.'; | ||
break; | ||
case 'Invalid confirmationPassword': { | ||
formErrors.confirmationPassword = 'Password incorrect!'; | ||
break; | ||
} | ||
default: | ||
console.info('Unknown error', error); | ||
return; | ||
} | ||
}); | ||
return formErrors; | ||
}; | ||
|
||
return children({ | ||
customer, | ||
isLoading: isMutateCustomerLoading || isMutateConsentsLoading, | ||
errors: translateErrors(mutateCustomerData?.errors || mutateConsentsData?.errors), | ||
publisherConsents: publisherConsents?.responseData?.consents, | ||
customerConsents: customerConsents?.responseData?.consents, | ||
consentsLoading: publisherConsentsLoading || customerConsentsLoading, | ||
onUpdateEmailSubmit, | ||
onUpdateInfoSubmit, | ||
onUpdateConsentsSubmit, | ||
onReset: reset, | ||
} as ChildrenParams); | ||
}; | ||
|
||
export default AccountContainer; |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,53 @@ | ||
import { useMutation, useQuery } from 'react-query'; | ||
import type { PaymentDetail, Subscription, Transaction, UpdateSubscriptionPayload } from 'types/subscription'; | ||
|
||
import { getPaymentDetails, getSubscriptions, getTransactions, updateSubscriptions } from '../../services/subscription.service'; | ||
import { AccountStore } from '../../stores/AccountStore'; | ||
import { ConfigStore } from '../../stores/ConfigStore'; | ||
|
||
type ChildrenParams = { | ||
subscriptions: Subscription[]; | ||
paymentDetails: PaymentDetail[]; | ||
transactions: Transaction[]; | ||
isLoading: boolean; | ||
onUpdateSubscriptionSubmit: (subscriptions: Subscription) => void; | ||
}; | ||
|
||
type Props = { | ||
children: (data: ChildrenParams) => JSX.Element; | ||
}; | ||
|
||
const SubscriptionContainer = ({ children }: Props): JSX.Element => { | ||
const customer = AccountStore.useState((state) => state.user); | ||
const auth = AccountStore.useState((state) => state.auth); | ||
const { config } = ConfigStore.getRawState(); | ||
const { cleengSandbox: sandbox } = config; | ||
const jwt = auth?.jwt || ''; | ||
const customerId = customer?.id || ''; | ||
|
||
const getSubscriptionsQuery = useQuery(['subscriptions', customerId], () => getSubscriptions({ customerId }, sandbox, jwt)); | ||
const { data: subscriptions, isLoading: isSubscriptionsLoading } = getSubscriptionsQuery; | ||
|
||
const subscriptionMutation = useMutation((values: UpdateSubscriptionPayload) => updateSubscriptions(values, sandbox, jwt)); | ||
const { mutate: mutateSubscriptions, isLoading: isSubscriptionMutationLoading } = subscriptionMutation; | ||
|
||
const getPaymentDetailsQuery = useQuery(['paymentDetails', customerId], () => getPaymentDetails({ customerId }, sandbox, jwt)); | ||
const { data: paymentDetails, isLoading: isPaymentDetailsLoading } = getPaymentDetailsQuery; | ||
|
||
const getTransactionsQuery = useQuery(['transactions', customerId], () => getTransactions({ customerId }, sandbox, jwt)); | ||
const { data: transactions, isLoading: isTransactionsLoading } = getTransactionsQuery; | ||
|
||
const onUpdateSubscriptionSubmit = ({ offerId, status }: Subscription, cancellationReason?: string) => { | ||
mutateSubscriptions({ customerId, offerId, status, cancellationReason }); | ||
}; | ||
|
||
return children({ | ||
subscriptions: subscriptions?.responseData, | ||
paymentDetails: paymentDetails?.responseData.paymentDetails, | ||
transactions: transactions?.responseData, | ||
isLoading: isSubscriptionsLoading || isPaymentDetailsLoading || isTransactionsLoading || isSubscriptionMutationLoading, | ||
onUpdateSubscriptionSubmit, | ||
} as ChildrenParams); | ||
}; | ||
|
||
export default SubscriptionContainer; |
Oops, something went wrong.