diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js index e6e459e3dc2..241ea12acdc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js @@ -21,6 +21,7 @@ import AppCard from "./AppCard"; import {Col, message, notification, Row, Result, Skeleton} from "antd"; import axios from "axios"; import {withConfigContext} from "../../context/ConfigContext"; +import {handleApiError} from "../../js/Utils"; class AppList extends React.Component { constructor(props) { @@ -72,20 +73,7 @@ class AppList extends React.Component { } }).catch((error) => { - console.log(error.response); - if (error.hasOwnProperty("response") && error.response.status === 401) { - //todo display a popup with error - message.error('You are not logged in'); - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load apps.", - }); - } - + handleApiError(error,"Error occurred while trying to load apps."); this.setState({loading: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js index 0536e966cf9..aac62976b74 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js @@ -17,11 +17,12 @@ */ import React from "react"; -import {Row, Typography, Icon} from "antd"; +import {Row, Typography, Icon, notification} from "antd"; import StarRatings from "react-star-ratings"; import "./DetailedRating.css"; import axios from "axios"; import {withConfigContext} from "../../../context/ConfigContext"; +import {handleApiError} from "../../../js/Utils"; const { Text } = Typography; @@ -62,9 +63,7 @@ class DetailedRating extends React.Component{ } }).catch(function (error) { - if (error.response.status === 401) { - window.location.href = window.location.origin+'/store/login'; - } + handleApiError(error,"Error occurred while trying to load ratings."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js index 6d55b45361d..d2b45a191ce 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js @@ -27,6 +27,7 @@ import axios from "axios"; import AppInstallModal from "./install/AppInstallModal"; import CurrentUsersReview from "./review/CurrentUsersReview"; import {withConfigContext} from "../../../context/ConfigContext"; +import {handleApiError} from "../../../js/Utils"; const {Title, Text, Paragraph} = Typography; @@ -78,20 +79,7 @@ class ReleaseView extends React.Component { } }).catch((error) => { - if (error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - this.setState({ - loading: false, - visible: false - }); - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while installing the app.", - }); - } + handleApiError(error,"Error occurred while installing the app."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js index 2af6be7a74d..4b7f11bbd22 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js @@ -24,6 +24,7 @@ import TimeAgo from 'javascript-time-ago' // Load locale-specific relative date/time formatting rules. import en from 'javascript-time-ago/locale/en' import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const columns = [ @@ -166,20 +167,7 @@ class DeviceInstall extends React.Component { } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("status") && error.response.status === 401) { - //todo display a popop with error - message.error('You are not logged in'); - window.location.href = window.location.origin + '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load devices.", - }); - } - + handleApiError(error,"Error occurred while trying to load devices."); this.setState({loading: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js index a8af7482c9a..44024687a26 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js @@ -21,6 +21,7 @@ import {Typography, Select, Spin, message, notification, Button} from "antd"; import debounce from 'lodash.debounce'; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const {Option} = Select; @@ -64,19 +65,8 @@ class GroupInstall extends React.Component { this.setState({data, fetching: false}); } - }).catch((error) => { console.log(error); - if (error.hasOwnProperty("status") && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+'/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load groups.", - }); - } - + }).catch((error) => { + handleApiError(error,"Error occurred while trying to load groups."); this.setState({fetching: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js index 877fd3a4cb9..0f6da0f2d7a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js @@ -21,6 +21,7 @@ import {Typography, Select, Spin, message, notification, Button} from "antd"; import debounce from 'lodash.debounce'; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const {Option} = Select; @@ -64,19 +65,8 @@ class RoleInstall extends React.Component { this.setState({data, fetching: false}); } - }).catch((error) => { console.log(error); - if (error.hasOwnProperty("status") && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+'/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load roles.", - }); - } - + }).catch((error) => { + handleApiError(error,"Error occurred while trying to load roles."); this.setState({fetching: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js index 8e592d52cf5..d04d11a24c1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js @@ -21,6 +21,7 @@ import {Typography, Select, Spin, message, notification, Button} from "antd"; import debounce from 'lodash.debounce'; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const {Option} = Select; @@ -67,18 +68,7 @@ class UserInstall extends React.Component { } }).catch((error) => { - if (error.response.hasOwnProperty(status) && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load users.", - }); - } - + handleApiError(error,"Error occurred while trying to load users."); this.setState({fetching: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js index ee2b8f65ffa..e30cac30bba 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js @@ -21,6 +21,7 @@ import {Drawer, Button, Icon, Row, Col, Typography, Divider, Input, Spin, notifi import StarRatings from "react-star-ratings"; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Title} = Typography; const {TextArea} = Input; @@ -103,20 +104,11 @@ class AddReview extends React.Component { } }).catch((error) => { - if (error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - this.setState({ - loading: false, - visible: false - }); - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "We are unable to add your review right now.", - }); - } + handleApiError(error,"We are unable to add your review right now."); + this.setState({ + loading: false, + visible: false + }); }); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js index ed1f14711c4..2173db1921b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js @@ -22,6 +22,7 @@ import SingleReview from "./singleReview/SingleReview"; import axios from "axios"; import AddReview from "./AddReview"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text, Paragraph} = Typography; @@ -52,17 +53,7 @@ class CurrentUsersReview extends React.Component { } }).catch((error) => { - if (error.response.hasOwnProperty(status) && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to get your review.", - }); - } + handleApiError(error,"Error occurred while trying to get your review."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js index ca0e84567c5..59020a26d62 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js @@ -24,6 +24,7 @@ import InfiniteScroll from 'react-infinite-scroller'; import SingleReview from "./singleReview/SingleReview"; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const limit = 5; @@ -60,16 +61,7 @@ class Reviews extends React.Component { } }).catch(function (error) { - if (error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load reviews.", - }); - } + handleApiError(error,"Error occurred while trying to load reviews."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js index 29f44b1a685..fe6ae73b9e9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js @@ -25,6 +25,7 @@ import "./SingleReview.css"; import EditReview from "./editReview/EditReview"; import axios from "axios"; import {withConfigContext} from "../../../../../context/ConfigContext"; +import {handleApiError} from "../../../../../js/Utils"; const {Text, Paragraph} = Typography; const colorList = ['#f0932b', '#badc58', '#6ab04c', '#eb4d4b', '#0abde3', '#9b59b6', '#3498db', '#22a6b3', '#e84393', '#f9ca24']; @@ -78,17 +79,7 @@ class SingleReview extends React.Component { this.props.deleteCallback(id); } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("response") && error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "We were unable to delete the review..", - }); - } + handleApiError(error,"We were unable to delete the review.."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js index f18bac5079b..67dd8c7c2d7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js @@ -22,6 +22,7 @@ import StarRatings from "react-star-ratings"; import axios from "axios"; import "./EditReview.css"; import {withConfigContext} from "../../../../../../context/ConfigContext"; +import {handleApiError} from "../../../../../../js/Utils"; const {Title} = Typography; const {TextArea} = Input; @@ -115,21 +116,11 @@ class EditReview extends React.Component { } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("response") && error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - this.setState({ - loading: false, - visible: false - }); - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "We are unable to add your review right now.", - }); - } + handleApiError(error,"We are unable to add your review right now."); + this.setState({ + loading: false, + visible: false + }); }); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/js/Utils.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/js/Utils.js new file mode 100644 index 00000000000..7a51083453f --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/js/Utils.js @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import {notification} from "antd"; + +export const handleApiError = (error, message) => { + console.log(error); + if (error.hasOwnProperty("response") && error.response.status === 401) { + window.location.href = window.location.origin + '/store/login'; + } else { + notification["error"]({ + message: "There was a problem", + duration: 0, + description: message, + }); + } +}; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js index 5aed47a3b41..ec615bfedfc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js @@ -21,6 +21,7 @@ import {Typography, Row, Col, Form, Icon, Input, Button, Checkbox} from 'antd'; import './Login.css'; import axios from 'axios'; import {withConfigContext} from "../context/ConfigContext"; +import {handleApiError} from "../js/Utils"; const {Title} = Typography; const {Text} = Typography; @@ -104,7 +105,8 @@ class NormalLoginForm extends React.Component { window.location = window.location.origin+ "/store"; } }).catch(function (error) { - if (error.response.status === 400) { + handleApiError(error,"Error occurred while trying to load groups."); + if (error.hasOwnProperty("response") && error.response.status === 400) { thisForm.setState({ inValid: true, loading: false diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js index e59b58d9907..044dae53782 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js @@ -26,6 +26,7 @@ import axios from "axios"; import "./Dashboard.css"; import {withConfigContext} from "../../context/ConfigContext"; import Logout from "./logout/Logout"; +import {handleApiError} from "../../js/Utils"; const {SubMenu} = Menu; @@ -58,16 +59,7 @@ class Dashboard extends React.Component { } }).catch((error) => { - if (error.hasOwnProperty("response") && error.response.status === 401) { - window.location.href = window.location.origin + '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load device types.", - }); - } + handleApiError(error,"Error occurred while trying to load device types."); this.setState({ loading: false }); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js index b1eb744b221..adac67e7378 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js @@ -23,6 +23,7 @@ import ReleaseView from "../../../../components/apps/release/ReleaseView"; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; import {Link} from "react-router-dom"; +import {handleApiError} from "../../../../js/Utils"; const {Title} = Typography; @@ -71,20 +72,7 @@ class Release extends React.Component { } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("response") && error.response.status === 401) { - //todo display a popop with error - message.error('You are not logged in'); - window.location.href = window.location.origin + '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load releases.", - }); - } - + handleApiError(error,"Error occurred while trying to load releases."); this.setState({loading: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js index 35068cc581b..87c57bc0142 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js @@ -20,6 +20,7 @@ import React from "react"; import {notification, Menu, Icon} from 'antd'; import axios from 'axios'; import {withConfigContext} from "../../../context/ConfigContext"; +import {handleApiError} from "../../../js/Utils"; /* This class for call the logout api by sending request @@ -52,19 +53,7 @@ class Logout extends React.Component { window.location = window.location.origin + "/store/login"; } }).catch(function (error) { - - if (error.hasOwnProperty("response") && error.response.status === 400) { - thisForm.setState({ - inValid: true - }); - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to logout.", - }); - } + handleApiError(error,"Error occurred while trying to get your review."); }); };