Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIT-54: Add API hook to connect to Publiq API
As seen in axios/axios#4998 (comment), axios causes an error in the latest RN version, therefor we need to upgrade to the most recent beta version for it to work.
- Loading branch information
1 parent
e4b35de
commit 7bcfce5
Showing
14 changed files
with
200 additions
and
29 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
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,38 @@ | ||
import { useCallback } from 'react'; | ||
import { Config } from 'react-native-config'; | ||
import { QueryObserverOptions, useQuery } from '@tanstack/react-query'; | ||
|
||
import { useAuthentication } from '../_context'; | ||
import { HttpClient, TApiError } from '../_http'; | ||
import { Headers, Params } from '../_http/HttpClient'; | ||
|
||
type TGetOptions = { | ||
enabled?: boolean; | ||
headers?: Headers; | ||
onError?: QueryObserverOptions['onError']; | ||
onSuccess?: QueryObserverOptions['onSuccess']; | ||
params?: Params; | ||
}; | ||
|
||
export function usePubliqApi() { | ||
const { accessToken } = useAuthentication(); | ||
|
||
const defaultHeaders: Headers = { | ||
Authorization: `Bearer ${accessToken}`, | ||
}; | ||
|
||
const get = useCallback( | ||
<T>(queryKey: unknown[], path: string, { headers = {}, params = {}, ...options }: TGetOptions = {}) => { | ||
return useQuery<T, TApiError>({ | ||
enabled: !!accessToken && (options.enabled === undefined || options.enabled), | ||
onError: options.onError, | ||
onSuccess: options.onSuccess, | ||
queryFn: async () => HttpClient.get<T>(`${Config.API_HOST}${path}`, params, { ...defaultHeaders, ...headers }), | ||
queryKey, | ||
}); | ||
}, | ||
[Config.API_HOST, accessToken], | ||
); | ||
|
||
return { get }; | ||
} |
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
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 @@ | ||
export * from './passholder'; |
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,120 @@ | ||
export type TCity = { | ||
/** Name of the city */ | ||
city: string; | ||
/** Postalcode of the city */ | ||
postalCode: string; | ||
}; | ||
|
||
export type TAddress = TCity & { | ||
/** Postal box number. */ | ||
box: string; | ||
/** ISO 3166-1 alpha-2 country code. */ | ||
country: string; | ||
/** House number. */ | ||
number: string; | ||
/** Street name of the address. */ | ||
street: string; | ||
}; | ||
|
||
export type TCardSystemMembership = { | ||
/** A region, usually one or multiple municipalities in Belgium, that uses UiTPAS and provides discounts and/or rewards. For example "Paspartoe" (Brussels), UiTPAS Leuven, UiTPAS Hasselt, UiTPAS Gent, and so on. */ | ||
cardSystem: { | ||
/** Indicates whether this cardsystem allows online cardless registrations. */ | ||
allowsCardlessRegistration?: boolean; | ||
/** Branding information of the card system */ | ||
branding?: { | ||
/** URL to the logo of the card system */ | ||
logo: string; | ||
/** Color code of the primary branding color. */ | ||
primaryColor: string; | ||
/** Color code of the secondary branding color. */ | ||
secondaryColor: string; | ||
}; | ||
/** List of cities that are part of this card system */ | ||
cities: TCity[]; | ||
/** ID of the card system */ | ||
id: number; | ||
/** Links of the card system */ | ||
links: { | ||
/** URL of the website of the card system */ | ||
website: string; | ||
}; | ||
/** Name of the card system. */ | ||
name: string; | ||
/** Indicates whether this is a permanent card system */ | ||
permanent?: boolean; | ||
}; | ||
/** If the passholder has right to a social tariff, this object contains details like the end date. */ | ||
socialTariff?: { | ||
/** Exact moment that the passholder's right to a social tariff expires. */ | ||
endDate: string; | ||
/** If true, the passholder's right to a social tariff has completely expired (the end date has passed and the passholder is no longer in a grace period). */ | ||
expired: boolean; | ||
/** When the end date of the right to a social tariff has passed, the passholder may still be in a grace period that they can buy tickets at a social tariff until their right to a social tariff has been renewed. */ | ||
inGracePeriod: boolean; | ||
}; | ||
status?: 'ACTIVE' | 'BLOCKED'; | ||
/** The UiTPAS number of the card that is linked to this card system membership. It is possible to have a CardSystemMembership without a card. However, a passholder always has at least one CardSystemMembership with a card. */ | ||
uitpasNumber: string; | ||
}; | ||
|
||
export type TPassHolder = { | ||
/** Address that the passholder lives at. Always present in responses. Passholders living outside of Belgium (usually near the border) will only have a postalCode and city in their address. */ | ||
address: TCity | TAddress; | ||
/** This field is always available in responses. */ | ||
cardSystemMemberships: TCardSystemMembership[]; | ||
/** | ||
* Name of the municipality that the passholder lives in. Deprecated in favor of address.city. | ||
* @deprecated | ||
*/ | ||
city?: string; | ||
/** This field is always available in responses. */ | ||
creationDate: string; | ||
/** Date that the passholder was born. */ | ||
dateOfBirth?: string; | ||
/** Contact email address of the passholder. Not present for every passholder. Multiple passholders can have the same email address. */ | ||
email?: string; | ||
/** First name of the passholder. */ | ||
firstName: string; | ||
/** Gender of the passholder. */ | ||
gender?: 'MALE' | 'FEMALE' | 'X'; | ||
/** This field is always available in responses. */ | ||
id: string; | ||
/** Unique national (Belgian) INSZ number of an individual passholder to look up. */ | ||
inszNumber?: string; | ||
/** Last name of the passholder. */ | ||
name: string; | ||
/** Human-readable name of the passholder's nationality. */ | ||
nationality?: string; | ||
/** Permissions that the passholder has given to be contacted. */ | ||
optInPreferences?: { | ||
/** Rewards, actions and events selected specifically for the passholder based on their UiTPAS history. */ | ||
infoMails: boolean; | ||
/** Notification when you reach an important UiTPAS milestone, for example a specific amount of points or an exclusive reward becomes available to you. */ | ||
milestoneMails: boolean; | ||
/** Sporadic post mail with information about UiTPAS. Will be sent to the passholder's postal address. */ | ||
post: boolean; | ||
/** Important information about the functionality of UiTPAS. */ | ||
serviceMails: boolean; | ||
/** Free (sporadic) SMS messages with rewards, actions and events selected specifically for the passholder based on their UiTPAS history. */ | ||
sms: boolean; | ||
}; | ||
/** Phone number that the passholder has registered, for example for SMS alerts. */ | ||
phoneNumber?: string; | ||
/** Amount of points the passholder has currently saved (and not used). This field is always available in responses. */ | ||
points: number; | ||
/** | ||
* Postal code of the municipality that the passholder lives in. Deprecated in favor of address.postalCode | ||
* @deprecated | ||
*/ | ||
postalCode?: string; | ||
/** An organisation that partners with UiTPAS to provide discounts and/or rewards, and/or allows points to be collected at their events. */ | ||
registrationOrganizer: { | ||
/** Unique ID of an UiTPAS organizer. (Same as its ID in UiTdatabank) */ | ||
id: string; | ||
/** Human-readable name of an UiTPAS organizer. */ | ||
name: string; | ||
}; | ||
/** Whether or not the passholder has a an UiTID registered. This field is always available in responses. */ | ||
uitidStatus: 'REGISTERED' | 'UNREGISTERED'; | ||
}; |
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,7 @@ | ||
import { usePubliqApi } from '../../_hooks/usePubliqApi'; | ||
import { TPassHolder } from '../_models'; | ||
|
||
export function useGetMe() { | ||
const api = usePubliqApi(); | ||
return api.get<TPassHolder>(['me'], '/passholders/me'); | ||
} |
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