116 lines
2.7 KiB
TypeScript
116 lines
2.7 KiB
TypeScript
import type { User, Node, Retailer, Maker, Product, Dispatch } from '../astroTypes';
|
|
import { useQuery, useMutation, useQueryClient, queryOptions } from "@tanstack/react-query";
|
|
import axios from "axios";
|
|
import { hasAuthCookie } from './authUtils';
|
|
|
|
const API_URL = "http://localhost:3001"
|
|
|
|
const nonAuthHeaders = {
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
const getMakers = async () => {
|
|
const url = `${API_URL}/api/makers`
|
|
console.log("Fetching url:", url)
|
|
const response = await axios.get(url);
|
|
|
|
const makers: Maker[] = response.data.docs;
|
|
console.log(`Fetch result from ${url}`, makers)
|
|
return makers;
|
|
|
|
}
|
|
|
|
export const useGetMakers = () => {
|
|
return useQuery<Maker[]>({
|
|
queryFn: () => getMakers(),
|
|
queryKey: ['makers'],
|
|
enabled: true
|
|
})
|
|
}
|
|
|
|
const getRetailers = async () => {
|
|
const url = `${API_URL}/api/retailers`
|
|
console.log("Fetching url:", url)
|
|
const response = await axios.get(url);
|
|
|
|
const retailers: Retailer[] = response.data.docs;
|
|
console.log(`Fetch result from ${url}`, retailers)
|
|
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}`
|
|
console.log("Fetching url:", url)
|
|
const response = await axios.get(url);
|
|
|
|
const user: User = response.data.docs;
|
|
console.log(`Fetch result from ${url}`, user)
|
|
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`
|
|
console.log("Fetching url:", url)
|
|
|
|
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
|
|
console.log(`Fetch result from ${url}`, 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`
|
|
console.log("Fetching url:", url)
|
|
const response = await axios.get(url);
|
|
|
|
const dispatches: Dispatch[] = response.data.docs;
|
|
|
|
console.log(`Fetch result from ${url}`, dispatches)
|
|
return dispatches;
|
|
|
|
}
|
|
|
|
export const useGetDispatches = () => {
|
|
return useQuery<Dispatch[]>({
|
|
queryFn: () => getDispatches(),
|
|
queryKey: ['dispatches'],
|
|
enabled: true
|
|
})
|
|
} |