Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #119

Merged
merged 3 commits into from
Feb 7, 2024
Merged

fix #119

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 48 additions & 19 deletions frontend/src/pages/Profil.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import axios from "axios";

import Recipe from "./Recipe";
import "./style/RecipeList.scss";
import addimage from "../assets/addimage.svg";

import "./style/Profil.scss";

function Profil() {
const [id, setId] = useState();
const [editing, setEditing] = useState(false);
const [recipeBy, setRecipeBy] = useState([]);
const [userData, setUserData] = useState({
avatar: null,
username: null,
firstname: null,
lastname: null,
birthdate: null,
description: null,
});
const [selectedFile, setSelectedFile] = useState(null);
const [selectedFileUrl, setselectedFileUrl] = useState(null);
const [fav, setFav] = useState([]);

const handleEditClick = () => {
Expand Down Expand Up @@ -73,23 +76,27 @@ function Profil() {
}
}, [id]);

const handleSaveClick = () => {
axios
.put(
`${import.meta.env.VITE_BACKEND_URL}/api/users/${id}`,
{
username: userData.username,
description: userData.description,
firstname: userData.firstname,
lastname: userData.lastname,
birthdate: userData.birthdate,
},
{ withCredentials: true }
)
.then(() => {
setEditing(false);
});
setEditing(false);
const handleSaveClick = async () => {
try {
const formData = new FormData();
formData.append("avatar", selectedFile);
formData.append("name", userData.recipeName);
formData.append("title", userData.recipeDesc);
formData.append("prep_time", userData.prepTime);
formData.append("nb_people", userData.nbPeople);
formData.append("difficulty", userData.difficulty);

await axios
.put(`${import.meta.env.VITE_BACKEND_URL}/api/users/${id}`, formData, {
withCredentials: true,
})
.then(() => {
setEditing(false);
});
setEditing(false);
} catch (err) {
console.error(err);
}
};

const handleChange = (e) => {
Expand All @@ -99,13 +106,35 @@ function Profil() {
[name]: value,
}));
};
const handleFileInput = (e) => {
setSelectedFile(e.target.files[0]);
};
const handleImageUpload = (e) => {
if (e.target.files && e.target.files[0]) {
const imgFile = e.target.files[0];
setselectedFileUrl(URL.createObjectURL(imgFile));
}
};

const combineImagehandler = (e) => {
handleFileInput(e);
handleImageUpload(e);
};
const imageUrl = `${import.meta.env.VITE_BACKEND_URL}/${userData.avatar}`;

return (
<div className="body-content" style={{ flexDirection: "column" }}>
<div className="Profil_container">
<div className="first">
<div className="left-bloc">
<img className="No-photo" src={addimage} alt="No Photos" />
{editing ? (
<div>
<img src={selectedFileUrl} alt="Preview" />
<input type="file" onChange={combineImagehandler} />
</div>
) : (
<img src={imageUrl} alt="vide" /> || "photo de profil"
)}
</div>
<div className="right-bloc">
<label className="stats-label">
Expand Down
Loading