Skip to content

Commit

Permalink
fix(network): add start method to handle panic in account
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Nov 5, 2018
1 parent a7b9dc2 commit efb2bb5
Show file tree
Hide file tree
Showing 21 changed files with 501 additions and 227 deletions.
41 changes: 27 additions & 14 deletions client/react-native/common/components/App.js
@@ -1,6 +1,11 @@
import React, { PureComponent } from 'react'
import Screens from './Screens'
import { NativeModules, Platform, ActivityIndicator, Linking } from 'react-native'
import {
NativeModules,
Platform,
ActivityIndicator,
Linking,
} from 'react-native'
import { Flex } from './Library'
import { subscriptions } from '../graphql'
import { SafeAreaView, NavigationActions } from 'react-navigation'
Expand All @@ -18,11 +23,7 @@ export default class App extends PureComponent {
async componentDidMount () {
try {
await CoreModule.start()
} catch (e) {
console.warn(e)
}

try {
await CoreModule.getPort()
subscriptions.eventStream.subscribe({
iterator: function * () {
try {
Expand Down Expand Up @@ -52,11 +53,13 @@ export default class App extends PureComponent {
subscriptions.eventStream.dispose()
}

Linking.getInitialURL().then(url => {
if (url !== null) {
this.handleOpenURL({ url })
}
}).catch(() => {})
Linking.getInitialURL()
.then(url => {
if (url !== null) {
this.handleOpenURL({ url })
}
})
.catch(() => {})

if (this._handleOpenURL === undefined) {
this._handleOpenURL = this.handleOpenURL.bind(this)
Expand Down Expand Up @@ -86,14 +89,18 @@ export default class App extends PureComponent {

const urlParts = url.split('/')

if (urlParts.length === 3 && urlParts[0] === 'add-contact' && urlParts[1] === 'public-key') {
if (
urlParts.length === 3 &&
urlParts[0] === 'add-contact' &&
urlParts[1] === 'public-key'
) {
console.log('Adding new contact via public key')

this.navigation.dispatch(
NavigationActions.navigate({
routeName: 'modal/contacts/add/by-public-key',
params: { initialKey: urlParts[2] },
}),
})
)
}
}
Expand All @@ -107,7 +114,13 @@ export default class App extends PureComponent {
<ActivityIndicator size='large' />
</Flex.Rows>
)}
{success && <Screens ref={nav => { this.navigation = nav }} />}
{success && (
<Screens
ref={nav => {
this.navigation = nav
}}
/>
)}
{Platform.OS === 'ios' && <KeyboardSpacer />}
</SafeAreaView>
)
Expand Down
Expand Up @@ -3,31 +3,36 @@ import React, { PureComponent } from 'react'

import { Flex, Header, Menu, Screen, Text } from '../../../Library'
import { colors } from '../../../../constants'
import { queries } from '../../../../graphql'

const { CoreModule } = NativeModules

export default class List extends PureComponent {
static navigationOptions = ({ navigation }) => ({
header: navigation.getParam('restartDaemon') ? null : (
<Header
navigation={navigation}
title='Developer Tools'
titleIcon='terminal'
backBtn
/>
),
header:
navigation.getParam('restartDaemon') ||
navigation.getParam('panic') ? null : (
<Header
navigation={navigation}
title='Developer Tools'
titleIcon='terminal'
backBtn
/>
),
tabBarVisible: false,
})

state = {
restartDaemon: false,
panic: false,
}

restartDaemon = async () => {
this.props.navigation.setParams({ restartDaemon: true })
this.setState({ restartDaemon: true }, async () => {
try {
await CoreModule.restart()
await CoreModule.getPort()
this.props.navigation.setParams({
restartDaemon: false,
})
Expand All @@ -38,18 +43,35 @@ export default class List extends PureComponent {
})
}

panic = async () => {
this.props.navigation.setParams({ panic: true })
this.setState({ panic: true }, async () => {
try {
queries.Panic.fetch()
await CoreModule.getPort()
this.props.navigation.setParams({
panic: false,
})
this.setState({ panic: false })
} catch (err) {
console.error(err)
}
})
}

render () {
const { navigation } = this.props
const { restartDaemon } = this.state
if (restartDaemon) {
const { restartDaemon, panic } = this.state
if (restartDaemon || panic) {
return (
<Screen style={{ backgroundColor: colors.white }}>
<Flex.Rows align='center'>
<Flex.Cols align='end'>
<ActivityIndicator size='large' />
</Flex.Cols>
<Text center margin align='start'>
Daemon is restarting, please wait ...
{restartDaemon && 'Daemon is restarting, please wait ...'}
{panic && 'Daemon has been panicked, please wait ...'}
</Text>
</Flex.Rows>
</Screen>
Expand All @@ -70,6 +92,7 @@ export default class List extends PureComponent {
title='Restart daemon'
onPress={this.restartDaemon}
/>
<Menu.Item icon='alert-triangle' title='Panic' onPress={this.panic} />
<Menu.Item
icon='list'
title='List events'
Expand Down
16 changes: 16 additions & 0 deletions client/react-native/common/graphql/queries/Panic.js
@@ -0,0 +1,16 @@
import { fetchQuery, graphql } from 'react-relay'

import { environment } from '../../relay'

const query = graphql`
query PanicQuery {
Panic(T: true) {
T
}
}
`

export default {
...query,
fetch: (variables = {}) => fetchQuery(environment, query, variables),
}
1 change: 1 addition & 0 deletions client/react-native/common/graphql/queries/index.js
Expand Up @@ -4,3 +4,4 @@ export Contact from './Contact'
export DeviceInfos from './DeviceInfos'
export AppVersion from './AppVersion'
export EventList from './EventList'
export Panic from './Panic'
25 changes: 6 additions & 19 deletions client/react-native/common/relay/environment.js
@@ -1,8 +1,9 @@
import { Environment, Network, RecordSource, Store } from 'relay-runtime'
import { NativeModules, Platform } from 'react-native'
import { SubscriptionClient } from 'subscriptions-transport-ws'
import { Platform, NativeModules } from 'react-native'
import { installRelayDevTools } from 'relay-devtools'

import { Environment, Network, RecordSource, Store } from 'relay-runtime'

// eslint-disable-next-line
if (__DEV__) {
installRelayDevTools()
Expand All @@ -12,7 +13,7 @@ if (__DEV__) {
if (Platform.OS === 'web') {
const CoreModule = {
start: async () => {},
restart: async () => {},
restart: async () => console.warn('not implemented in web'),
getPort: async () => {
const url = new URL(window.location.href)
return url.searchParams.get('gql-port') || '8700'
Expand Down Expand Up @@ -64,26 +65,12 @@ let getIP = () =>
}
})

const getPortInterval = async resolve => {
try {
const port = await CoreModule.getPort()
resolve(port)
} catch (error) {
console.warn(error)
setTimeout(() => {
getPortInterval(resolve)
}, 1000)
}
}
const getPort = () => new Promise(resolve => getPortInterval(resolve))

getIP().then(console.log)
getPort().then(console.log)

export const fetchQuery = async (operation, variables) => {
try {
const response = await fetch(
`http://${await getIP()}:${await getPort()}/query`,
`http://${await getIP()}:${await CoreModule.getPort()}/query`,
{
method: 'POST',
headers: {
Expand All @@ -106,7 +93,7 @@ const setupSubscription = async (config, variables, cacheConfig, observer) => {
try {
const query = config.text
const subscriptionClient = new SubscriptionClient(
`ws://${await getIP()}:${await getPort()}/query`,
`ws://${await getIP()}:${await CoreModule.getPort()}/query`,
{
reconnect: true,
}
Expand Down
3 changes: 3 additions & 0 deletions client/react-native/common/schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit efb2bb5

Please sign in to comment.