From 1a804e3fc0b1a570e5a5421be3fa9118fed676c9 Mon Sep 17 00:00:00 2001 From: Somnath Chattaraj Date: Sun, 13 Oct 2024 10:29:42 +0530 Subject: [PATCH 1/2] Automate Backend Deploy --- .github/workflows/deploy.yaml | 59 +++++++++++++++++------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index c5412cd..756e4c1 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -25,56 +25,55 @@ jobs: context: ./backend file: ./backend/Dockerfile push: true - tags: somnath910/statuscode1:latest + tags: somnath910/statuscode1:latest # Replace with your Docker Hub username and repository - name: Verify Pushed Image - run: docker pull somnath910/statuscode1:latest + run: docker pull somnath910/statuscode1:latest # Replace with your Docker Hub username and repository --- - name: Deploy to EC2 with Prisma Migrations and Cleanup on: push: branches: - - master + - master # Specify your deployment branch jobs: deploy: runs-on: ubuntu-latest - + steps: - - name: Checkout code - uses: actions/checkout@v2 + - name: Checkout code + uses: actions/checkout@v2 - - name: Add SSH Key - uses: webfactory/ssh-agent@v0.5.3 - with: - ssh-private-key: ${{ secrets.SSH_KEY }} + - name: Add SSH Key + uses: webfactory/ssh-agent@v0.5.3 + with: + ssh-private-key: ${{ secrets.SSH_KEY }} - - name: Deploy to EC2, Clean Up Old Migrations, and Run Prisma Migrations - run: | - ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.EC2_IP }} << 'EOF' - cd /home/ubuntu/Campus-Chatter/backend # Change this to your project path on EC2 + - name: Deploy to EC2, Clean Up Old Migrations, and Run Prisma Migrations + run: | + ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.EC2_IP }} << 'EOF' + cd /home/ubuntu/Campus-Chatter/backend # Change this to your project path on EC2 - # Stash any local changes - git stash + # Stash any local changes + git stash - # Pull the latest changes from master - git pull origin master + # Pull the latest changes from master + git pull origin master - # Remove old migration files - rm -rf prisma/migrations + # Remove old migration files + rm -rf prisma/migrations - # Install any new dependencies - npm install + # Install any new dependencies + npm install - # Apply Prisma migrations using deploy for production (instead of dev) - npx prisma migrate deploy --force + # Apply Prisma migrations using deploy for production (instead of dev) + npx prisma migrate deploy --force - # Compile TypeScript - tsc -b + # Compile TypeScript + tsc -b - # Restart your backend service using PM2 - pm2 restart dist/index.js - EOF + # Restart your backend service using PM2 + pm2 restart dist/index.js + EOF From 98dee5f0090fc47c5cd51a73262d75ff625f8518 Mon Sep 17 00:00:00 2001 From: tanish35 Date: Sun, 13 Oct 2024 10:33:26 +0530 Subject: [PATCH 2/2] Fix username bug,Add profile details in edit --- backend/src/controllers/userControllers.ts | 4 +- frontend/src/components/AddUsername.jsx | 2 +- frontend/src/components/EditDetails.jsx | 66 +++++++++++-------- frontend/src/components/Posts.jsx | 4 +- frontend/src/components/SinglePost.jsx | 18 +++++ .../src/components/chatroomui/joinRoom1.jsx | 4 ++ 6 files changed, 69 insertions(+), 29 deletions(-) diff --git a/backend/src/controllers/userControllers.ts b/backend/src/controllers/userControllers.ts index 4208765..a050050 100644 --- a/backend/src/controllers/userControllers.ts +++ b/backend/src/controllers/userControllers.ts @@ -55,7 +55,8 @@ const googleSignInOrSignUp = asyncHandler( sameSite: "lax", }); const username = user.username; - res.status(200).json({ isCollegeEmail, username }); + const userId = user.user_id; + res.status(200).json({ isCollegeEmail, username, userId }); } ); @@ -318,6 +319,7 @@ const getCurrentUserDetails = asyncHandler( user_id: true, email: true, username: true, + pic: true, userCourses: { select: { Course: { diff --git a/frontend/src/components/AddUsername.jsx b/frontend/src/components/AddUsername.jsx index b5f427f..414c8d4 100644 --- a/frontend/src/components/AddUsername.jsx +++ b/frontend/src/components/AddUsername.jsx @@ -71,7 +71,7 @@ const AddUsername = () => { setLoading(false); toast({ title: "Error", - description: "Error adding username.", + description: err.response?.data.message || "An error occurred", status: "error", duration: 3000, isClosable: true, diff --git a/frontend/src/components/EditDetails.jsx b/frontend/src/components/EditDetails.jsx index 76bba6c..c44be20 100644 --- a/frontend/src/components/EditDetails.jsx +++ b/frontend/src/components/EditDetails.jsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { Box, FormControl, @@ -8,6 +8,8 @@ import { VStack, useToast, Flex, + Avatar, + Text, } from "@chakra-ui/react"; import { z } from "zod"; import axios from "axios"; @@ -41,24 +43,31 @@ const EditDetails = () => { setFormData({ ...formData, pic: e.target.files[0] }); }; - if (loadingUser) { - return ( - - - - ); - } - if (!userDetails) { - toast({ - title: "Error", - description: "You need to be logged in to access this page.", - status: "error", - duration: 3000, - isClosable: true, - }); - navigate("/login"); - return; - } + useEffect(() => { + if (loadingUser) return; + + if (!userDetails) { + toast({ + title: "Error", + description: "You need to be logged in to access this page.", + status: "error", + duration: 3000, + isClosable: true, + }); + navigate("/login"); + } else if (userDetails.username === null) { + navigate(`/addusername/${userDetails.user_id}`); + } + }, [userDetails, loadingUser, navigate, toast]); + + useEffect(() => { + if (userDetails) { + setFormData({ + username: userDetails.username || "", + pic: "", + }); + } + }, [userDetails]); const handleSubmit = async (e) => { e.preventDefault(); @@ -92,13 +101,6 @@ const EditDetails = () => { pic: picUrl, }; - const user = await axios.get("/api/user/me", { - withCredentials: true, - }); - if (!updatedFormData.username) { - updatedFormData.username = user.data.username; - } - EditDetailsSchema.parse(updatedFormData); setError({}); @@ -130,8 +132,20 @@ const EditDetails = () => { } }; + if (loadingUser) { + return ( + + + + ); + } + return ( + + + {userDetails?.username || "Your Username"} +
diff --git a/frontend/src/components/Posts.jsx b/frontend/src/components/Posts.jsx index 6a4c471..cf03096 100644 --- a/frontend/src/components/Posts.jsx +++ b/frontend/src/components/Posts.jsx @@ -108,7 +108,6 @@ const Posts = () => { withCredentials: true, }); setAllCommunities(response.data.college); - console.log(response.data.college); setLoading(false); } catch (err) { setLoading(false); @@ -167,6 +166,9 @@ const Posts = () => { if (!userDetails) { return ; } + if (userDetails.username === null) { + return ; + } return ( diff --git a/frontend/src/components/SinglePost.jsx b/frontend/src/components/SinglePost.jsx index 8095203..e6bd809 100644 --- a/frontend/src/components/SinglePost.jsx +++ b/frontend/src/components/SinglePost.jsx @@ -15,12 +15,15 @@ import { } from "@chakra-ui/react"; import axios from "axios"; import CreateComment from "./CreateComment"; // Import the CreateComment component +import { useUser } from "../hook/useUser"; +import { InfinitySpin } from "react-loader-spinner"; const SinglePost = () => { const { id } = useParams(); const [post, setPost] = useState(null); const [loading, setLoading] = useState(true); const [postLiked, setPostLiked] = useState(false); + const { userDetails, loadingUser } = useUser(); useEffect(() => { const fetchPost = async () => { @@ -55,6 +58,21 @@ const SinglePost = () => { checkIfLiked(); }, [id]); + if (loadingUser) { + return ( +
+ +
+ ); + } + + if (!userDetails) { + return ; + } + if (userDetails.username === null) { + return ; + } + const handleLike = async (postId) => { if (postLiked) { try { diff --git a/frontend/src/components/chatroomui/joinRoom1.jsx b/frontend/src/components/chatroomui/joinRoom1.jsx index f123682..59d7965 100644 --- a/frontend/src/components/chatroomui/joinRoom1.jsx +++ b/frontend/src/components/chatroomui/joinRoom1.jsx @@ -40,6 +40,10 @@ const JoinRoom1 = () => { ); } + if (userDetails.username == null) { + navigate(`/addusername/${userDetails.user_id}`); + } + function handleClick(roomId) { localStorage.removeItem("roomId"); localStorage.removeItem("userId");