File size: 3,077 Bytes
1c8e50c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import axios from 'axios';

// Read base URL from env (build-time). Provide a safe fallback and normalize a few
// common misconfigurations (e.g. ":8090" or missing protocol).
let API_BASE_URL = process.env.REACT_APP_API_BASE_URL;

const resolveBrowserDefault = () => {
  if (typeof window !== 'undefined' && window.location) {
    return `${window.location.origin}/api`;
  }
  return 'http://localhost:8090';
};

// Normalization helpers:
const normalizeBaseUrl = (raw) => {
  if (!raw) return resolveBrowserDefault();
  if (raw.startsWith('/')) return raw;
  // If user provided a port-only like ":8090", prepend localhost
  if (raw.startsWith(':')) return `http://localhost${raw}`;
  // If no protocol specified but contains host (e.g. "localhost:8090"), add http://
  if (!/^https?:\/\//i.test(raw)) return `http://${raw}`;
  return raw;
};

API_BASE_URL = normalizeBaseUrl(API_BASE_URL);
// Helpful debug info during development (will appear in browser console)
if (process.env.NODE_ENV === 'development') {
  // eslint-disable-next-line no-console
  console.debug('[api] Using API_BASE_URL =', API_BASE_URL);
}
const AUTH_TOKEN_KEY = 'authToken';

const api = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
  },
});

api.interceptors.request.use((config) => {
  const token = window.localStorage.getItem(AUTH_TOKEN_KEY);
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

const buildPath = (path) => path.replace(/\/{2,}/g, '/');

export const authAPI = {
  signup: (userData) => api.post(buildPath('/auth/signup'), userData),
  signin: (credentials) => api.post(buildPath('/auth/signin'), credentials),
  getProfile: () => api.get(buildPath('/api/users/profile')),
};

export const taskAPI = {
  createTask: (taskData) => api.post(buildPath('/api/tasks'), taskData),
  getTask: (taskId) => api.get(buildPath(`/api/tasks/${taskId}`)),
  getUserTasks: () => api.get(buildPath('/api/tasks/user')),
  getAllTasks: () => api.get(buildPath('/api/tasks')),
  updateTask: (taskId, taskData) => api.put(buildPath(`/api/tasks/${taskId}`), taskData),
  assignTask: (taskId, userId) => api.put(buildPath(`/api/tasks/${taskId}/user/${userId}/assigned`)),
  completeTask: (taskId) => api.put(buildPath(`/api/tasks/${taskId}/complete`)),
  deleteTask: (taskId) => api.delete(buildPath(`/api/tasks/${taskId}`)),
};

export const submissionAPI = {
  createSubmission: (submissionData) => api.post(buildPath('/api/submissions'), submissionData),
  getSubmission: (submissionId) => api.get(buildPath(`/api/submissions/${submissionId}`)),
  getAllSubmissions: () => api.get(buildPath('/api/submissions')),
  getTaskSubmissions: (taskId) => api.get(buildPath(`/api/submissions/task/${taskId}`)),
  updateSubmission: (submissionId, submissionData) => api.put(buildPath(`/api/submissions/${submissionId}`), submissionData),
};

export const userAPI = {
  getAllUsers: () => api.get(buildPath('/api/users')),
  getUserById: (userId) => api.get(buildPath(`/api/users/${userId}`)),
};

export default api;