Annen applikasjon

mail@pastecode.io avatar
unknown
javascript
2 years ago
5.8 kB
1
Indexable
Never
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { IParking } from './parking';
import { IBikeloop } from './bikeloop';
import { ParkingEventType } from './parkingEventType';
import { IBikeloopStatus } from './bikeloopStatus';
import { IUser } from './user';
import { IUserRequest } from './userRequest';
import { IQueue } from './queue';
import { IPaymentMethod } from './paymentMethod';
import { IPaymentMethods } from './paymentMethods';
import { auth } from '../auth';
import { IParkingStatus } from './parkingStatus';
import { IParkingPicture } from './parkingPicture';
import { IDailyStats } from './dailyStats';

export const bookingApi = createApi({
    reducerPath: 'bookingApi',
    baseQuery: fetchBaseQuery({
        baseUrl: 'https://api.bikeloop.io/',
        prepareHeaders: async (headers) => {
            const access_token = await auth.getAccessTokenSilently()();
            if (access_token) {
                headers.set('Authorization', `Bearer ${access_token}`);
            }
            return headers;
        },
    }),
    tagTypes: ['PaymentMethods', 'Parking'],
    endpoints: (builder) => ({
        getParkings: builder.query<
            IParking[],
            {
                bikeloopId?: string;
                active?: boolean;
                limit?: number;
                search?: string;
                userId?: string;
            }
        >({
            query: (query) => {
                return { url: 'parkingsdetails', params: query };
            },
            providesTags: ['Parking'],
        }),
        getParking: builder.query<IParkingStatus, string>({
            query: (parkingId) => {
                return { url: `parkings/${parkingId}/statuses` };
            },
            providesTags: ['Parking'],
        }),
        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'],
        }),
        getBikeloop: builder.query<IBikeloop, string | undefined>({
            query: (bikeloopId) => ({
                method: 'GET',
                url: `bikeloops/${bikeloopId}`,
            }),
        }),
        getBikeloopStatus: builder.query<IBikeloopStatus, string | undefined>({
            query: (bikeloopId) => ({
                method: 'GET',
                url: `bikeloops/${bikeloopId}/status`,
            }),
        }),
        getUser: builder.query<IUser, string | 'me'>({
            query: (userId) => ({
                method: 'GET',
                url: `users/${userId}`,
            }),
        }),
        getUsers: builder.query<IUser[], string>({
            query: (query) => ({
                method: 'GET',
                url: `users?${new URLSearchParams({ search: query })}`,
            }),
        }),
        putUser: builder.mutation<
            IUser,
            { userId: string | 'me'; userInfo: IUserRequest }
        >({
            query: ({ userId, userInfo }) => ({
                method: 'PUT',
                url: `users/${userId}`,
                body: userInfo,
            }),
        }),
        getQueue: builder.query<IQueue, string>({
            query: (parkingId) => ({
                method: 'GET',
                url: `parkings/${parkingId}/queue`,
            }),
        }),
        getParkingPicture: builder.query<IParkingPicture, string | undefined>({
            query: (parkingId) => ({
                method: 'GET',
                url: `parkings/${parkingId}/picture`,
            }),
        }),
        getDailyStats: builder.query<IDailyStats[], {fromDate?: string, toDate?: string}>({
            query: (query) => ({
                method: 'GET',
                url: `stats/timeseries?fromDate=${query.fromDate}&toDate=${query.toDate}`,
            }),
        }),
        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,
    useGetParkingQuery,
    useLazyGetParkingsQuery,
    usePostParkingEventMutation,
    useGetBikeloopsQuery,
    useGetBikeloopQuery,
    useGetBikeloopStatusQuery,
    useLazyGetBikeloopStatusQuery,
    usePutUserMutation,
    useGetUserQuery,
    useGetUsersQuery,
    useLazyGetUserQuery,
    useGetQueueQuery,
    useLazyGetQueueQuery,
    useGetParkingPictureQuery,
    useGetDailyStatsQuery,
    useGetPaymentMethodsQuery,
    useLazyGetPaymentMethodsQuery,
    useDeletePaymentMethodMutation,
    useSetPaymentMethodAsDefaultMutation,
} = bookingApi;