bookingApi

mail@pastecode.io avatar
unknown
javascript
2 years ago
6.1 kB
4
Indexable
Never
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { compareDatesDesc, IParking } from './parking';
import { IBikeloop } from './bikeloop';
import { ParkingEventType } from './parkingEventType';
import { IBikeloopStatus } from './bikeloopStatus';
import { IUser } from './user';
import { IUserRequest } from './userRequest';
import { getAccessToken } from '../auth/auth';
import { IQueue } from './queue';
import { IPaymentMethodKey } from './paymentMethodKey';
import { IPaymentMethod } from './paymentMethod';
import { IPaymentMethods } from './paymentMethods';

export const bookingApi = createApi({
    reducerPath: 'bookingApi',
    baseQuery: fetchBaseQuery({
        baseUrl: 'https://api.bikeloop.io/',
        prepareHeaders: async (headers) => {
            const token = await getAccessToken();
            if (token) {
                headers.set('Authorization', `bearer ${token}`);
            }

            return headers;
        },
    }),
    tagTypes: ['PaymentMethods', 'Parking', 'UserInfo'],
    endpoints: (builder) => ({
        getParkings: builder.query<IParking[], void>({
            query: () => {
                return { url: 'parkings', params: { active: true } };
            },
            providesTags: ['Parking'],
        }),
        getParkingHistory: builder.query<IParking[], void>({
            query: () => {
                return { url: 'parkings', params: { active: false } };
            },
            transformResponse: (response) => {
                // Sort history descending
                const parkings = (response ?? []) as IParking[];

                return parkings.sort(compareDatesDesc);
            },
            providesTags: ['Parking'],
        }),
        postParking: builder.mutation<IParking, string>({
            query: (bikeloopId) => ({
                method: 'POST',
                url: 'parkings',
                body: { BikeloopId: bikeloopId },
            }),
            invalidatesTags: ['Parking'],
        }),
        cancelParking: builder.mutation<IParking, string>({
            query: (parkingId) => ({
                method: 'DELETE',
                url: `parkings/${parkingId}`,
            }),
        }),
        postParkingEvent: builder.mutation<IParking, { id: string; eventType: ParkingEventType }>({
            query: ({ id, eventType }) => ({
                method: 'POST',
                body: { type: eventType },
                url: `parkings/${id}/event`,
            }),
            invalidatesTags: ['Parking'],
        }),
        getBikeloops: builder.query<IBikeloop[], void>({
            query: () => ({
                method: 'GET',
                url: `bikeloops`,
            }),
            providesTags: ['Parking'],
        }),
        getBikeloopStatus: builder.query<IBikeloopStatus, string>({
            query: (bikeloopId) => ({
                method: 'GET',
                url: `bikeloops/${bikeloopId}/status`,
            }),
        }),
        getUser: builder.query<IUser, string | 'me'>({
            providesTags: ['UserInfo'],
            query: (userId) => ({
                method: 'GET',
                url: `users/${userId}`,
            }),
        }),
        postUser: builder.mutation<IUser, void>({
            invalidatesTags: ['UserInfo'],
            query: () => ({
                method: 'POST',
                url: `users`,
            }),
        }),
        putUser: builder.mutation<IUser, { userId: string | 'me'; userInfo: IUserRequest }>({
            invalidatesTags: ['UserInfo'],
            query: ({ userId, userInfo }) => ({
                method: 'PUT',
                url: `users/${userId}`,
                body: userInfo,
            }),
        }),
        deleteUser: builder.mutation<IUser, string | 'me'>({
            invalidatesTags: ['UserInfo'],
            query: (userId) => ({
                method: 'DELETE',
                url: `users/${userId}`,
            }),
        }),
        getQueue: builder.query<IQueue, string>({
            query: (parkingId) => ({
                method: 'GET',
                url: `parkings/${parkingId}/queue`,
            }),
        }),
        createPaymentMethodKey: builder.query<IPaymentMethodKey, void>({
            query: () => ({
                method: 'POST',
                url: `users/me/paymentmethod`,
            }),
        }),
        getPaymentMethods: builder.query<IPaymentMethods, void>({
            providesTags: ['PaymentMethods'],
            query: () => ({
                method: 'GET',
                url: `users/me/paymentmethods`,
            }),
        }),
        deletePaymentMethod: builder.mutation<void, IPaymentMethod>({
            invalidatesTags: ['PaymentMethods'],
            query: (paymentMethod) => ({
                method: 'DELETE',
                url: `users/me/paymentmethods/${paymentMethod.paymentMethodId}`,
            }),
        }),
        setPaymentMethodAsDefault: builder.mutation<void, IPaymentMethod>({
            invalidatesTags: ['PaymentMethods'],
            query: (paymentMethod) => ({
                method: 'POST',
                url: `users/me/paymentmethods/default`,
                body: {
                    id: paymentMethod.paymentMethodId,
                },
            }),
        }),
    }),
});

export const {
    useGetParkingsQuery,
    useLazyGetParkingsQuery,
    useGetParkingHistoryQuery,
    useLazyGetParkingHistoryQuery,
    useCancelParkingMutation,
    usePostParkingMutation,
    usePostParkingEventMutation,
    useGetBikeloopsQuery,
    useGetBikeloopStatusQuery,
    useLazyGetBikeloopStatusQuery,
    usePostUserMutation,
    usePutUserMutation,
    useDeleteUserMutation,
    useGetUserQuery,
    useLazyGetUserQuery,
    useGetQueueQuery,
    useLazyGetQueueQuery,
    useCreatePaymentMethodKeyQuery,
    useLazyCreatePaymentMethodKeyQuery,
    useGetPaymentMethodsQuery,
    useLazyGetPaymentMethodsQuery,
    useDeletePaymentMethodMutation,
    useSetPaymentMethodAsDefaultMutation,
} = bookingApi;