Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
feat: add unstyled react native example app
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleyzissou authored and wtrocki committed Mar 25, 2020
1 parent a83f5b8 commit 113f4e8
Show file tree
Hide file tree
Showing 33 changed files with 904 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/reactnative/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[android]
target = Google Inc.:Google APIs:23

[maven_repositories]
central = https://repo1.maven.org/maven2
4 changes: 4 additions & 0 deletions examples/reactnative/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: '@react-native-community',
};
75 changes: 75 additions & 0 deletions examples/reactnative/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[ignore]
; We fork some components by platform
.*/*[.]android.js

; Ignore "BUCK" generated dirs
<PROJECT_ROOT>/\.buckd/

; Ignore polyfills
node_modules/react-native/Libraries/polyfills/.*

; These should not be required directly
; require from fbjs/lib instead: require('fbjs/lib/warning')
node_modules/warning/.*

; Flow doesn't support platforms
.*/Libraries/Utilities/LoadingView.js

[untyped]
.*/node_modules/@react-native-community/cli/.*/.*

[include]

[libs]
node_modules/react-native/Libraries/react-native/react-native-interface.js
node_modules/react-native/flow/

[options]
emoji=true

esproposal.optional_chaining=enable
esproposal.nullish_coalescing=enable

module.file_ext=.js
module.file_ext=.json
module.file_ext=.ios.js

munge_underscores=true

module.name_mapper='^react-native$' -> '<PROJECT_ROOT>/node_modules/react-native/Libraries/react-native/react-native-implementation'
module.name_mapper='^react-native/\(.*\)$' -> '<PROJECT_ROOT>/node_modules/react-native/\1'
module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> '<PROJECT_ROOT>/node_modules/react-native/Libraries/Image/RelativeImageStub'

suppress_type=$FlowIssue
suppress_type=$FlowFixMe
suppress_type=$FlowFixMeProps
suppress_type=$FlowFixMeState

suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\)
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\)?:? #[0-9]+
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError

[lints]
sketchy-null-number=warn
sketchy-null-mixed=warn
sketchy-number=warn
untyped-type-import=warn
nonstrict-import=warn
deprecated-type=warn
unsafe-getters-setters=warn
inexact-spread=warn
unnecessary-invariant=warn
signature-verification-failure=warn
deprecated-utility=error

[strict]
deprecated-type
nonstrict-import
sketchy-null
unclear-type
unsafe-getters-setters
untyped-import
untyped-type-import

[version]
^0.105.0
1 change: 1 addition & 0 deletions examples/reactnative/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
61 changes: 61 additions & 0 deletions examples/reactnative/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# OSX
#
.DS_Store

# Xcode
#
ios/
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# Android/IntelliJ
#
android/
build/
.idea
.gradle
local.properties
*.iml

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore
!debug.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

*/fastlane/report.xml
*/fastlane/Preview.html
*/fastlane/screenshots

# Bundle artifact
*.jsbundle

# CocoaPods
/ios/Pods/
6 changes: 6 additions & 0 deletions examples/reactnative/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
bracketSpacing: false,
jsxBracketSameLine: true,
singleQuote: true,
trailingComma: 'all',
};
1 change: 1 addition & 0 deletions examples/reactnative/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
64 changes: 64 additions & 0 deletions examples/reactnative/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Offix - React Native Todo Example App

This example demonstrates how to get started using Offix in a React Native project. The app is a simple
todo app making use of the `offix-client` and can be used as launch pad to getting started
with Offix and make use of the features in the library.

## Getting started

Follow the React Native [getting started](https://reactnative.dev/docs/getting-started) guide, if you have not setup React Native already.

Note: this example app uses the React Native CLI and not Expo.

### Setting up a server

For simplicity, a Graphback runtime server is available in the server folder in the examples directory. Follow the gettings started instructions there to get started quickly.

Alternatively, you can implement your own backend server.

### Starting the client

Next, configure the GraphQL server address in the `src/clientConfig.js` file:

```
...
// Note for android build, you will need
// the IP address for localhost,
// usually 192.168.1.10:<PORT_NUMBER>
const wsLink = new WebSocketLink({
uri: 'ws://<YOUR-SERVER-ADDRESS-HERE>',
...
});
// Note for android build, you will need
// the IP address for localhost,
// usually 192.168.1.10:<PORT_NUMBER>
const httpLink = new HttpLink({
uri: 'http://<YOUR-SERVER-ADDRESS-HERE>',
});
...
```

### Starting the client

Lastly, run the following commands from the React example folder.

```
yarn
yarn start
```

To run ios:
```
yarn ios
```

Or for Android:
```
yarn android
```
14 changes: 14 additions & 0 deletions examples/reactnative/__tests__/App-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @format
*/

import 'react-native';
import React from 'react';
import App from '../App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
renderer.create(<App />);
});
3 changes: 3 additions & 0 deletions examples/reactnative/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
9 changes: 9 additions & 0 deletions examples/reactnative/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @format
*/

import {AppRegistry} from 'react-native';
import { Offix } from './src/Offix';
import { name as appName } from './src/app.json';

AppRegistry.registerComponent(appName, () => Offix);
17 changes: 17 additions & 0 deletions examples/reactnative/metro.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/

module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
39 changes: 39 additions & 0 deletions examples/reactnative/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "rnative",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"@react-native-community/async-storage": "^1.8.1",
"@react-native-community/netinfo": "^5.6.2",
"apollo-client": "^2.6.8",
"apollo-link-ws": "^1.0.19",
"graphql": "^14.6.0",
"graphql-tag": "^2.10.3",
"offix-client": "^0.15.0",
"react": "16.9.0",
"react-apollo": "^3.1.3",
"react-native": "0.61.5",
"react-offix-hooks": "^0.15.0",
"subscriptions-transport-ws": "^0.9.16"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/runtime": "^7.6.2",
"@react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.9.0",
"eslint": "^6.5.1",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
51 changes: 51 additions & 0 deletions examples/reactnative/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React, { useState } from 'react';
import { useQuery } from '@apollo/react-hooks';
import { useOfflineMutation, useNetworkStatus } from 'react-offix-hooks';
import { AddTodo, TodoList, TodoModal, Loading, Error } from './components';
import { GET_TODOS, ADD_TODO } from './gql/queries';
import { mutateOptions } from './helpers';
import { View, Text, Button } from 'react-native';

const App = () => {
const { loading, error, data, subscribeToMore } = useQuery(GET_TODOS);
const [addTodo] = useOfflineMutation(ADD_TODO, mutateOptions.add);
const [modalActive, setModalActive] = useState(false);

const isOnline = useNetworkStatus();

const toggleModal = () => {
setModalActive(!modalActive);
};

if (loading) return <Loading />;

if (error) return <Error error={error} />;

return (
<View>
<View>
<Text>OFFIX TODO</Text>
<Text>A simple todo app using offix & graphback</Text>
<Text>Network status: {(isOnline) ? 'Online' : 'Offline'}</Text>
</View>

<TodoModal
title="Create a task"
subtitle=""
active={modalActive}
close={toggleModal}
Component={() => <AddTodo addTodo={addTodo} cancel={toggleModal} />}
/>

<View>
<Button title="Add" onPress={toggleModal} />
</View>

<View>
<TodoList todos={data.findAllTodos} subscribeToMore={subscribeToMore} />
</View>
</View>
);
};

export default App;
29 changes: 29 additions & 0 deletions examples/reactnative/src/Offix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useState, useEffect } from 'react';
import { ApolloOfflineClient } from 'offix-client';
import { ApolloOfflineProvider } from 'react-offix-hooks';
import { ApolloProvider } from '@apollo/react-hooks';
import App from './App';
import { Loading } from './components';
import { clientConfig } from './clientConfig';

const client = new ApolloOfflineClient(clientConfig);

export const Offix = () => {
const [initialized, setInitialized] = useState(false);

useEffect(() => {
client.init().then(() => setInitialized(true));
}, []);

// If client is still initializing,
// display loading screen
if (!initialized) return <Loading />;

return (
<ApolloOfflineProvider client={client}>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</ApolloOfflineProvider>
);
};
Loading

0 comments on commit 113f4e8

Please sign in to comment.