103 lines
2.5 KiB
JavaScript
103 lines
2.5 KiB
JavaScript
/* global FormData */
|
|
|
|
import { API, authHeader } from './apiSlice';
|
|
|
|
export const stockApi = API.injectEndpoints({
|
|
endpoints: (build) => ({
|
|
getStocks: build.query({
|
|
query: ({ search = '', page = '' }) => {
|
|
let queryString = '';
|
|
if (search || page) queryString += '?'
|
|
if (search) queryString += `search=${search}`
|
|
if (search && page) queryString += `&`
|
|
if (page) queryString += `page=${page}`
|
|
return {
|
|
url: `stock/list${queryString}`,
|
|
//headers: authHeader(),
|
|
};
|
|
},
|
|
refetchOnMountOrArgChange: true,
|
|
keepUnusedDataFor: 0,
|
|
}),
|
|
getStock: build.query({
|
|
query: ({ stockId }) => {
|
|
console.log(stockId)
|
|
return {
|
|
url: `/stock/${stockId}`,
|
|
//headers: authHeader(),
|
|
};
|
|
},
|
|
}),
|
|
createStock: build.mutation({
|
|
query: ({ data }) => {
|
|
return {
|
|
url: `/stock/create`,
|
|
method: 'POST',
|
|
headers: authHeader(),
|
|
body: JSON.stringify(data),
|
|
};
|
|
},
|
|
}),
|
|
updateStock: build.mutation({
|
|
query: ({ stockId, data }) => {
|
|
return {
|
|
url: `/stock/${stockId}`,
|
|
method: 'PUT',
|
|
headers: authHeader(),
|
|
body: JSON.stringify(data),
|
|
};
|
|
},
|
|
}),
|
|
uploadStockPicture: build.mutation({
|
|
query: ({ id, file }) => {
|
|
const formData = new FormData();
|
|
formData.append('picture', file);
|
|
return {
|
|
url: `/stock/picture/${id}`,
|
|
method: 'POST',
|
|
headers: authHeader(),
|
|
body: formData,
|
|
};
|
|
},
|
|
}),
|
|
deleteStock: build.mutation({
|
|
query: ({ stockId }) => {
|
|
return {
|
|
url: `/stock/${stockId}`,
|
|
method: 'DELETE',
|
|
headers: authHeader(),
|
|
};
|
|
},
|
|
}),
|
|
addSpecialist: build.mutation({
|
|
query: ({ stockId, specialistId }) => {
|
|
return {
|
|
url: `/stock/${stockId}/specialist/${specialistId}`,
|
|
method: 'PUT',
|
|
headers: authHeader(),
|
|
};
|
|
},
|
|
}),
|
|
removeSpecialist: build.mutation({
|
|
query: ({ stockId, specialistId }) => {
|
|
return {
|
|
url: `/stock/${stockId}/specialist/${specialistId}`,
|
|
method: 'DELETE',
|
|
headers: authHeader(),
|
|
};
|
|
},
|
|
}),
|
|
}),
|
|
});
|
|
|
|
export const {
|
|
useGetStockQuery,
|
|
useGetStocksQuery,
|
|
useCreateStockMutation,
|
|
useUpdateStockMutation,
|
|
useUploadStockPictureMutation,
|
|
useDeleteStockMutation,
|
|
useAddSpecialistMutation,
|
|
useRemoveSpecialistMutation,
|
|
} = stockApi;
|