Fix: Use centralized API functions instead of manual URL construction
- ComparisonView now uses fetchComparison from lib/api - SchoolSearchModal now uses fetchSchools from lib/api - Fixed bug in fetcher function that incorrectly sliced URLs (url.slice(4) was removing '/com' from '/compare') This fixes the malformed URL issue where '/api/compare' became '/apipare'. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import { SchoolSearchModal } from './SchoolSearchModal';
|
|||||||
import { EmptyState } from './EmptyState';
|
import { EmptyState } from './EmptyState';
|
||||||
import type { ComparisonData, MetricDefinition } from '@/lib/types';
|
import type { ComparisonData, MetricDefinition } from '@/lib/types';
|
||||||
import { formatPercentage, formatProgress } from '@/lib/utils';
|
import { formatPercentage, formatProgress } from '@/lib/utils';
|
||||||
|
import { fetchComparison } from '@/lib/api';
|
||||||
import styles from './ComparisonView.module.css';
|
import styles from './ComparisonView.module.css';
|
||||||
|
|
||||||
interface ComparisonViewProps {
|
interface ComparisonViewProps {
|
||||||
@@ -55,13 +56,8 @@ export function ComparisonView({
|
|||||||
|
|
||||||
// Fetch comparison data
|
// Fetch comparison data
|
||||||
if (selectedSchools.length > 0) {
|
if (selectedSchools.length > 0) {
|
||||||
const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || '/api';
|
|
||||||
console.log('Fetching comparison data for URNs:', urns);
|
console.log('Fetching comparison data for URNs:', urns);
|
||||||
fetch(`${apiBaseUrl}/compare?urns=${urns}`)
|
fetchComparison(urns, { cache: 'no-store' })
|
||||||
.then((res) => {
|
|
||||||
console.log('Comparison API response status:', res.status);
|
|
||||||
return res.json();
|
|
||||||
})
|
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
console.log('Comparison data received:', data);
|
console.log('Comparison data received:', data);
|
||||||
setComparisonData(data.comparison);
|
setComparisonData(data.comparison);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { useState, useMemo } from 'react';
|
|||||||
import { Modal } from './Modal';
|
import { Modal } from './Modal';
|
||||||
import { useComparison } from '@/hooks/useComparison';
|
import { useComparison } from '@/hooks/useComparison';
|
||||||
import { debounce } from '@/lib/utils';
|
import { debounce } from '@/lib/utils';
|
||||||
|
import { fetchSchools } from '@/lib/api';
|
||||||
import type { School } from '@/lib/types';
|
import type { School } from '@/lib/types';
|
||||||
import styles from './SchoolSearchModal.module.css';
|
import styles from './SchoolSearchModal.module.css';
|
||||||
|
|
||||||
@@ -36,9 +37,7 @@ export function SchoolSearchModal({ isOpen, onClose }: SchoolSearchModalProps) {
|
|||||||
|
|
||||||
setIsSearching(true);
|
setIsSearching(true);
|
||||||
try {
|
try {
|
||||||
const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || '/api';
|
const data = await fetchSchools({ search: term, page_size: 10 }, { cache: 'no-store' });
|
||||||
const response = await fetch(`${apiBaseUrl}/schools?search=${encodeURIComponent(term)}&page_size=10`);
|
|
||||||
const data = await response.json();
|
|
||||||
setResults(data.schools || []);
|
setResults(data.schools || []);
|
||||||
setHasSearched(true);
|
setHasSearched(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -272,7 +272,9 @@ export async function fetchDataInfo(
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export async function fetcher<T>(url: string): Promise<T> {
|
export async function fetcher<T>(url: string): Promise<T> {
|
||||||
const fullUrl = url.startsWith('http') ? url : `${API_BASE_URL}${url.startsWith('/') ? url.slice(4) : url}`;
|
// If it's already a full URL, use it directly
|
||||||
|
// Otherwise, prepend the API_BASE_URL
|
||||||
|
const fullUrl = url.startsWith('http') ? url : `${API_BASE_URL}${url.startsWith('/') ? url : `/${url}`}`;
|
||||||
|
|
||||||
const response = await fetch(fullUrl);
|
const response = await fetch(fullUrl);
|
||||||
return handleResponse<T>(response);
|
return handleResponse<T>(response);
|
||||||
|
|||||||
Reference in New Issue
Block a user