import { api } from './api'; import { account, user } from '../urls'; import { buildClientRequestUrl } from '../../utils'; import { AccountChangeEmailFormType, AccountChangePasswordFormType, AccountOrderCancellation, AccountOrderCancellationReason, ContactFormType, LoyaltyBalanceItem, Order, Quotations } from '../../types'; interface GetOrdersResponse { count: number; results: Order[]; } interface GetOrdersParams { limit?: number; page?: number; createdDate?: string; endDate?: string; shipping_option_slug?: string; shipping_option_operator?: string; filterType?: string; filterValue?: string; currency?: string; } export interface GetQuotationsResponse { count: number; results: Quotations[]; next: string | null; previous: string | null; } export interface GetQuotationsParams { page?: number; status?: string; limit?: number; } export type PasswordResetType = { password: string; repeatPassword: string; slug: string; }; export type AccountProfileFormFilteredType = { first_name: string; last_name: string; phone: string; date_of_birth: string; gender: string; sms_allowed: boolean; email_allowed: boolean; }; export type ContactFormSubject = [ { id: string; is_order_needed: boolean; text: string; } ]; interface LoyaltyTransactions { count: number; results: { amount: string; created_date: string; transaction: { created_date: string; order: string; }; }[]; } interface PasswordResetValidateResponse { validlink: boolean; } const accountApi = api.injectEndpoints({ endpoints: (builder) => ({ updatePassword: builder.mutation({ query: (body) => ({ url: buildClientRequestUrl(account.updatePassword, { contentType: 'application/json' }), method: 'POST', body }) }), updateEmail: builder.mutation({ query: (body) => ({ url: buildClientRequestUrl(account.updateEmail, { contentType: 'application/json' }), method: 'POST', body }) }), updateProfile: builder.mutation({ query: (body) => ({ url: buildClientRequestUrl(account.updateProfile, { accept: 'application/json', contentType: 'application/json' }), method: 'PATCH', body }), invalidatesTags: ['Profile'] }), getProfileInfo: builder.query({ query: () => buildClientRequestUrl(user.profiles), providesTags: ['Profile'] }), getContactSubjects: builder.query({ query: () => buildClientRequestUrl(account.getContactSubjects) }), getOrder: builder.query({ query: (id) => buildClientRequestUrl(account.orderId(id)) }), getOrders: builder.query({ query: ({ page, limit, createdDate, endDate, shipping_option_operator, shipping_option_slug, filterType, filterValue, currency } = {}) => buildClientRequestUrl( account.getOrders({ page, limit, createdDate, endDate, shipping_option_operator, shipping_option_slug, filterType, filterValue, currency }) ) }), getOldOrders: builder.query({ query: ({ page, limit } = {}) => buildClientRequestUrl(account.getOldOrders({ page, limit })) }), getQuotations: builder.query({ query: ({ page, status, limit }) => buildClientRequestUrl(account.getQuotations(page, status, limit)) }), sendContact: builder.mutation({ query: (body) => { return { url: buildClientRequestUrl(account.sendContact, { useFormData: true }), method: 'POST', body }; } }), cancelOrder: builder.mutation({ query: ({ id, ...body }) => ({ url: buildClientRequestUrl(account.cancelOrder(id), { contentType: 'application/json' }), method: 'POST', body }) }), bulkCancellation: builder.mutation({ query: (body) => ({ url: buildClientRequestUrl(account.bulkCancellationRequest, { contentType: 'application/json' }), method: 'POST', body }) }), getCancellationReasons: builder.query( { query: () => buildClientRequestUrl(account.cancellationReasons) } ), passwordReset: builder.mutation({ query: ({ slug, password, repeatPassword }) => ({ url: buildClientRequestUrl(account.passwordReset(slug), { useFormData: true }), method: 'POST', body: { new_password1: password, new_password2: repeatPassword } }) }), getBasketOffers: builder.query({ query: () => buildClientRequestUrl('/account/basket-offers') }), getFutureBasketOffers: builder.query({ query: () => buildClientRequestUrl('/account/future-basket-offers') }), getExpiredBasketOffers: builder.query({ query: () => buildClientRequestUrl('/account/expired-basket-offers') }), getDiscountItems: builder.query({ query: () => buildClientRequestUrl('/account/discount-items') }), anonymize: builder.mutation({ query: () => ({ url: buildClientRequestUrl(account.anonymize), method: 'PATCH' }) }), getLoyaltyBalance: builder.query< { balance: number; balances?: LoyaltyBalanceItem[] }, void >({ query: () => buildClientRequestUrl(account.loyaltyBalance) }), getLoyaltyTransactions: builder.query({ query: () => buildClientRequestUrl(account.loyaltyTransactions) }), getValidatePasswordResetToken: builder.query< PasswordResetValidateResponse, string >({ query: (slug) => buildClientRequestUrl(account.passwordReset(slug)) }) }), overrideExisting: true }); export const { useGetContactSubjectsQuery, useGetOrderQuery, useGetOrdersQuery, useGetOldOrdersQuery, useGetQuotationsQuery, useSendContactMutation, useUpdateEmailMutation, useUpdatePasswordMutation, useUpdateProfileMutation, useGetProfileInfoQuery, useCancelOrderMutation, useBulkCancellationMutation, useGetCancellationReasonsQuery, useGetBasketOffersQuery, useGetFutureBasketOffersQuery, useGetExpiredBasketOffersQuery, useGetDiscountItemsQuery, usePasswordResetMutation, useAnonymizeMutation, useGetLoyaltyBalanceQuery, useGetLoyaltyTransactionsQuery, useGetValidatePasswordResetTokenQuery } = accountApi;