Skip to content

Commit

Permalink
Refactor API endpoints to use VITE_SERVER_PORT for environment config…
Browse files Browse the repository at this point in the history
…uration
  • Loading branch information
Anuj3553 committed Nov 10, 2024
1 parent 91d57a7 commit c51940b
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 23 deletions.
8 changes: 6 additions & 2 deletions client/src/component/Blog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ BlogPage.propTypes = {
};

export default function BlogPage(props) {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const [searchTerm, setSearchTerm] = useState("");
const [selectedCategory, setSelectedCategory] = useState("All");
const [blogPosts, setBlogPosts] = useState([]);
Expand Down Expand Up @@ -63,7 +65,7 @@ export default function BlogPage(props) {

const fetchData = async () => {
try {
let response = await fetch("http://localhost:5000/api/blog/all-blog");
let response = await fetch(`${VITE_SERVER_PORT}/api/blog/all-blog`);
let data = await response.json();
setBlogPosts(data.blogs);
console.log(data.blogs);
Expand Down Expand Up @@ -111,6 +113,8 @@ export default function BlogPage(props) {

useEffect(() => {
fetchData();

// eslint-disable-next-line
}, []);

return (
Expand Down Expand Up @@ -214,7 +218,7 @@ export default function BlogPage(props) {
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-2 gap-8">
{filteredPosts.length > 0 ? (
filteredPosts.map((blogPost) => {
const postUrl = `http://localhost:5000/api/blog/getById/${blogPost._id}`;
const postUrl = `{VITE_SERVER_PORT}/api/blog/getById/${blogPost._id}`;
return (
<div
className={`max-w-3xl mx-auto my-8 ${props.mode === 'light' ? 'bg-white' : 'bg-gray-800'} rounded-lg shadow-md overflow-hidden`}
Expand Down
4 changes: 2 additions & 2 deletions client/src/component/Collab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { io } from "socket.io-client";
import MonacoEditor from "@monaco-editor/react";

// Adjust this to your Socket.IO server URL
const SOCKET_SERVER_URL = "http://localhost:5000";
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const Collab = () => {
const [code, setCode] = useState("// Start coding collaboratively!\n");
Expand All @@ -12,7 +12,7 @@ const Collab = () => {

// Initialize the Socket.IO client
useEffect(() => {
const newSocket = io(SOCKET_SERVER_URL);
const newSocket = io(VITE_SERVER_PORT);
setSocket(newSocket);

newSocket.on("code_update", (newCode) => {
Expand Down
4 changes: 3 additions & 1 deletion client/src/component/CreateBlog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'react-quill/dist/quill.snow.css';
import { useNavigate } from 'react-router-dom';

const CreateBlog = () => {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const [formData, setFormData] = useState({
id: '',
title: '',
Expand Down Expand Up @@ -42,7 +44,7 @@ const CreateBlog = () => {
const blogPost = { ...formData, id: uniqueId }; // Add the ID to formData
console.log(blogPost)
try {
const response = await axios.post('http://localhost:5000/api/blog/post-blog', blogPost, {
const response = await axios.post(`${VITE_SERVER_PORT}/api/blog/post-blog`, blogPost, {
headers: { 'Content-Type': 'application/json' }
});

Expand Down
21 changes: 17 additions & 4 deletions client/src/component/DiscussionForum.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import PropTypes from 'prop-types';
import { useState, useEffect } from 'react';

const DiscussionForum = (props) => {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const [questions, setQuestions] = useState([]);

// Fetch questions from the backend API
useEffect(() => {
const fetchQuestions = async () => {
try {
const response = await fetch('http://localhost:5000/api/discussion/getQuestion');
const response = await fetch(`${VITE_SERVER_PORT}/api/discussion/getQuestion`);
if (response.ok) {
const data = await response.json();
setQuestions(data);
Expand All @@ -20,6 +23,8 @@ const DiscussionForum = (props) => {
};

fetchQuestions();

// eslint-disable-next-line
}, []);

// Helper function to save a new question
Expand All @@ -31,7 +36,7 @@ const DiscussionForum = (props) => {
};

try {
const response = await fetch('http://localhost:5000/api/discussion/postQuestion', {
const response = await fetch(`${VITE_SERVER_PORT}/api/discussion/postQuestion`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -53,7 +58,7 @@ const DiscussionForum = (props) => {
// Helper function to add an answer to a question
const addAnswer = async (questionId, answerContent) => {
try {
const response = await fetch(`http://localhost:5000/api/discussion/${questionId}/answer`, {
const response = await fetch(`${VITE_SERVER_PORT}/api/discussion/${questionId}/answer`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Expand All @@ -77,7 +82,7 @@ const DiscussionForum = (props) => {
};

// Function to render the Question Card
const renderQuestionCard = (question) => {
const renderQuestionCard = (question, props) => {
return (
<div
className={`${props.mode === 'dark' ? 'bg-gray-800 text-white' : 'bg-white text-gray-800'
Expand Down Expand Up @@ -129,6 +134,10 @@ const DiscussionForum = (props) => {
);
};

AnswerForm.propTypes = {
questionId: PropTypes.string.isRequired,
};

// Function to render the Question Form
const QuestionForm = () => {
const [newQuestion, setNewQuestion] = useState('');
Expand Down Expand Up @@ -190,4 +199,8 @@ const DiscussionForum = (props) => {
);
};

DiscussionForum.propTypes = {
mode: PropTypes.string.isRequired,
};

export default DiscussionForum;
3 changes: 1 addition & 2 deletions client/src/component/EditProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import userDummyImg from "../assets/images/User/User.png";
import "../css/EditProfile.css";

const EditProfile = (props) => {
const VITE_SERVER_PORT =
import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const [profile, setProfile] = useState({
name: "",
Expand Down
4 changes: 3 additions & 1 deletion client/src/component/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useEffect, useState } from 'react';
import VisitorCounter from './Footers/VisitorCount';

const Footer = (props) => {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const [email, setEmail] = useState("");
const [name, setName] = useState("");
const [message, setMessage] = useState("");
Expand All @@ -25,7 +27,7 @@ const Footer = (props) => {
}

try {
const response = await fetch("http://localhost:5000/api/profile/subscribe", {
const response = await fetch(`${VITE_SERVER_PORT}/api/profile/subscribe`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
6 changes: 4 additions & 2 deletions client/src/component/Footers/VisitorCount.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { useEffect, useState } from "react";

// Function to increment the visitor count
async function incrementVisitorCount() {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

try {
const response = await fetch("http://localhost:5000/api/visitor/increment", {
const response = await fetch(`${VITE_SERVER_PORT}/api/visitor/increment`, {
method: "POST",
});
const data = await response.json();
Expand All @@ -17,7 +19,7 @@ async function incrementVisitorCount() {
// Function to get the current visitor count
async function getVisitorCount() {
try {
const response = await fetch("http://localhost:5000/api/visitor/count", {
const response = await fetch(`${VITE_SERVER_PORT}/api/visitor/count`, {
method: "GET",
});
const data = await response.json();
Expand Down
12 changes: 6 additions & 6 deletions client/src/component/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useAuth } from '../contexts/authContext';

const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const Login = ({ mode, showAlert, loggedin, setloggedin }) => {
const Login = ({ mode, loggedin, setloggedin }) => {

const [credentials, setCredentials] = useState({ email: "", password: "" });
const [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -61,7 +61,7 @@ const Login = ({ mode, showAlert, loggedin, setloggedin }) => {

const json = await response.json();
console.log(json);

if (json.success) {
localStorage.setItem("token", json.authtoken);
toast.success("Login Successfully!");
Expand Down Expand Up @@ -114,7 +114,7 @@ const Login = ({ mode, showAlert, loggedin, setloggedin }) => {
toast.error("Google sign-in failed. Please try again.");
setloggedin(false);
}
};
};

return (
<div className="min-h-screen flex items-center justify-center mt-14" data-aos="zoom-in" data-aos-duration="1800">
Expand Down Expand Up @@ -186,17 +186,17 @@ const Login = ({ mode, showAlert, loggedin, setloggedin }) => {
<button className="submit" id="login-btn" type="submit" onClick={handleSubmit} disabled={loading}>
{loading ? <Spin size="small" /> : "Login"}
</button>



<button
disabled={loggedin}
onClick={(e) => {
onClick={(e) => {
e.preventDefault();
navigate("/login-otp")
}}
className={`w-full flex items-center justify-center mt-3 gap-x-3 py-2.5 border bg-[#6366f1] text-white rounded-full text-sm font-medium hover:bg-[#4a4cc5] transition duration-300`}>

Login with mobile

</button>
Expand Down
4 changes: 3 additions & 1 deletion client/src/component/Projects.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useNavigate } from 'react-router-dom';
import axios from 'axios';

const Projects = ({ mode }) => {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const navigate = useNavigate();
const [projects, setProjects] = useState([]); // State to store fetched projects
const [loading, setLoading] = useState(true); // State for loading state
Expand All @@ -13,7 +15,7 @@ const Projects = ({ mode }) => {
// Fetch the data from the API when the component mounts
const fetchProjects = async () => {
try {
const response = await axios.get('http://localhost:5000/api/showcaseProjects/all-projects');
const response = await axios.get(`${VITE_SERVER_PORT}/api/showcaseProjects/all-projects`);
setProjects(response.data); // Store the fetched projects in state
setLoading(false); // Set loading to false after data is fetched
} catch (err) {
Expand Down
4 changes: 3 additions & 1 deletion client/src/component/ReadMoreBlog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ const images = [


const ReadMoreBlog = (props) => {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const { id } = useParams();
const [blogPost, setBlogPost] = useState(null);
const fetchData = async () => {
try {
const response = await fetch(`http://localhost:5000/api/blog/getById/${id}`);
const response = await fetch(`${VITE_SERVER_PORT}/api/blog/getById/${id}`);

const data = await response.json();
setBlogPost(data.blog); // Set the retrieved blog post to state
Expand Down
4 changes: 3 additions & 1 deletion client/src/component/UploadProject.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import PropTypes from "prop-types";
import { useNavigate } from 'react-router-dom';

const UploadProject = ({ mode }) => {
const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com";

const navigate = useNavigate();
const [formData, setFormData] = useState({
title: '',
Expand Down Expand Up @@ -32,7 +34,7 @@ const UploadProject = ({ mode }) => {
e.preventDefault();

try {
const response = await fetch('http://localhost:5000/api/showcaseProjects/post-project', {
const response = await fetch(`${VITE_SERVER_PORT}/api/showcaseProjects/post-project`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down

0 comments on commit c51940b

Please sign in to comment.