Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
Thinh Nguyen D committed Sep 30, 2024
1 parent 5dd4af8 commit 0910942
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
REACT_APP_BACKEND_API = "https://diy-backend.onrender.com/api"
REACT_APP_BACKEND_API = "https://diy-backend.onrender.com/api"

REACT_APP_CLOUDINARY_CLOUD_NAME="dguiswfoo"
REACT_APP_CLOUDINARY_UPLOAD_PRESET="kyowgelt"
6 changes: 6 additions & 0 deletions src/app/config.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export const BASE_URL = process.env.REACT_APP_BACKEND_API;

export const CLOUDINARY_CLOUD_NAME =
process.env.REACT_APP_CLOUDINARY_CLOUD_NAME;

export const CLOUDINARY_UPLOAD_PRESET =
process.env.REACT_APP_CLOUDINARY_UPLOAD_PRESET;
2 changes: 1 addition & 1 deletion src/constants/API.constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const API = {
REGISTER: "/users",
LOGIN: "/users/login",
GET_USER_INFO: "/user/currentUser",
GET_USER_INFO: "/users/currentUser",
};
7 changes: 2 additions & 5 deletions src/contexts/AuthContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ const reducer = (state, action) => {
const AuthContext = createContext({ ...initialState });

const setSession = (accessToken, remember) => {
console.log("🚀 Puritin ~ setSession ~ remember:", remember);
console.log("🚀 Puritin ~ setSession ~ accessToken:", accessToken);
if (accessToken && remember === true) {
if (accessToken && !!remember) {
window.localStorage.setItem("accessToken", accessToken);
apiService.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
} else if (accessToken && remember === false) {
} else if (accessToken && !remember) {
window.localStorage.removeItem("accessToken");
window.sessionStorage.setItem("accessToken", accessToken);
apiService.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
Expand Down Expand Up @@ -119,7 +117,6 @@ function AuthProvider({ children }) {
const sessionToken = window.sessionStorage.getItem("accessToken");
const accessToken = storageToken || sessionToken;
const remember = !!storageToken;
console.log("🚀 Puritin ~ initialize ~ remember:", remember);

if (accessToken && isValidToken(accessToken)) {
setSession(accessToken, remember);
Expand Down
1 change: 0 additions & 1 deletion src/pages/RegisterPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function RegisterPage() {
toast.error(error.message);
}
//trim data first name, lastname and email
console.log("Register", data);
};

return (
Expand Down
12 changes: 8 additions & 4 deletions src/pages/UserPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TabContext, TabList } from "@mui/lab";
import { styled } from "@mui/material/styles";

import useAuth from "../../hooks/useAuth";
import { cloudinaryUpload } from "../../ultis/cloudinary";

const VisuallyHiddenInput = styled("input")({
clip: "rect(0 0 0 0)",
Expand All @@ -27,14 +28,19 @@ function UserPage() {
const [value, setValue] = useState("1");

const { user } = useAuth();
console.log("🚀 Puritin ~ UserPage ~ user:", user);

const navigate = useNavigate();

const handleChange = (event, newValue) => {
setValue(newValue);
};

const handleChangeAvatar = async (image) => {
console.log("🚀 Puritin ~ handleChangeAvatar ~ image:", image);
const imageUrl = await cloudinaryUpload(image);
console.log("🚀 Puritin ~ handleChangeAvatar ~ imageUrl:", imageUrl);
};

return (
<>
<div className="userPage_container">
Expand All @@ -50,9 +56,7 @@ function UserPage() {
</div>
<VisuallyHiddenInput
type="file"
onChange={(event) =>
console.log("file upload", event.target.files)
}
onChange={(event) => handleChangeAvatar(event.target.files)}
multiple
/>
</Button>
Expand Down
9 changes: 8 additions & 1 deletion src/pages/VideoPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ function VideoPage() {
return (
<div className="videoPage_container">
<div className="leftSideVideoPage_container">
<div className="player_box" style={{ position: "relative" }}></div>
<div className="player_box" style={{ position: "relative" }}>
<img
src="images/art3.png"
alt="test"
width={"100%"}
height={"100%"}
/>
</div>
<div className="below_player_box"></div>
</div>
<div className="rightSideVideoPage_container"></div>
Expand Down
8 changes: 4 additions & 4 deletions src/routes/AuthRequire/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ function AuthRequire({ children }) {

const location = useLocation();

if (!isAuthenticated) {
return <Navigate to="/login" state={{ from: location }} replace />;
}

if (!isInitialized) {
return (
<div>
Expand All @@ -23,6 +19,10 @@ function AuthRequire({ children }) {
);
}

if (!isAuthenticated) {
return <Navigate to="/login" state={{ from: location }} replace />;
}

return children;
}

Expand Down
25 changes: 25 additions & 0 deletions src/ultis/cloudinary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CLOUDINARY_CLOUD_NAME, CLOUDINARY_UPLOAD_PRESET } from "../app/config";
import axios from "axios";

export const cloudinaryUpload = async (image) => {
if (!image) return "";

try {
const formData = new FormData();

formData.append("file", image);
formData.append("upload_preset", CLOUDINARY_UPLOAD_PRESET);
console.log("🚀 Puritin ~ cloudinaryUpload ~ formData:", formData);

const response = await axios({
url: `https://api.cloudinary.com/v1_1/${CLOUDINARY_CLOUD_NAME}/image/upload`,
data: formData,
headers: { "Content-Type": "multipart/form-data" },
});

const imageUrl = response.data.secure_url;
return imageUrl;
} catch (error) {
console.log("🚀 Puritin ~ cloudinaryUpload ~ error:", error);
}
};

0 comments on commit 0910942

Please sign in to comment.