-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,53 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useRef, useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
import getUser from "../services/getUsers"; | ||
|
||
const useUser = () => { | ||
const [user, setUser] = useState(""); | ||
const [error, setError] = useState(""); | ||
const [loadUser, setLoadUser] = useState(false); | ||
const previousUser = useRef(user); | ||
|
||
// Validate user | ||
useEffect(() => { | ||
const validateUser = (user: string) => { | ||
if (user === "") { | ||
setError("Please enter a username"); | ||
return; | ||
toast.error("Please enter a username"); | ||
return false; | ||
} | ||
|
||
if (user.match(/[^a-zA-Z0-9 ]/)) { | ||
setError("Please enter only letters and numbers"); | ||
return; | ||
toast.error("Please enter only letters and numbers"); | ||
return false; | ||
} | ||
|
||
if (user.length < 3) { | ||
setError("Please enter at least 3 characters"); | ||
return; | ||
toast.error("Please enter at least 3 characters"); | ||
return false; | ||
} | ||
|
||
setError(""); | ||
}, [user]); | ||
return true; | ||
}; | ||
|
||
return { user, setUser, error, setError }; | ||
const getUserData = async (user: string): Promise<boolean> => { | ||
// Get repos from services | ||
try { | ||
setLoadUser(true); | ||
previousUser.current = user; | ||
const response = await getUser({ user }); | ||
|
||
if (!response.name) { | ||
toast.error("No user found"); | ||
return false; | ||
} | ||
return true; | ||
} catch (error: any) { | ||
toast.error(error.message); | ||
return false; | ||
} finally { | ||
setLoadUser(false); | ||
} | ||
}; | ||
|
||
return { user, setUser, validateUser, getUserData, loadUser }; | ||
}; | ||
|
||
export default useUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { toast } from "react-hot-toast"; | ||
import { serverUrl, fetchMode, userUrl, headers } from "../utils/constants"; | ||
|
||
interface IParams { | ||
user: string; | ||
} | ||
|
||
interface IUser { | ||
name: string; | ||
avatar_url: string; | ||
html_url: string; | ||
} | ||
|
||
/** | ||
* Function that fetch repositories data from server | ||
* @param user github username | ||
* @returns an array with all repositories of a user | ||
*/ | ||
export default async function getUser(params: IParams): Promise<IUser> { | ||
let userData: IUser = { name: "", avatar_url: "", html_url: "" }; | ||
try { | ||
const { user } = params; | ||
|
||
if (fetchMode === "client") { | ||
const rawData = await fetch(`${userUrl}/${user}`, headers); | ||
const data = await rawData.json(); | ||
userData = { | ||
name: data.name, | ||
avatar_url: data.avatar_url, | ||
html_url: data.html_url, | ||
}; | ||
} else { | ||
const response = await fetch(`${serverUrl}/api/users`, { | ||
method: "POST", | ||
body: JSON.stringify({ user: user }), | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
userData = await response.json(); | ||
} | ||
|
||
return userData; | ||
} catch (error: any) { | ||
toast.error("No user found"); | ||
return userData; | ||
} | ||
} |
bff5efb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
github-history – ./
github-history-wrujel.vercel.app
github-history-git-main-wrujel.vercel.app
github-history.vercel.app