(props)
| 3 | import ProfileContext from './profileContext'; |
| 4 | |
| 5 | const ProfileState = (props) => { |
| 6 | const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || 'https://bitbox-uxbo.onrender.com'; |
| 7 | const profileInitial = []; |
| 8 | |
| 9 | const [userProfile, setUserProfile] = useState(profileInitial); |
| 10 | |
| 11 | const createUserProfile = async (name, college, phone, address) => { |
| 12 | try { |
| 13 | const response = await fetch(`${VITE_SERVER_PORT}/api/profile/createprofile`, { |
| 14 | method: 'POST', |
| 15 | headers: { |
| 16 | 'Content-Type': 'application/json', |
| 17 | 'auth-token': localStorage.getItem('token'), |
| 18 | }, |
| 19 | body: JSON.stringify({ name, college, phone, address }), |
| 20 | }); |
| 21 | const profile = await response.json(); |
| 22 | setUserProfile([profile, ...userProfile]); |
| 23 | } catch (error) { |
| 24 | console.error('Error creating profile:', error); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | const getUserProfile = async () => { |
| 29 | try { |
| 30 | const response = await fetch(`${VITE_SERVER_PORT}/api/profile/fetchprofile`, { |
| 31 | method: 'GET', |
| 32 | headers: { |
| 33 | 'Content-Type': 'application/json', |
| 34 | 'auth-token': localStorage.getItem('token'), |
| 35 | }, |
| 36 | }); |
| 37 | const json = await response.json(); |
| 38 | setUserProfile(json); |
| 39 | } catch (error) { |
| 40 | console.error('Error fetching profile:', error); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | const updateUserProfile = async (name, college, phone, address) => { |
| 45 | try { |
| 46 | const response = await fetch(`${VITE_SERVER_PORT}/api/profile/updateprofile`, { |
| 47 | method: 'PUT', |
| 48 | headers: { |
| 49 | 'Content-Type': 'application/json', |
| 50 | 'auth-token': localStorage.getItem('token'), |
| 51 | }, |
| 52 | body: JSON.stringify({ name, college, phone, address }), |
| 53 | }); |
| 54 | const profile = await response.json(); |
| 55 | setUserProfile([profile, ...userProfile]); |
| 56 | } catch (error) { |
| 57 | console.error('Error creating profile:', error); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | return ( |
| 62 | <ProfileContext.Provider value={{ userProfile, createUserProfile, getUserProfile, updateUserProfile }}> |
nothing calls this directly
no outgoing calls
no test coverage detected