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,35 +24,53 @@ export const errorAction = error => ({
error,
});
export const getCurrentSelectedData = deviceId => async dispatch => {
try {
dispatch(loadingAction(true));
const currentSelectedDevice = await fireStore
.collection('device')
.doc(deviceId)
.get();
export const getCurrentSelectedShadowAction = shadow => ({
type: GET_CURRENT_SELECTED_SHADOW,
shadow,
});
const currentSelectedDeviceData = await currentSelectedDevice.data();
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);
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);
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));
const currentSelectedDevice = await fireStore
.collection('device')
.doc(deviceId)
.get();
const currentSelectedDeviceData = await currentSelectedDevice.data();
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,6 +54,7 @@ class SmartMeterScreen extends PureComponent {
const { allMainDeviceInfo, isLoading, error } = this.props;
return !error ? (
<>
<FlatList
refreshControl={
<RefreshControl
......@@ -83,6 +83,7 @@ class SmartMeterScreen extends PureComponent {
)}
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