33 lines
967 B
TypeScript
33 lines
967 B
TypeScript
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
|
import { getConfigValue } from '@brojs/cli'
|
|
import { BaseResponse, User } from '../model'
|
|
|
|
export const api = createApi({
|
|
reducerPath: 'api',
|
|
baseQuery: fetchBaseQuery({
|
|
baseUrl: getConfigValue('nav2.api'),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
}),
|
|
tagTypes: ['Users'],
|
|
endpoints: (builder) => ({
|
|
users: builder.query<Record<string, User>, void>({
|
|
query: () => ({
|
|
url: '/users',
|
|
}),
|
|
providesTags: ['Users'],
|
|
transformResponse: (response: BaseResponse<User[]>) =>
|
|
response.body.reduce((acc, user) => ({ ...acc, [user.id]: user }), {}),
|
|
}),
|
|
updateRating: builder.mutation<void, { userId: string, rating: number }>({
|
|
query: ({ userId, rating }) => ({
|
|
url: `/user/rate/${userId}`,
|
|
method: 'POST',
|
|
body: { rating },
|
|
}),
|
|
invalidatesTags: ['Users'],
|
|
}),
|
|
}),
|
|
})
|