Skip to content

Commit

Permalink
Refactor to make eslint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
a-limyr committed Jan 22, 2025
1 parent bc9edc2 commit 64e6e41
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
49 changes: 2 additions & 47 deletions client/src/components/SearchInput/TripSchemaContext.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,10 @@
import React, { createContext, useEffect, useState } from 'react';
import { createContext } from 'react';
import type { TripArgsRepresentation } from './useTripArgs';
import { fetchTripArgs } from './useTripArgs';

interface TripSchemaContextValue {
export interface TripSchemaContextValue {
tripArgs: TripArgsRepresentation | null;
loading: boolean;
error: string | null;
}

export const TripSchemaContext = createContext<TripSchemaContextValue | undefined>(undefined);

interface TripSchemaProviderProps {
endpoint: string;
children: React.ReactNode;
}

export function TripSchemaProvider({ endpoint, children }: TripSchemaProviderProps) {
const [tripArgs, setTripArgs] = useState<TripArgsRepresentation | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
let isMounted = true;

async function loadSchema() {
setLoading(true);
setError(null);
try {
const result = await fetchTripArgs(endpoint);
if (isMounted) {
setTripArgs(result);
}
} catch (err) {
console.error('Error loading trip arguments:', err);
if (isMounted) {
setError('Failed to load trip schema');
}
} finally {
if (isMounted) {
setLoading(false);
}
}
}

loadSchema();
return () => {
isMounted = false;
};
}, [endpoint]);

const value: TripSchemaContextValue = { tripArgs, loading, error };

return <TripSchemaContext.Provider value={value}>{children}</TripSchemaContext.Provider>;
}
47 changes: 47 additions & 0 deletions client/src/components/SearchInput/TripSchemaProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useEffect, useState } from 'react';
import { TripSchemaContext, TripSchemaContextValue } from './TripSchemaContext';
import { fetchTripArgs, TripArgsRepresentation } from './useTripArgs';

interface TripSchemaProviderProps {
endpoint: string;
children: React.ReactNode;
}

export function TripSchemaProvider({ endpoint, children }: TripSchemaProviderProps) {
const [tripArgs, setTripArgs] = useState<TripArgsRepresentation | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
let isMounted = true;

async function loadSchema() {
setLoading(true);
setError(null);
try {
const result = await fetchTripArgs(endpoint);
if (isMounted) {
setTripArgs(result);
}
} catch (err) {
console.error('Error loading trip arguments:', err);
if (isMounted) {
setError('Failed to load trip schema');
}
} finally {
if (isMounted) {
setLoading(false);
}
}
}

loadSchema();
return () => {
isMounted = false;
};
}, [endpoint]);

const value: TripSchemaContextValue = { tripArgs, loading, error };

return <TripSchemaContext.Provider value={value}>{children}</TripSchemaContext.Provider>;
}
7 changes: 5 additions & 2 deletions client/src/screens/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { InputFieldsSection } from '../components/SearchBar/InputFieldsSection.t
import TripQueryArguments from '../components/SearchInput/TripQueryArguments.tsx';
import Sidebar from '../components/SearchInput/Sidebar.tsx';
import ViewArgumentsRaw from '../components/SearchInput/ViewArgumentsRaw.tsx';
import { TripSchemaProvider } from '../components/SearchInput/TripSchemaContext.tsx';
import { TripSchemaProvider } from '../components/SearchInput/TripSchemaProvider.tsx';
import { getApiUrl } from '../util/getApiUrl.ts';

export function App() {
Expand Down Expand Up @@ -50,7 +50,10 @@ export function App() {
setTripQueryVariables={setTripQueryVariables}
></TripQueryArguments>
</TripSchemaProvider>
<ViewArgumentsRaw tripQueryVariables={tripQueryVariables} setTripQueryVariables={setTripQueryVariables}></ViewArgumentsRaw>
<ViewArgumentsRaw
tripQueryVariables={tripQueryVariables}
setTripQueryVariables={setTripQueryVariables}
></ViewArgumentsRaw>
</Sidebar>
</div>
<div className="box map-section">
Expand Down

0 comments on commit 64e6e41

Please sign in to comment.