Skip to content

Commit

Permalink
Profil updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Palutenya committed Feb 7, 2024
1 parent bb66bd9 commit a82fb36
Show file tree
Hide file tree
Showing 27 changed files with 54 additions and 44 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions backend/src/controllers/authControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ const verifyToken = async (req, res, next) => {
} else {
const decoded = jwt.verify(token, process.env.APP_SECRET);
const user = await tables.user.read(decoded.id);
console.info(decoded);
if (user)
res.status(200).json({
success: "User is valid",
is_loggin: true,
is_admin: decoded.is_admin,
description: user.description,
id: user.id,
});
else
res
Expand Down
21 changes: 14 additions & 7 deletions backend/src/models/RecipeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class RecipeManager extends AbstractManager {
async create(recipe, image, userId) {
// Execute the SQL INSERT query to add a new user to the "recipe" table
const [result] = await this.database.query(
`insert into ${this.table} (user_id, name, title, prep_time, nb_people, difficulty, image, tag1, tag2, tag3,total_kcal) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)`,
`insert into ${this.table} (user_id, name, title, prep_time, nb_people, difficulty, image, tag1, tag2, tag3,
total_kcal)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
userId,
recipe.name,
Expand All @@ -29,27 +31,32 @@ class RecipeManager extends AbstractManager {
}

async readAll() {
const [rows] = await this.database.query(`select * from ${this.table}`);
const [rows] = await this.database.query(`select *
from ${this.table}`);

return rows;
}

async read(id) {
const [rows] = await this.database.query(
`SELECT r.*, u.username from recipe AS r
JOIN user AS u ON u.id = r.user_id
WHERE r.id=?`,
`SELECT r.*, u.username
from recipe AS r
JOIN user AS u ON u.id = r.user_id
WHERE r.id = ?`,
[id]
);
return rows[0];
}

async readByUser(id) {
const [rows] = await this.database.query(
`SELECT r.name, r.title, r.image FROM recipe AS r JOIN user AS u ON u.id = r.user_id WHERE u.id = ?`,
`SELECT r.name, r.title, r.image, u.id
FROM recipe AS r
JOIN user AS u ON u.id = r.user_id
WHERE u.id = ?`,
[id]
);
return rows[0];
return rows;
}
}

Expand Down
75 changes: 38 additions & 37 deletions frontend/src/pages/Profil.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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({
Expand All @@ -19,26 +20,49 @@ function Profil() {
setEditing(true);
};

const handleKeepClick = () => {
const params = { id: "value" };
useEffect(() => {
axios
.get(`${import.meta.env.VITE_BACKEND_URL}/api/users/${params.id}`, {
.get(`${import.meta.env.VITE_BACKEND_URL}/api/verify-token`, {
withCredentials: true,
})
.then((res) => {
setUserData(res.data);
setEditing(false);
})
.catch((error) => {
console.error(error);
setId(res.data.id);
axios
.get(`${import.meta.env.VITE_BACKEND_URL}/api/users/${res.data.id}`, {
withCredentials: true,
})
.then((response) => {
setUserData(response.data);
setEditing(false);
})
.catch((error) => {
console.error(error);
});
});
};
}, []);

useEffect(() => {
if (id) {
axios
.get(`${import.meta.env.VITE_BACKEND_URL}/api/recipebyuser/${id}`, {
withCredentials: true,
})
.then((res) => {
if (res.data) {
setRecipeBy(res.data);
} else {
console.info("No data found");
}
})
.catch((error) => {
console.error(error);
});
}
}, [id]);
const handleSaveClick = () => {
const params = { id: "value" };
axios
.put(
`${import.meta.env.VITE_BACKEND_URL}/api/users/${params.id}`,
`${import.meta.env.VITE_BACKEND_URL}/api/users/${id}`,
{
username: userData.username,
description: userData.description,
Expand All @@ -48,8 +72,7 @@ function Profil() {
},
{ withCredentials: true }
)
.then((res) => {
console.info(res.data.firstname);
.then(() => {
setEditing(false);
})
.catch((error) => {
Expand All @@ -66,26 +89,6 @@ function Profil() {
}));
};

useEffect(() => {
const params = { id: "value" };
const endpoints = [
`${import.meta.env.VITE_BACKEND_URL}/api/recipebyuser/${params.id}`,
];
Promise.all(
endpoints.map((endpoint) =>
axios.get(endpoint, {
withCredentials: true,
})
)
)
.then(([{ data: recipebyuser }]) => {
setRecipeBy(recipebyuser);
})
.catch((erreur) => {
console.error(erreur);
});
}, []);

return (
<div className="body-content">
<div className="Profil_container">
Expand Down Expand Up @@ -171,7 +174,7 @@ function Profil() {
Mes recettes :{" "}
{recipeBy &&
recipeBy.map((recipeU) => (
<li key={recipeU.user_id}>
<li key={recipeU.id}>
{recipeU.name} {recipeU.title}
</li>
))}
Expand All @@ -181,9 +184,7 @@ function Profil() {
<button
className="Edit"
type="button"
onClick={
editing ? handleSaveClick : handleKeepClick && handleEditClick
}
onClick={editing ? handleSaveClick : handleEditClick}
>
{editing ? "Save" : "Edit"}
</button>
Expand Down

0 comments on commit a82fb36

Please sign in to comment.