Commit 11217db6 by HaOuiha

update

parent 9d29eb67
......@@ -7,6 +7,8 @@ export const GET_CURRENT_SELECTED_DATA = 'GET_CURRENT_SELECTED_DATA';
export const GET_CURRENT_SELECTED_DATA_LOADING = 'GET_CURRENT_SELECTED_DATA_LOADING';
export const GET_CURRENT_SELECTED_DATA_ERROR = 'GET_CURRENT_SELECTED_DATA_ERROR';
export const GET_CURRENT_SELECTED_SHADOW = 'GET_CURRENT_SELECTED_SHADOW';
export const getCurrentSelectedDataAction = currentSelectedData => ({
type: GET_CURRENT_SELECTED_DATA,
currentSelectedData,
......@@ -22,6 +24,40 @@ export const errorAction = error => ({
error,
});
export const getCurrentSelectedShadowAction = shadow => ({
type: GET_CURRENT_SELECTED_SHADOW,
shadow,
});
export const getCurrentSelectedShadow = deviceId => async (dispatch, getState) => {
const { currentSelectedDeviceReducer } = getState();
const { currentSelectedData } = currentSelectedDeviceReducer;
const selectedDeviceId = currentSelectedData.deviceId;
const URL = `${baseURL}/shadow/${deviceId || selectedDeviceId}`;
const idToken = await app.auth().currentUser.getIdToken(true);
const options = {
headers: { Authorization: `Token ${idToken}` },
url: URL,
};
try {
const getShadow = async () => {
try {
const response = await axios(options);
return response.data.shadow.value;
} catch (error) {
alert(error);
return null;
}
};
const shadowData = await getShadow();
dispatch(getCurrentSelectedShadowAction(shadowData));
} catch (error) {
dispatch(errorAction(error.message || error || 'Error'));
}
};
export const getCurrentSelectedData = deviceId => async dispatch => {
try {
dispatch(loadingAction(true));
......@@ -31,26 +67,10 @@ export const getCurrentSelectedData = deviceId => async dispatch => {
.get();
const currentSelectedDeviceData = await currentSelectedDevice.data();
const idToken = await app.auth().currentUser.getIdToken(true);
console.log(idToken);
const URL = `${baseURL}/shadow/${deviceId}`;
const options = {
headers: { Authorization: `Token ${idToken}` },
url: URL,
};
console.log(URL);
try {
const getShadow = await axios(options);
console.log(getShadow);
} catch (error) {
alert(error);
}
const currentSelectedDeviceDataMoreInfo = { ...currentSelectedDeviceData, deviceId: currentSelectedDevice.id };
dispatch(loadingAction(false));
return dispatch(getCurrentSelectedDataAction(currentSelectedDeviceDataMoreInfo));
dispatch(getCurrentSelectedDataAction(currentSelectedDeviceDataMoreInfo));
} catch (error) {
dispatch(errorAction(error.message || error || 'Error'));
dispatch(loadingAction(false));
......@@ -61,7 +81,7 @@ export const updateDetail = (type, value) => async (dispatch, getState) => {
const { currentSelectedDeviceReducer } = getState();
const { currentSelectedData } = currentSelectedDeviceReducer;
const selectedDeviceId = currentSelectedData.deviceId;
const { deviceId, isSharing, ...currentSelectedMainData } = currentSelectedData;
const { deviceId, ...currentSelectedMainData } = currentSelectedData;
const getNewData = () => {
switch (type) {
......
......@@ -2,10 +2,12 @@ import {
GET_CURRENT_SELECTED_DATA,
GET_CURRENT_SELECTED_DATA_LOADING,
GET_CURRENT_SELECTED_DATA_ERROR,
GET_CURRENT_SELECTED_SHADOW,
} from '../actions/currentSelectedAction';
const initState = {
currentSelectedData: {},
shadow: null,
isLoading: true,
error: null,
};
......@@ -14,6 +16,8 @@ const currentSelectedDeviceReducer = (state = initState, action) => {
switch (action.type) {
case GET_CURRENT_SELECTED_DATA:
return { ...state, currentSelectedData: action.currentSelectedData };
case GET_CURRENT_SELECTED_SHADOW:
return { ...state, shadow: action.shadow };
case GET_CURRENT_SELECTED_DATA_LOADING:
return { ...state, isLoading: action.isLoading };
case GET_CURRENT_SELECTED_DATA_ERROR:
......
......@@ -4,7 +4,7 @@ import { color, theme } from '../../../constants/Styles';
import { FlatList, StyleSheet, ActivityIndicator, RefreshControl, ScrollView, Platform } from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';
import { connect } from 'react-redux';
import { getCurrentSelectedData } from '../../../reduxStore/actions/currentSelectedAction';
import { getCurrentSelectedData, getCurrentSelectedShadow } from '../../../reduxStore/actions/currentSelectedAction';
import { getTimers } from '../../../reduxStore/actions/timersAction';
import { isIphoneX } from '../../../utils/isPhoneX';
......@@ -48,6 +48,7 @@ class SmartMeterDetailScreen extends Component {
getData = async () => {
const deviceId = this.props.navigation.getParam('deviceId');
await this.props.getCurrentSelectedData(deviceId);
await this.props.getCurrentSelectedShadow(deviceId);
this.props.getTimers();
};
......@@ -68,6 +69,10 @@ class SmartMeterDetailScreen extends Component {
}
};
componentWillMount = () => {
this._mcbLinksListMounted = false;
};
setMainCardData = async () => {
const { currentSelectedData } = this.props;
this.setState({
......@@ -312,6 +317,7 @@ const mapStateToProps = state => ({
const mapDispatchToProps = {
getCurrentSelectedData,
getCurrentSelectedShadow,
getTimers,
};
......
......@@ -7,7 +7,6 @@ import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import IoniconsHeaderButton from '../../../components/IoniconsHeaderButton';
import { isIphoneX } from '../../../utils/isPhoneX';
import { getAllMainDeviceInfo } from '../../../reduxStore/actions/allMainDeviceAction';
import { connect } from 'react-redux';
import { SearchBar } from 'react-native-elements';
......@@ -55,34 +54,36 @@ class SmartMeterScreen extends PureComponent {
const { allMainDeviceInfo, isLoading, error } = this.props;
return !error ? (
<FlatList
refreshControl={
<RefreshControl
refreshing={isLoading}
onRefresh={this.getData}
title="Pull to refresh"
tintColor={color.primary}
titleColor={color.darkGrey}
colors={[color.primary]}
/>
}
style={[theme.container]}
contentContainerStyle={[theme.containerWithPadding, { paddingBottom: isIphoneX() ? 90 : 55 }]} //iPhoneX BottomSpace = 34
data={allMainDeviceInfo}
keyExtractor={(item, index) => item.deviceId}
ItemSeparatorComponent={() => <View style={theme.mt1} />}
ListEmptyComponent={() => (
<View>
<Text style={[theme.normalText, { marginTop: 20 }]}>
{isLoading ? '' : this._isMounted ? 'No Device Connected' : ''}
</Text>
</View>
)}
ListHeaderComponent={() => (
<Text style={[theme.smallTitle, theme.textDark, { marginBottom: 10 }]}>Smart Device</Text>
)}
renderItem={(item, index) => this.renderItem(item)}
/>
<>
<FlatList
refreshControl={
<RefreshControl
refreshing={isLoading}
onRefresh={this.getData}
title="Pull to refresh"
tintColor={color.primary}
titleColor={color.darkGrey}
colors={[color.primary]}
/>
}
style={[theme.container]}
contentContainerStyle={[theme.containerWithPadding, { paddingBottom: isIphoneX() ? 90 : 55 }]} //iPhoneX BottomSpace = 34
data={allMainDeviceInfo}
keyExtractor={(item, index) => item.deviceId}
ItemSeparatorComponent={() => <View style={theme.mt1} />}
ListEmptyComponent={() => (
<View>
<Text style={[theme.normalText, { marginTop: 20 }]}>
{isLoading ? '' : this._isMounted ? 'No Device Connected' : ''}
</Text>
</View>
)}
ListHeaderComponent={() => (
<Text style={[theme.smallTitle, theme.textDark, { marginBottom: 10 }]}>Smart Device</Text>
)}
renderItem={(item, index) => this.renderItem(item)}
/>
</>
) : (
<View>
<Text>{error}</Text>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment