-
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
10 changed files
with
137 additions
and
33 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
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
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
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
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,8 @@ | ||
import axios from 'axios'; | ||
|
||
export const api = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || '', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); |
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,64 @@ | ||
"use client"; | ||
|
||
import { AxiosError } from "axios"; | ||
import React, { createContext, useContext, useEffect, useState } from "react"; | ||
|
||
import { api } from "../lib/axios"; | ||
|
||
export interface Artist { | ||
uuid: string; | ||
name: string; | ||
email: string; | ||
artistImage: string; | ||
} | ||
|
||
interface UserContextType { | ||
user: Artist | null; | ||
isLoading: boolean; | ||
error: Error | null; | ||
} | ||
|
||
const UserContext = createContext<UserContextType | undefined>(undefined); | ||
|
||
export function UserProvider({ children }: { children: React.ReactNode }) { | ||
const [user, setUser] = useState<Artist | null>(null); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchUser = async () => { | ||
try { | ||
const { data } = await api.get<Artist>("/artist"); | ||
setUser(data); | ||
} catch (err) { | ||
if (err instanceof AxiosError) { | ||
setError( | ||
new Error( | ||
err.response?.data?.message || "Failed to fetch user data" | ||
) | ||
); | ||
} else { | ||
setError(new Error("An unexpected error occurred")); | ||
} | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
fetchUser(); | ||
}, []); | ||
|
||
return ( | ||
<UserContext.Provider value={{ user, isLoading, error }}> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
} | ||
|
||
export function useUser() { | ||
const context = useContext(UserContext); | ||
if (context === undefined) { | ||
throw new Error("useUser must be used within a UserProvider"); | ||
} | ||
return context; | ||
} |