Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: edit delete racing moments #141

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tracknow/web/src/hooks/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EditUserPic,
OneUser
} from '../Types';
import { useLaptimes } from './useLaptimes';

// backend api routes.

Expand All @@ -33,6 +34,9 @@ const endpoints = {
GET_ONE_LAPTIME: (user_id: number, id: number) => `${API_PREFIX_URL}/users/${user_id}/laptimes/${id}`, // see a specific laptime of someone
GET_USERS_LAPTIMES: (user_id: number, page: number) => `${API_PREFIX_URL}/users/${user_id}/laptimes?page=${page}`, // see personal moments of other users

EDIT_USER_LAPTIME: (id: number) => `${API_PREFIX_URL}/user/laptime/edit/${id}`,
DELETE_USER_LAPTIME: (id: number) => `${API_PREFIX_URL}/user/laptime/delete/${id}`,

GET_IDENTITY: `${API_PREFIX_URL}/protected`, // make sure user is logged in and in session.

// formula 1 standings
Expand Down Expand Up @@ -240,6 +244,8 @@ async function fetchAUserLaptime(user_id: number, id: number): Promise<GetUserLa
const data: GetUserLaptimesResponse = await response.json();
return data;
}

// function to edit user profile
async function EditUserProfile(user_id: Number, editUser: EditUser) {

const token = localStorage.getItem('access_token')
Expand All @@ -260,6 +266,7 @@ async function EditUserProfile(user_id: Number, editUser: EditUser) {
return data;
};

// function to edit user profile picture
async function EditUserProfilePic(user_id: Number, editUserpic: EditUserPic) {

const token = localStorage.getItem('access_token')
Expand Down Expand Up @@ -301,6 +308,46 @@ async function fetchUsersLaptimes(user_id: number, page: number): Promise<GetUse
const data: GetUserLaptimesResponse[] = await response.json();
return data;
};

async function editUserLaptime(id: number, edit_laptime: Laptime): Promise<Laptime> {

const token = localStorage.getItem('access_token')

const response = await fetch(endpoints.EDIT_USER_LAPTIME(id), {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY,
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(edit_laptime)
});

if (!response.ok) {
throw new Error('Failed to edit User laptime')
}

const data = await response.json()
return data;
};

async function deleteUserLaptime(id: number) {

const token = localStorage.getItem('access_token')

const response = await fetch(endpoints.DELETE_USER_LAPTIME(id), {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
'x-api-key': API_KEY
}
})

const data = await response.json();
return data;
};

// function to fetch 'live' f1 constructors standings
async function fetchF1Teams() {

Expand Down Expand Up @@ -364,6 +411,8 @@ const API = {
EditUserProfilePic,
fetchAUserLaptime,
fetchUsersLaptimes,
deleteUserLaptime,
editUserLaptime,
fetchF1Drivers,
fetchF1Teams,
fetchYoutube
Expand Down
34 changes: 32 additions & 2 deletions tracknow/web/src/hooks/useLaptimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ export const useLaptimes = () => {
mySetPage((prevPage) => prevPage + 1);
}
};

// add laptimes
const addLaptime = async (newLaptime: Laptime,) => {
const addLaptime = async (newLaptime: Laptime) => {

try {

Expand All @@ -109,10 +110,39 @@ export const useLaptimes = () => {
}
};

// edit user laptime
const editLaptime = async (id: number, editLaptime: Laptime) => {

try {

const response = await API.editUserLaptime(id, editLaptime);
return response;

} catch (error) {

throw new Error("Laptime cannot be edited!")
}

};

// delete user laptime
const deleteLaptime = async (id: number) => {

try {

const response = await API.deleteUserLaptime(id);
return response;

} catch (error) {

throw new Error("Laptime cannot be deleted.")
}
};

return {
laptime, addLaptime, mylaptime, fetchMoreData,
hasMore, fetchMoreData2, hasMore2, laptime_loading,
fetchUsersLaptimes, fetchAUserLaptime
fetchUsersLaptimes, fetchAUserLaptime, editLaptime, deleteLaptime

};

Expand Down
Loading