@peable/services
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -8,8 +8,9 @@ interface User { | ||
} | ||
declare const useGetUsersByIds: (ids: string[], fields?: (keyof User)[]) => { | ||
users: User[] | null; | ||
declare const useGetUserById: (fields?: (keyof User)[]) => { | ||
user: User | null; | ||
status: "loading" | "loaded" | "error" | "idle"; | ||
error: string | null; | ||
getUser: (id: string) => Promise<void>; | ||
}; | ||
export default useGetUsersByIds; | ||
export default useGetUserById; | ||
//# sourceMappingURL=getUserById.d.ts.map |
@@ -1,39 +0,29 @@ | ||
import { useState, useEffect } from "react"; | ||
import { useState } from "react"; | ||
import axios from "axios"; | ||
const useGetUsersByIds = (ids, fields) => { | ||
const [users, setUsers] = useState(null); | ||
const useGetUserById = (fields) => { | ||
const [user, setUser] = useState(null); | ||
const [status, setStatus] = useState("idle"); | ||
const [error, setError] = useState(null); | ||
useEffect(() => { | ||
const getUsers = async () => { | ||
setStatus("loading"); | ||
try { | ||
const responses = await Promise.all(ids.map((id) => axios.get(`http://localhost:3001/api/users/${id}`, { | ||
withCredentials: true, | ||
}))); | ||
const users = responses.map((response) => { | ||
const user = response.data; | ||
return fields | ||
? fields.reduce((obj, key) => ({ ...obj, [key]: user[key] }), {}) | ||
: user; | ||
}); | ||
setUsers(users); | ||
setStatus("loaded"); | ||
const getUser = async (id) => { | ||
setStatus("loading"); | ||
try { | ||
const response = await axios.get(`http://localhost:3001/api/users/${id}`); | ||
const user = fields | ||
? fields.reduce((obj, key) => ({ ...obj, [key]: response.data[key] }), {}) | ||
: response.data; | ||
setUser(user); | ||
setStatus("loaded"); | ||
} | ||
catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
setError(error.message); | ||
} | ||
catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
setError(error.message); | ||
} | ||
else { | ||
setError("An unknown error occurred while loading the user data."); | ||
} | ||
setStatus("error"); | ||
else { | ||
setError("An unknown error occurred while loading the user data."); | ||
} | ||
}; | ||
if (ids.length > 0) { | ||
getUsers(); | ||
setStatus("error"); | ||
} | ||
}, [ids, fields]); | ||
return { users, status, error }; | ||
}; | ||
return { user, status, error, getUser }; | ||
}; | ||
export default useGetUsersByIds; | ||
export default useGetUserById; |
{ | ||
"name": "@peable/services", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5498
105