generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add subscription/ plans hooks and mutations
- Loading branch information
1 parent
2e9ec85
commit 9f1c594
Showing
11 changed files
with
1,486 additions
and
970 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { QueryClientConfig } from '../types'; | ||
import { | ||
buildChangePlanRoute, | ||
buildSetDefaultCardRoute, | ||
CREATE_SETUP_INTENT_ROUTE, | ||
GET_CARDS_ROUTE, | ||
GET_CURRENT_CUSTOMER, | ||
GET_OWN_PLAN_ROUTE, | ||
GET_PLANS_ROUTE, | ||
} from './routes'; | ||
import { DEFAULT_GET, DEFAULT_PATCH, DEFAULT_POST, failOnError } from './utils'; | ||
|
||
export const getPlans = async ({ API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${GET_PLANS_ROUTE}`, DEFAULT_GET).then( | ||
failOnError, | ||
); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const getOwnPlan = async ({ API_HOST }: QueryClientConfig) => { | ||
const res = await fetch( | ||
`${API_HOST}/${GET_OWN_PLAN_ROUTE}`, | ||
DEFAULT_GET, | ||
).then(failOnError); | ||
return res.json(); | ||
}; | ||
|
||
// payload: planId | ||
export const changePlan = async ( | ||
{ planId }: { planId: string }, | ||
{ API_HOST }: QueryClientConfig, | ||
) => { | ||
const res = await fetch(`${API_HOST}/${buildChangePlanRoute(planId)}`, { | ||
...DEFAULT_PATCH, | ||
headers: {}, | ||
}).then(failOnError); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const getCards = async ({ API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${GET_CARDS_ROUTE}`, DEFAULT_GET).then( | ||
failOnError, | ||
); | ||
return res.json(); | ||
}; | ||
|
||
export const setDefaultCard = async ( | ||
{ cardId }: { cardId: string }, | ||
{ API_HOST }: QueryClientConfig, | ||
) => { | ||
const res = await fetch(`${API_HOST}/${buildSetDefaultCardRoute(cardId)}`, { | ||
...DEFAULT_PATCH, | ||
headers: {}, | ||
}).then(failOnError); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const createSetupIntent = async ({ API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${CREATE_SETUP_INTENT_ROUTE}`, { | ||
...DEFAULT_POST, | ||
headers: {}, | ||
}).then(failOnError); | ||
return res.json(); | ||
}; | ||
|
||
export const getCurrentCustomer = async ({ API_HOST }: QueryClientConfig) => { | ||
const res = await fetch( | ||
`${API_HOST}/${GET_CURRENT_CUSTOMER}`, | ||
DEFAULT_GET, | ||
).then(failOnError); | ||
return res.json(); | ||
}; |
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
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,50 @@ | ||
import { useQuery } from 'react-query'; | ||
import { Map, List } from 'immutable'; | ||
import { QueryClientConfig } from '../types'; | ||
import * as Api from '../api'; | ||
import { | ||
CARDS_KEY, | ||
CURRENT_CUSTOMER_KEY, | ||
OWN_PLAN_KEY, | ||
PLANS_KEY, | ||
} from '../config/keys'; | ||
|
||
export default (queryConfig: QueryClientConfig) => { | ||
const { retry, cacheTime, staleTime } = queryConfig; | ||
const defaultOptions = { | ||
retry, | ||
cacheTime, | ||
staleTime, | ||
}; | ||
|
||
const usePlans = () => | ||
useQuery({ | ||
queryKey: PLANS_KEY, | ||
queryFn: () => Api.getPlans(queryConfig).then((data) => List(data)), | ||
...defaultOptions, | ||
}); | ||
|
||
const useOwnPlan = () => | ||
useQuery({ | ||
queryKey: OWN_PLAN_KEY, | ||
queryFn: () => Api.getOwnPlan(queryConfig).then((data) => Map(data)), | ||
...defaultOptions, | ||
}); | ||
|
||
const useCards = () => | ||
useQuery({ | ||
queryKey: CARDS_KEY, | ||
queryFn: () => Api.getCards(queryConfig).then((data) => List(data)), | ||
...defaultOptions, | ||
}); | ||
|
||
const useCurrentCustomer = () => | ||
useQuery({ | ||
queryKey: CURRENT_CUSTOMER_KEY, | ||
queryFn: () => | ||
Api.getCurrentCustomer(queryConfig).then((data) => Map(data)), | ||
...defaultOptions, | ||
}); | ||
|
||
return { usePlans, useOwnPlan, useCards, useCurrentCustomer }; | ||
}; |
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,58 @@ | ||
import { QueryClient } from 'react-query'; | ||
import { | ||
CURRENT_CUSTOMER_KEY, | ||
MUTATION_KEYS, | ||
OWN_PLAN_KEY, | ||
} from '../config/keys'; | ||
import { | ||
changePlanRoutine, | ||
createSetupIntentRoutine, | ||
setDefaultCardRoutine, | ||
} from '../routines'; | ||
import * as Api from '../api'; | ||
import { QueryClientConfig } from '../types'; | ||
|
||
export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => { | ||
const { notifier } = queryConfig; | ||
|
||
queryClient.setMutationDefaults(MUTATION_KEYS.CHANGE_PLAN, { | ||
mutationFn: (payload) => | ||
Api.changePlan(payload, queryConfig).then(() => payload), | ||
onSuccess: () => { | ||
notifier?.({ type: changePlanRoutine.SUCCESS }); | ||
}, | ||
onError: (error) => { | ||
notifier?.({ type: changePlanRoutine.FAILURE, payload: { error } }); | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries(OWN_PLAN_KEY); | ||
}, | ||
}); | ||
|
||
queryClient.setMutationDefaults(MUTATION_KEYS.CREATE_SETUP_INTENT, { | ||
mutationFn: () => Api.createSetupIntent(queryConfig), | ||
onSuccess: () => { | ||
notifier?.({ type: createSetupIntentRoutine.SUCCESS }); | ||
}, | ||
onError: (error) => { | ||
notifier?.({ | ||
type: createSetupIntentRoutine.FAILURE, | ||
payload: { error }, | ||
}); | ||
}, | ||
}); | ||
|
||
queryClient.setMutationDefaults(MUTATION_KEYS.SET_DEFAULT_CARD, { | ||
mutationFn: (payload) => | ||
Api.setDefaultCard(payload, queryConfig).then(() => payload), | ||
onSuccess: () => { | ||
notifier?.({ type: setDefaultCardRoutine.SUCCESS }); | ||
}, | ||
onError: (error) => { | ||
notifier?.({ type: setDefaultCardRoutine.FAILURE, payload: { error } }); | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries(CURRENT_CUSTOMER_KEY); | ||
}, | ||
}); | ||
}; |
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,5 @@ | ||
import createRoutine from './utils'; | ||
|
||
export const changePlanRoutine = createRoutine('CHANGE_PLAN'); | ||
export const createSetupIntentRoutine = createRoutine('CREATE_SETUP_INTENT'); | ||
export const setDefaultCardRoutine = createRoutine('SET_DEFAULT_CARD'); |
Oops, something went wrong.