Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
safetcut-app
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
atichat
safetcut-app
Commits
a0ea765d
Commit
a0ea765d
authored
Oct 02, 2019
by
HaOuiha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix bug unsubsrciption
parent
388add81
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
90 deletions
+54
-90
components/MeterCard.js
+10
-32
utils/apollo/apollo.js
+44
-58
No files found.
components/MeterCard.js
View file @
a0ea765d
...
@@ -5,41 +5,21 @@ import { BaseButton, TouchableOpacity } from 'react-native-gesture-handler';
...
@@ -5,41 +5,21 @@ import { BaseButton, TouchableOpacity } from 'react-native-gesture-handler';
import
{
color
,
theme
}
from
'../constants/Styles'
;
import
{
color
,
theme
}
from
'../constants/Styles'
;
import
{
graphql
}
from
'@apollo/react-hoc'
;
import
{
graphql
}
from
'@apollo/react-hoc'
;
import
{
READ_SHADOW
,
SUBSCRIBE_SHADOW
}
from
'../utils/apollo/apollo'
;
import
{
READ_SHADOW
,
SUBSCRIBE_SHADOW
}
from
'../utils/apollo/apollo'
;
import
{
withNavigationFocus
}
from
'react-navigation'
;
// const MeterCardWithSubscription = props => {
const
MeterCard
=
({
item
,
onPressEachCard
,
data
,
isFocused
,
...
rest
})
=>
{
// console.log(props);
let
unsubscribeShadow
=
null
;
// return (
// <Query query={READ_SHADOW} variables={{ deviceid: props.item.deviceId }}>
// {({ subscribeToMore, ...result }) => (
// <MeterCard
// {...props}
// {...result}
// subscribeToNewShadow={() =>
// subscribeToMore({
// document: SUBSCRIBE_SHADOW,
// variables: { deviceid: props.item.deviceId },
// updateQuery: (prev, { subscriptionData }) => {
// return subscriptionData;
// },
// })
// }
// />
// )}
// </Query>
// );
// };
const
MeterCard
=
({
item
,
onPressEachCard
,
data
,
...
rest
})
=>
{
const
[
breakerStatus
,
setBreakerStatus
]
=
useState
(
item
.
breakerStatus
);
const
[
breakerStatus
,
setBreakerStatus
]
=
useState
(
item
.
breakerStatus
);
useEffect
(()
=>
{
useEffect
(()
=>
{
subscribeToNewShadow
(
data
.
subscribeToMore
);
subscribeToNewShadow
(
data
.
subscribeToMore
);
isFocused
===
false
&&
unsubscribeShadow
();
return
()
=>
{
return
()
=>
{
subscribeToNewShadow
.
unsubscribe
();
unsubscribeShadow
();
};
};
},
[]);
},
[
isFocused
]);
const
subscribeToNewShadow
=
subscribeToMore
=>
{
const
subscribeToNewShadow
=
subscribeToMore
=>
{
subscribeToMore
({
unsubscribeShadow
=
subscribeToMore
({
document
:
SUBSCRIBE_SHADOW
,
document
:
SUBSCRIBE_SHADOW
,
variables
:
{
deviceid
:
item
.
deviceId
},
variables
:
{
deviceid
:
item
.
deviceId
},
updateQuery
:
(
prev
,
{
subscriptionData
})
=>
{
updateQuery
:
(
prev
,
{
subscriptionData
})
=>
{
...
@@ -100,13 +80,11 @@ const MeterCard = ({ item, onPressEachCard, data, ...rest }) => {
...
@@ -100,13 +80,11 @@ const MeterCard = ({ item, onPressEachCard, data, ...rest }) => {
);
);
};
};
const
withShadow
=
graphql
(
READ_SHADOW
,
{
const
WithSubscription
=
graphql
(
READ_SHADOW
,
{
options
:
props
=>
({
options
:
props
=>
({
variables
:
{
deviceid
:
props
.
deviceId
}
}),
variables
:
{
deviceid
:
props
.
deviceId
},
}),
});
});
export
default
withShadow
(
MeterCard
);
export
default
WithSubscription
(
withNavigationFocus
(
MeterCard
)
);
const
styles
=
StyleSheet
.
create
({
const
styles
=
StyleSheet
.
create
({
meterOn
:
{
meterOn
:
{
...
...
utils/apollo/apollo.js
View file @
a0ea765d
...
@@ -13,26 +13,24 @@ import AsyncStorage from '@react-native-community/async-storage';
...
@@ -13,26 +13,24 @@ import AsyncStorage from '@react-native-community/async-storage';
const
BASE_URL
=
'gqlv2.nexpie.io'
;
const
BASE_URL
=
'gqlv2.nexpie.io'
;
let
T
=
null
;
//
let T = null;
const
getToken
=
async
()
=>
(
T
=
await
AsyncStorage
.
getItem
(
'token'
));
//
const getToken = async () => (T = await AsyncStorage.getItem('token'));
getToken
();
//
getToken();
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
const
wsLink
=
new
WebSocketLink
({
const
wsLink
=
new
WebSocketLink
({
uri
:
`wss://
${
BASE_URL
}
/graphql`
,
uri
:
`wss://
${
BASE_URL
}
/graphql`
,
options
:
{
options
:
{
reconnect
:
true
,
reconnect
:
true
,
connectionParams
:
{
//
connectionParams: {
headers
:
{
Authorization
:
`Bearer
${
T
}
`
},
//
headers: { Authorization: `Bearer ${T}` },
},
//
},
},
},
});
});
const
setAuthorizationLink
=
setContext
(
async
()
=>
{
const
setAuthorizationLink
=
setContext
(
async
()
=>
{
const
token
=
await
AsyncStorage
.
getItem
(
'token'
);
const
token
=
await
AsyncStorage
.
getItem
(
'token'
);
return
{
return
{
headers
:
{
authorization
:
token
?
`Bearer
${
token
}
`
:
''
}
};
headers
:
{
authorization
:
token
?
`Bearer
${
token
}
`
:
''
},
};
});
});
const
httpLink
=
new
HttpLink
({
uri
:
`https://
${
BASE_URL
}
`
});
const
httpLink
=
new
HttpLink
({
uri
:
`https://
${
BASE_URL
}
`
});
...
@@ -70,6 +68,43 @@ const apolloMutation = async (mutation, variables) => {
...
@@ -70,6 +68,43 @@ const apolloMutation = async (mutation, variables) => {
});
});
};
};
export
{
client
,
apolloQuery
,
apolloMutation
};
/*----------------------------------------------------------------- */
const
READ_SHADOW
=
gql
`
query($deviceid: String!) {
shadow(deviceid: $deviceid) {
data
}
}
`
;
const
WRITE_SHADOW
=
gql
`
mutation($deviceid: String!, $data: JSON!) {
writeShadow(deviceid: $deviceid, value: $data) {
data
}
}
`
;
const
SUBSCRIBE_SHADOW
=
gql
`
subscription($deviceid: String!) {
shadowUpdated(deviceid: $deviceid) {
data
}
}
`
;
const
QUERY_DEVICE_BY_TAG
=
gql
`
query($tag: String!) {
device(tag: $tag) {
deviceid
}
}
`
;
export
{
READ_SHADOW
,
WRITE_SHADOW
,
QUERY_DEVICE_BY_TAG
,
SUBSCRIBE_SHADOW
};
// const apolloSubscription = async (query, queryVariables, subscribe, subscribeVariables) => {
// const apolloSubscription = async (query, queryVariables, subscribe, subscribeVariables) => {
// const queryData = client.watchQuery({
// const queryData = client.watchQuery({
// query: query,
// query: query,
...
@@ -114,52 +149,3 @@ const apolloMutation = async (mutation, variables) => {
...
@@ -114,52 +149,3 @@ const apolloMutation = async (mutation, variables) => {
// return tempData;
// return tempData;
// };
// };
export
{
client
,
apolloQuery
,
apolloMutation
};
/*----------------------------------------------------------------- */
const
READ_SHADOW
=
gql
`
query($deviceid: String!) {
shadow(deviceid: $deviceid) {
data
}
}
`
;
const
WRITE_SHADOW
=
gql
`
mutation($deviceid: String!, $data: JSON!) {
writeShadow(deviceid: $deviceid, value: $data) {
data
}
}
`
;
const
SUBSCRIBE_SHADOW
=
gql
`
subscription($deviceid: String!) {
shadowUpdated(deviceid: $deviceid) {
data
}
}
`
;
const
QUERY_DEVICE_BY_TAG
=
gql
`
query($tag: String!) {
device(tag: $tag) {
deviceid
}
}
`
;
export
{
READ_SHADOW
,
WRITE_SHADOW
,
QUERY_DEVICE_BY_TAG
,
SUBSCRIBE_SHADOW
};
// const client = new ApolloClient({
// link,
// // request: async operation => {
// // const token = await AsyncStorage.getItem('token');
// // operation.setContext({
// // headers: {
// // authorization: token ? `Bearer ${token}` : '',
// // },
// // });
// // },
// });
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment