156 lines
3.6 KiB
TypeScript
156 lines
3.6 KiB
TypeScript
import type { User, Node, Retailer, Maker, Product, Dispatch, CreateDispatch } from '../astroTypes';
|
|
import { useQuery, useMutation, useQueryClient, queryOptions } from "@tanstack/react-query";
|
|
import axios from "axios";
|
|
import { hasAuthCookie } from './authUtils';
|
|
import { queryClient } from '@/components/App';
|
|
|
|
export const API_URL = "http://localhost:3001"
|
|
|
|
const headers = {
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
const getMakers = async () => {
|
|
const url = `${API_URL}/api/makers`
|
|
const response = await axios.get(url);
|
|
|
|
const makers: Maker[] = response.data.docs;
|
|
return makers;
|
|
|
|
}
|
|
|
|
export const useGetMakers = () => {
|
|
return useQuery<Maker[]>({
|
|
queryFn: () => getMakers(),
|
|
queryKey: ['makers'],
|
|
enabled: true
|
|
})
|
|
}
|
|
|
|
const getRetailers = async () => {
|
|
const url = `${API_URL}/api/retailers`
|
|
const response = await axios.get(url);
|
|
|
|
const retailers: Retailer[] = response.data.docs;
|
|
return retailers;
|
|
|
|
}
|
|
|
|
export const useGetRetailers = () => {
|
|
return useQuery<Retailer[]>({
|
|
queryFn: () => getRetailers(),
|
|
queryKey: ['retailers'],
|
|
enabled: true
|
|
})
|
|
}
|
|
|
|
const getUser = async (userId: string) => {
|
|
const url = `${API_URL}/api/users/${userId}`
|
|
const response = await axios.get(url);
|
|
|
|
const user: User = response.data.docs;
|
|
return user;
|
|
|
|
}
|
|
|
|
export const useGetUser = (userId: string) => {
|
|
return useQuery<User>({
|
|
queryFn: () => getUser(userId),
|
|
queryKey: ['user'],
|
|
enabled: true//If login cookie
|
|
})
|
|
}
|
|
|
|
const getMyself = async (authToken: string) => {
|
|
const url = `${API_URL}/api/users/me`
|
|
|
|
const authHeaders = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": `JWT ${authToken}`,
|
|
}
|
|
|
|
|
|
const response = await axios.get(`${API_URL}/api/users/me`, {
|
|
withCredentials: true,
|
|
headers: authHeaders
|
|
});
|
|
|
|
const user: User = response.data.user
|
|
return user;
|
|
|
|
}
|
|
|
|
export const useGetMyself = (authToken: string) => {
|
|
return useQuery<User>({
|
|
queryFn: () => getMyself(authToken),
|
|
queryKey: ['myself'],
|
|
enabled: authToken !== ''
|
|
})
|
|
}
|
|
|
|
const getDispatches = async () => {
|
|
const url = `${API_URL}/api/dispatches`
|
|
const response = await axios.get(url);
|
|
|
|
const dispatches: Dispatch[] = response.data.docs;
|
|
return dispatches;
|
|
|
|
}
|
|
|
|
export const useGetDispatches = () => {
|
|
return useQuery<Dispatch[]>({
|
|
queryFn: () => getDispatches(),
|
|
queryKey: ['dispatches'],
|
|
enabled: true
|
|
})
|
|
}
|
|
|
|
const createDispatch = async (dispatch: CreateDispatch) => {
|
|
const url = `${API_URL}/api/dispatches`;
|
|
return await axios.post(url, dispatch);
|
|
};
|
|
|
|
export const useCreateDispatch = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (dispatch: CreateDispatch) => createDispatch(dispatch),
|
|
onSuccess: () => {
|
|
void queryClient.invalidateQueries({ queryKey: ['dispatches'] });
|
|
},
|
|
mutationKey: ["createDispatch"]
|
|
})
|
|
};
|
|
|
|
const getRetailersByAdminId = async (user: User | undefined) => {
|
|
if(user === undefined) {
|
|
console.error("getMyRetailers error: user undefined")
|
|
return []
|
|
}
|
|
const adminId = user.id
|
|
const url = `${API_URL}/api/retailers`
|
|
const response = await axios.get(url);
|
|
|
|
const retailers: Retailer[] = response.data.docs;
|
|
|
|
let myRetailers: Retailer[] = []
|
|
for (let retailer of retailers) {
|
|
if(retailer.admins) {
|
|
for (let admin of retailer.admins) {
|
|
if(admin.id === adminId) {
|
|
myRetailers.push(retailer)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return myRetailers;
|
|
|
|
}
|
|
|
|
export const useGetMyRetailers = (user: User | undefined) => {
|
|
return useQuery<Retailer[]>({
|
|
queryFn: () => getRetailersByAdminId(user),
|
|
queryKey: ['myRetailers'],
|
|
enabled: (user !== undefined)
|
|
})
|
|
} |