Skip to content

Commit

Permalink
add in password for every api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdisungkar committed May 30, 2024
1 parent ff6a1f6 commit 964d18a
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 185 deletions.
70 changes: 4 additions & 66 deletions application/app/frontend/src/api/api.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
export const getPassenger = async () => {
const url = "/passengers";
const response = await fetch(url);
return response.json();
}

export const AddPassenger = async (post) => {
const url = "/passengers";
const response = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(post),
});
return response.json();
};

export const loginUser = async (body) => {
const url = "/profile";
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(body)
});
if (response.ok) {
return response.json();
} else {
throw new Error('Failed to login');
}
} catch (error) {
throw error;
}
};

export const fetchCoordinates = async (address) => {
const url = `https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(address)}&format=json`;
const response = await fetch(url);
Expand All @@ -53,29 +13,7 @@ export const fetchCoordinates = async (address) => {
}
};


export const getPendingTripRequests = async (username) => {
const url = "/trip_requests/get/pending";
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({username})
});
if (response.ok) {
return response.json();
} else {
throw new Error('Failed to fetch pending trip requests');
}
} catch (error) {
throw error;
}
};

export const getNearbyTripRequests = async (tripId, username) => {
export const getNearbyTripRequests = async (tripId, username, password) => {
const url = "/trip/get/pending_nearby";
try {
const response = await fetch(url, {
Expand All @@ -84,7 +22,7 @@ export const getNearbyTripRequests = async (tripId, username) => {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({ trip_id: tripId, username })
body: JSON.stringify({ trip_id: tripId, username, password })
});
if (response.ok) {
return response.json();
Expand All @@ -96,7 +34,7 @@ export const getNearbyTripRequests = async (tripId, username) => {
}
};

export const approveRequest = async (username, tripRequestId, tripId) => {
export const approveRequest = async (username, password, tripRequestId, tripId) => {
const url = "/trip/post/approve";
try {
const response = await fetch(url, {
Expand All @@ -105,7 +43,7 @@ export const getNearbyTripRequests = async (tripId, username) => {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, trip_request_id: tripRequestId, trip_id: tripId }),
body: JSON.stringify({ username, password, trip_request_id: tripRequestId, trip_id: tripId }),
});
if (response.ok) {
return await response.json();
Expand Down
9 changes: 6 additions & 3 deletions application/app/frontend/src/components/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const SimpleMap = () => {
'Accept': 'application/json',
},
body: JSON.stringify({
username: user.username,
password: user.password,
trip_id: tripId
}),
});
Expand All @@ -50,7 +52,7 @@ const SimpleMap = () => {
if (!doneFetch) {
fetchTripLocation();
}
}, [tripId, doneFetch]);
}, [tripId, doneFetch, user.username, user.password]);

useEffect(() => {
const fetchLocations = async () => {
Expand All @@ -63,7 +65,8 @@ const SimpleMap = () => {
},
body: JSON.stringify({
trip_id: tripId,
username: user.username
username: user.username,
password: user.password
}),
});

Expand All @@ -79,7 +82,7 @@ const SimpleMap = () => {
if (doneFetch) {
fetchLocations();
}
}, [tripId, doneFetch, user.username]);
}, [tripId, doneFetch, user.username, user.password]);

useEffect(() => {
if (doneFetch && trip.start_location && trip.end_location && passengers.length > 0) {
Expand Down
97 changes: 0 additions & 97 deletions application/app/frontend/src/components/PassengerManager.js

This file was deleted.

4 changes: 2 additions & 2 deletions application/app/frontend/src/components/RequestList.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const RequestList = () => {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({ username: user.username }), // Use the username from context
body: JSON.stringify({ username: user.username, password: user.password }), // Use the username from context
});

if (response.ok) {
Expand All @@ -37,7 +37,7 @@ const RequestList = () => {
};

fetchTrips();
}, [user.username]);
}, [user.password, user.username]);

if (loading) {
return <div>Loading trips...</div>;
Expand Down
5 changes: 4 additions & 1 deletion application/app/frontend/src/components/RideRequest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from 'react';
import React, { useContext, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { fetchCoordinates } from '../api/api';
import styles from '../styles/RideRequest.module.css';
Expand Down Expand Up @@ -65,6 +65,8 @@ function RideRequest() {
const dropoffCoords = await fetchCoordinates(dropoffAddress);

const tripData = {
username: user.username,
password: user.password,
start_location: { ...pickupCoords},
end_location: { ...dropoffCoords},
};
Expand Down Expand Up @@ -111,6 +113,7 @@ function RideRequest() {

const tripData = {
username: user.username,
password: user.password,
pickup_location: { ...pickupCoords, "address": pickupAddress },
dropoff_location: { ...dropoffCoords, "address": dropoffAddress },
...tripWindows
Expand Down
7 changes: 4 additions & 3 deletions application/app/frontend/src/components/RideRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import styles from '../styles/TripRequest.module.css';
function RideRequests() {
const { user } = useContext(UserContext);
const username = user.username;
const password = user.password;
const { tripId } = useParams();
const [tripRequests, setTripRequests] = useState([]);
const [errorMessage, setErrorMessage] = useState('');
Expand All @@ -19,21 +20,21 @@ function RideRequests() {
useEffect(() => {
const fetchTripRequests = async () => {
try {
const data = await getNearbyTripRequests(tripId, username);
const data = await getNearbyTripRequests(tripId, username, password);
setTripRequests(data.Trips);
} catch (error) {
setErrorMessage('Failed to fetch trip requests.');
console.error('Failed to fetch trip requests:', error);
}
};
fetchTripRequests();
}, [tripId, username]);
}, [password, tripId, username]);

const handleApprove = async (tripRequestId) => {
try {
setErrorMessage('');
setSuccessMessage('');
const response = await approveRequest(username, tripRequestId, tripId);
const response = await approveRequest(username, password, tripRequestId, tripId);
setSuccessMessage(response.message);
setTripRequests(tripRequests.filter(request => request.id !== tripRequestId));
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions application/app/frontend/src/components/TripList.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TripList = () => {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({ username: user.username }), // Use the username from context
body: JSON.stringify({ username: user.username, password: user.password }), // Use the username from context
});

if (response.ok) {
Expand All @@ -38,7 +38,7 @@ const TripList = () => {
};

fetchTrips();
}, [user.username]);
}, [user.password, user.username]);

if (loading) {
return <div>Loading trips...</div>;
Expand Down
1 change: 1 addition & 0 deletions application/app/frontend/src/components/TripRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function TripRequest() {
const tripData = {
...tripDetails,
username: user.username,
password: user.password,
start_location: {...startCoords, "address": startAddress},
end_location: {...endCoords, "address": endAddress}
};
Expand Down
2 changes: 1 addition & 1 deletion application/app/frontend/src/components/loginEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function LoginEmail() {
});
const data = await response.json();
if (response.status === 200) {
setUser({ username, user_type });
setUser({ username, password, user_type });
if (user_type === 'driver') {
navigate('/trip-request');
} else if (user_type === 'passenger') {
Expand Down
5 changes: 0 additions & 5 deletions application/app/frontend/src/pages/SignUpPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ const SignUpPage = () => {
};

const handleSubmit = async (event) => {
console.log(username);
console.log(password);
console.log(email);
console.log(maxAvailableSeat);
console.log(licencePlate);
event.preventDefault();
try {
if (user_type === 'driver') {
Expand Down
Loading

0 comments on commit 964d18a

Please sign in to comment.