Skip to content

Commit

Permalink
feat: common api client
Browse files Browse the repository at this point in the history
  • Loading branch information
BRAVO68WEB committed Jan 3, 2024
1 parent d9b69fb commit 00e9cbd
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 70 deletions.
11 changes: 11 additions & 0 deletions apps/web/src/libs/api.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import axios from 'axios';

const apiClient = axios.create({
baseURL: import.meta.env.VITE_API_URL,
})

export default {
get: apiClient.get,
post: apiClient.post,
put: apiClient.put,
}
4 changes: 2 additions & 2 deletions apps/web/src/pages/app.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import '../App.css'
import 'react-toastify/dist/ReactToastify.css';
import Navbar from '../components/navbar';
import axios from 'axios';
import { useEffect, useState } from 'react';
import Machines from "../components/machine"
import Challenge from '../components/challenge';
import { ToastContainer } from 'react-toastify';
import apiClient from '../libs/api.client';

function Player() {
const token = localStorage.getItem('token');
Expand All @@ -16,7 +16,7 @@ function Player() {
useEffect(() => {
if(!token) return window.location.href = '/login';

axios.get(import.meta.env.VITE_API_URL + "/challenge/progress", {
apiClient.get("/challenge/progress", {
headers: {
"Authorization": "Bearer " + token,
}
Expand Down
22 changes: 9 additions & 13 deletions apps/web/src/pages/leaderboard.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import "../App.css";
import "react-toastify/dist/ReactToastify.css";
import { useEffect, useState } from "react";
import axios from "axios";
import apiClient from '../libs/api.client';
import Navbar from "../components/navbar";

function Leaderboard() {
const [leaderboard, setLeaderboard] = useState([]);
Expand All @@ -11,18 +12,12 @@ function Leaderboard() {

if (!token) return (window.location.href = "/login");

let config = {
method: "get",
maxBodyLength: Infinity,
url: import.meta.env.VITE_API_URL + "/stats/leaderboard",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
};

axios
.request(config)
apiClient
.get("/stats/leaderboard", {
headers: {
Authorization: "Bearer " + token,
}
})
.then((response) => {
if(!Array.isArray(response.data)) return window.location.href = '/login'
setLeaderboard(response.data);
Expand All @@ -34,6 +29,7 @@ function Leaderboard() {

return (
<>
<Navbar />
<div>
<img src="/ee.png" className="logo react" alt="ee logo" />
</div>
Expand Down
27 changes: 10 additions & 17 deletions apps/web/src/pages/login.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react'
import axios from 'axios'
import apiClient from '../libs/api.client';
import { ToastContainer, toast } from 'react-toastify';
import { Link } from "react-router-dom";

Expand All @@ -14,23 +14,16 @@ function Home() {
let token = localStorage.getItem('token');

const handleSubmit = (event) => {
event.preventDefault()
let data = JSON.stringify({
"email": email,
"password": password
});
event.preventDefault();

let config = {
method: 'post',
maxBodyLength: Infinity,
url: import.meta.env.VITE_API_URL + '/user/login',
headers: {
'Content-Type': 'application/json'
},
data : data
};

axios.request(config)
apiClient.post("/user/login", {
email,
password
}, {
headers: {
"Content-Type": "application/json"
}
})
.then((response) => {
if (response.data.message === 'Login successful') {
token = response.data.token;
Expand Down
31 changes: 12 additions & 19 deletions apps/web/src/pages/register.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react'
import axios from 'axios'
import apiClient from '../libs/api.client';
import { ToastContainer, toast } from 'react-toastify';
import { Link } from "react-router-dom";

Expand All @@ -14,24 +14,17 @@ function Home() {

const handleSubmit = (event) => {
event.preventDefault()
let data = JSON.stringify({
"email": email,
"password": password,
"firstName": firstName,
"lastName": lastName
});

let config = {
method: 'post',
maxBodyLength: Infinity,
url: import.meta.env.VITE_API_URL + '/user/register',
headers: {
'Content-Type': 'application/json'
},
data : data
};

axios.request(config)

apiClient.post("/user/register", {
email,
password,
firstName,
lastName
}, {
headers: {
"Content-Type": "application/json"
}
})
.then((response) => {
if (response.data.email === email) {
toast.success('Register Successful!', {
Expand Down
23 changes: 8 additions & 15 deletions apps/web/src/pages/verify.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react'
import axios from 'axios'
import apiClient from '../libs/api.client';
import { ToastContainer, toast } from 'react-toastify';

import '../App.css'
Expand All @@ -10,21 +10,14 @@ function Home() {

const handleSubmit = (event) => {
event.preventDefault()
let data = JSON.stringify({
"otp": otp,
});

let config = {
method: 'post',
maxBodyLength: Infinity,
url: import.meta.env.VITE_API_URL + '/user/verify',
headers: {
'Content-Type': 'application/json'
},
data : data
};

axios.request(config)
apiClient.post("/user/verify", {
otp
}, {
headers: {
"Content-Type": "application/json"
}
})
.then((response) => {
if (response.data.message === 'User verified successfully') {
toast.success('Email Verified!', {
Expand Down
10 changes: 6 additions & 4 deletions apps/web/src/pages/whoami.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import '../App.css'
import 'react-toastify/dist/ReactToastify.css';
import { useState, useEffect } from 'react';
import axios from 'axios';
import apiClient from '../libs/api.client';
import Navbar from "../components/navbar";

function WhoAmI() {
const [userWhoami, setUserWhoami] = useState(null);
Expand All @@ -13,7 +14,7 @@ function WhoAmI() {

if(!token) return window.location.href = '/login';

axios.get(import.meta.env.VITE_API_URL + "/user/whoami", {
apiClient.get("/user/whoami", {
headers: {
"Authorization": "Bearer " + token,
}
Expand All @@ -24,7 +25,7 @@ function WhoAmI() {
setUserWhoami(res.data);
})

axios.get(import.meta.env.VITE_API_URL + "/team/whoami", {
apiClient.get("/team/whoami", {
headers: {
"Authorization": "Bearer " + token,
}
Expand All @@ -35,7 +36,7 @@ function WhoAmI() {
setTeamWhoami(res.data);
})

axios.get(import.meta.env.VITE_API_URL + "/stats/team", {
apiClient.get(import.meta.env.VITE_API_URL + "/stats/team", {
headers: {
"Authorization": "Bearer " + token,
}
Expand All @@ -48,6 +49,7 @@ function WhoAmI() {

return (
<>
<Navbar />
<div>
<img src="/ee.png" className="logo react" alt="ee logo" />
</div>
Expand Down

0 comments on commit 00e9cbd

Please sign in to comment.