-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the movie frontend using Relay
- Loading branch information
1 parent
575e2a3
commit a1abe38
Showing
2 changed files
with
72 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,63 @@ | ||
import React from 'react'; | ||
import React, { Suspense } from 'react'; | ||
import { | ||
BrowserRouter as Router, | ||
Switch, | ||
Route, | ||
} from "react-router-dom"; | ||
|
||
import { Environment, Store, RecordSource, Network } from 'relay-runtime'; | ||
import { RelayEnvironmentProvider } from 'react-relay/hooks'; | ||
|
||
import { Movie, Movies } from './Movie'; | ||
|
||
|
||
const relayEnvironment = new Environment({ | ||
// TODO What are thooose?! | ||
store: new Store(new RecordSource()), | ||
network: Network.create(({ text: query }, variables) => | ||
fetch('/graphql', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ query, variables }), | ||
}).then(response => response.json())), | ||
}); | ||
|
||
class ErrorBoundary extends React.PureComponent<unknown, { error?: boolean }> { | ||
constructor(props: unknown) { | ||
super(props); | ||
this.state = {}; | ||
} | ||
|
||
static getDerivedStateFromError() { | ||
return { error: true }; | ||
} | ||
|
||
render() { | ||
if (this.state.error) { | ||
return <p>An error occured!</p>; | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export const App: React.FC = () => { | ||
return ( | ||
<Router> | ||
<Switch> | ||
<Route path="/movie/:id"> | ||
<Movie /> | ||
</Route> | ||
<Route path="/"> | ||
<Movies /> | ||
</Route> | ||
</Switch> | ||
</Router> | ||
<RelayEnvironmentProvider environment={relayEnvironment}> | ||
<ErrorBoundary> | ||
<Suspense fallback="Loading ..."> | ||
<Router> | ||
<Switch> | ||
<Route path="/movie/:id"> | ||
<Movie /> | ||
</Route> | ||
<Route path="/"> | ||
<Movies /> | ||
</Route> | ||
</Switch> | ||
</Router> | ||
</Suspense> | ||
</ErrorBoundary> | ||
</RelayEnvironmentProvider> | ||
); | ||
}; |
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