diff --git a/backend/users/serializers.py b/backend/users/serializers.py index 531d912..962d789 100644 --- a/backend/users/serializers.py +++ b/backend/users/serializers.py @@ -31,6 +31,19 @@ class UpdateProfileSerializer(serializers.ModelSerializer): """ Serializer for updating user profile. """ + profile_pic = serializers.ImageField(required=False) + first_name = serializers.CharField(max_length=255, required=False) + about = serializers.CharField(required=False) + class Meta: model = CustomUser - fields = ('profile_pic', 'first_name', 'about') \ No newline at end of file + fields = ('profile_pic', 'first_name', 'about') + + def update(self, instance, validated_data): + """ + Update an existing user's profile. + """ + for attr, value in validated_data.items(): + setattr(instance, attr, value) + instance.save() + return instance \ No newline at end of file diff --git a/backend/users/views.py b/backend/users/views.py index 0cebbae..af201b7 100644 --- a/backend/users/views.py +++ b/backend/users/views.py @@ -8,7 +8,7 @@ from rest_framework.views import APIView from rest_framework.parsers import MultiPartParser from users.serializers import CustomUserSerializer, UpdateProfileSerializer - +from users.models import CustomUser class CustomUserCreate(APIView): """ @@ -49,8 +49,12 @@ class CustomUserProfileUpdate(APIView): return Response(data) def post(self, request): - serializer = UpdateProfileSerializer(data=request.data) + if not CustomUser.objects.filter(email=request.user.email).exists(): + return Response ({ + 'error': 'User does not exist' + }, status=status.HTTP_404_NOT_FOUND) + serializer = UpdateProfileSerializer(request.user, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) - return Response(serializer.errors, status=400) \ No newline at end of file + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index c18349d..e9bb13b 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -2,11 +2,11 @@ import './App.css'; import { BrowserRouter, Route, Routes, Link } from 'react-router-dom'; import TestAuth from './components/testAuth'; -import IconSideNav from './components/IconSideNav'; import LoginPage from './components/authentication/LoginPage'; import SignUpPage from './components/authentication/SignUpPage'; import NavBar from './components/Nav/Navbar'; import Home from './components/Home'; +import ProfileUpdate from './components/ProfileUpdatePage' const App = () => { return ( @@ -18,11 +18,9 @@ const App = () => { }/> }/> }/> + }/> - {/*
- -
*/} ); } diff --git a/frontend/src/api/UserProfileApi.jsx b/frontend/src/api/UserProfileApi.jsx new file mode 100644 index 0000000..ca80fd1 --- /dev/null +++ b/frontend/src/api/UserProfileApi.jsx @@ -0,0 +1,21 @@ +import axios from 'axios'; + +const ApiUpdateUserProfile = async (formData) => { + try { + const response = await axios.post('http://127.0.1:8000/api/user/update/', formData, { + headers: { + 'Authorization': "Bearer " + localStorage.getItem('access_token'), + 'Content-Type': 'multipart/form-data', + }, + }); + + console.log(response.data); + + return response.data; + } catch (error) { + console.error('Error updating user profile:', error); + throw error; + } +}; + +export { ApiUpdateUserProfile }; diff --git a/frontend/src/components/ProfileUpdatePage.jsx b/frontend/src/components/ProfileUpdatePage.jsx new file mode 100644 index 0000000..06a0213 --- /dev/null +++ b/frontend/src/components/ProfileUpdatePage.jsx @@ -0,0 +1,107 @@ +import React, { useState, useRef } from 'react'; +import { ApiUpdateUserProfile } from '../api/UserProfileApi'; + +function ProfileUpdate() { + const [file, setFile] = useState(null); + const [username, setUsername] = useState(''); + const [fullName, setFullName] = useState(''); + const [about, setAbout] = useState(''); + const defaultImage = 'https://i1.sndcdn.com/artworks-cTz48e4f1lxn5Ozp-L3hopw-t500x500.jpg'; + const fileInputRef = useRef(null); + + const handleImageUpload = () => { + if (fileInputRef.current) { + fileInputRef.current.click(); + } + }; + + const handleFileChange = (e) => { + const selectedFile = e.target.files[0]; + if (selectedFile) { + setFile(selectedFile); + } + }; + + const handleSave = () => { + const formData = new FormData(); + formData.append('profile_pic', file); + formData.append('first_name', username); + formData.append('about', about); + + ApiUpdateUserProfile(formData); + }; + + return ( +
+ {/* Profile Image */} +
+ +
+ {file ? ( + Profile + ) : ( + <> + Default + + + + )} +
+
+ + {/* Username Field */} +
+ + setUsername(e.target.value)} + /> +
+ + {/* Full Name Field */} +
+ + setFullName(e.target.value)} + /> +
+ + {/* About Field */} +
+ +