-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from jpedroh/develop
Develop
- Loading branch information
Showing
8 changed files
with
202 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import Flight from '@mach/common' | ||
import { createContext, FC, useCallback, useReducer } from 'react' | ||
import getFlights, { GetFlightsQuery } from '../actions/get-flights' | ||
|
||
type FlightsState = { | ||
data: { items: Flight[]; count: number; query?: GetFlightsQuery } | ||
loading: boolean | ||
error: Error | null | ||
} | ||
|
||
type FlightsContextData = { | ||
state: FlightsState | ||
loadFlights: (params: GetFlightsQuery) => Promise<void> | ||
} | ||
|
||
const FlightsContext = createContext({} as FlightsContextData) | ||
|
||
const initialState = { | ||
data: { items: [], count: 0, query: null }, | ||
loading: false, | ||
error: null | ||
} | ||
|
||
const reducer = ( | ||
state: FlightsState, | ||
action: { | ||
type: FlightsActions | ||
payload?: any | ||
} | ||
): typeof state => { | ||
switch (action.type) { | ||
case 'LOAD_FLIGHTS_INIT': | ||
return { | ||
...state, | ||
data: { ...state.data, query: action.payload }, | ||
loading: true, | ||
error: null | ||
} | ||
case 'LOAD_FLIGHTS_SUCCESS': | ||
return { | ||
...state, | ||
data: { ...state.data, ...action.payload }, | ||
loading: false, | ||
error: null | ||
} | ||
case 'LOAD_FLIGHTS_ERROR': | ||
return { | ||
...state, | ||
data: { items: [], count: 0 }, | ||
loading: false, | ||
error: action.payload | ||
} | ||
default: | ||
throw new Error('Invalid operation provided') | ||
} | ||
} | ||
|
||
type FlightsActions = | ||
| 'LOAD_FLIGHTS_INIT' | ||
| 'LOAD_FLIGHTS_SUCCESS' | ||
| 'LOAD_FLIGHTS_ERROR' | ||
|
||
const FlightsProvider: FC = ({ children }) => { | ||
const [state, dispatch] = useReducer(reducer, initialState) | ||
|
||
const loadFlights = useCallback(async (params: GetFlightsQuery) => { | ||
try { | ||
dispatch({ type: 'LOAD_FLIGHTS_INIT', payload: params }) | ||
const payload = await getFlights(params) | ||
dispatch({ type: 'LOAD_FLIGHTS_SUCCESS', payload }) | ||
} catch (error) { | ||
dispatch({ type: 'LOAD_FLIGHTS_ERROR', payload: error }) | ||
} | ||
}, []) | ||
|
||
return ( | ||
<FlightsContext.Provider value={{ state, loadFlights }}> | ||
{children} | ||
</FlightsContext.Provider> | ||
) | ||
} | ||
|
||
export { FlightsContext, FlightsProvider } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import React, { useEffect } from 'react' | ||
import 'bootstrap/dist/css/bootstrap.min.css' | ||
import { AppProps } from 'next/app' | ||
import React, { useEffect } from 'react' | ||
import { FlightsProvider } from '../contexts/FlightsContext' | ||
|
||
const App: React.FC<AppProps> = ({ Component, pageProps }) => { | ||
useEffect(() => { | ||
document.title = 'Mach - Flight Planning' | ||
}) | ||
return <Component {...pageProps} /> | ||
}, []) | ||
return ( | ||
<FlightsProvider> | ||
<Component {...pageProps} /> | ||
</FlightsProvider> | ||
) | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { GetFlightsQuery } from '../actions/get-flights' | ||
|
||
export default function sanitizeParameters(params: Partial<GetFlightsQuery>) { | ||
return Object.fromEntries( | ||
Object.entries(params).filter(([_, v]) => v != '') | ||
) as GetFlightsQuery | ||
} |
Oops, something went wrong.