Skip to content

Commit dee4ccf

Browse files
committed
updatiing clients
1 parent f86f803 commit dee4ccf

10 files changed

Lines changed: 183 additions & 84 deletions

File tree

src/components/PageTitle/PageTitle.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const PageTitle = ({ classes, ...props }) => (
1313
variant="contained"
1414
size="large"
1515
color="secondary"
16-
onClick = {props.onBtnClick}
16+
onClick={props.onBtnClick}
17+
disabled={props.btnDisabled}
1718
>
1819
{props.button}
1920
</Button>
@@ -29,15 +30,15 @@ const styles = theme => ({
2930
marginTop: theme.spacing.unit * 5
3031
},
3132
typo: {
32-
color: theme.palette.text.hint,
33+
color: theme.palette.text.hint
3334
},
3435
button: {
3536
boxShadow: theme.customShadows.widget,
36-
textTransform: 'none',
37-
'&:active' : {
38-
boxShadow: theme.customShadows.widgetWide,
39-
},
40-
},
37+
textTransform: "none",
38+
"&:active": {
39+
boxShadow: theme.customShadows.widgetWide
40+
}
41+
}
4142
});
4243

4344
export default withStyles(styles)(PageTitle);

src/components/add-adapters/addAdapterState.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const username = currentUser && currentUser.get("username");
99
export const initialState = {
1010
isLoading: true,
1111
error: null,
12-
errorMsg: "",
12+
errorMsg: null,
1313
isSuccess: false
1414
};
1515

@@ -87,7 +87,7 @@ export const editAdapter = ({
8787
startAfter,
8888
expiredBefore
8989
}) => async dispatch => {
90-
// dispatch(startAddingAdapter());
90+
dispatch(startAddingAdapter());
9191

9292
try {
9393
const Client = Parse.Object.extend(username);
@@ -113,12 +113,12 @@ export const editAdapter = ({
113113
await client.save({
114114
adapters: newAdapters
115115
});
116-
// dispatch(addingAdapterSuccess(true));
116+
dispatch(addingAdapterSuccess(true));
117117
const newClient = Object.assign({}, clientData, { adapters: newAdapters });
118118
dispatch(openAddAdapter(false));
119119
dispatch(setClient(newClient));
120120
} catch (error) {
121-
// dispatch(addingAdapterFailure(error.message));
121+
dispatch(addingAdapterFailure(error.message));
122122
}
123123
};
124124

@@ -148,7 +148,7 @@ export default function AddAdapterReducer(
148148
case RESET_ERROR:
149149
return {
150150
error: false,
151-
errorMsg: ""
151+
errorMsg: null
152152
};
153153
default:
154154
return state;

src/components/add-adapters/addAdapterView.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ import {
2727
KeyboardDatePicker
2828
} from "@material-ui/pickers";
2929

30+
const availableAdapters = [
31+
{ label: "MQTT", value: "mqtt" },
32+
{ label: "COaP", value: "coap" },
33+
{ label: "HTTP", value: "http" }
34+
];
35+
3036
const AddAdapter = ({ classes, adapterData, ...props }) => {
37+
const addedApters = props.client.adapters.map(value => value.type);
3138
React.useEffect(() => {
3239
if (adapterData) {
3340
props.setTyoe(adapterData.type);
@@ -43,7 +50,7 @@ const AddAdapter = ({ classes, adapterData, ...props }) => {
4350
{/* <Header /> */}
4451
<Grid container className={classes.container}>
4552
<div style={{ position: "relative", marginTop: "40px" }}>
46-
<Fade in={!props.error}>
53+
<Fade in={props.error}>
4754
<Typography color="secondary" className={classes.errorMessage}>
4855
{props.errorMsg}
4956
</Typography>
@@ -53,7 +60,7 @@ const AddAdapter = ({ classes, adapterData, ...props }) => {
5360
className={classes.notificationItem}
5461
shadowless
5562
type="customer"
56-
message="Adapter added"
63+
message={adapterData ? "Adapter updated" : "Adapter added"}
5764
variant="contained"
5865
color="success"
5966
/>
@@ -78,9 +85,16 @@ const AddAdapter = ({ classes, adapterData, ...props }) => {
7885
onChange={e => props.setTyoe(e.target.value)}
7986
fullWidth
8087
>
81-
<MenuItem value="mqtt">MQTT</MenuItem>
82-
<MenuItem value="coap">COaP</MenuItem>
83-
<MenuItem value="http">HTTP</MenuItem>
88+
{availableAdapters
89+
.filter(data => !addedApters.includes(data.value))
90+
.map((adapter, index) => (
91+
<MenuItem
92+
key={`adapter-select${index}`}
93+
value={adapter.value}
94+
>
95+
{adapter.label}
96+
</MenuItem>
97+
))}
8498
</Select>
8599
</FormControl>
86100
<FormControlLabel

src/components/add-client/addClientContainer.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { withHandlers, withState, lifecycle, compose } from "recompose";
22
import AddClientView from "./addClientView";
33
import { withRouter } from "react-router-dom";
44
import { connect } from "react-redux";
5-
import { addClient, resetError } from "./addClientState";
5+
import { addClient, resetError, editClient } from "./addClientState";
66
import { getUserClients } from "../../pages/clients/ClientState";
77
import deburr from "lodash/deburr";
88
const Parse = window.Parse;
@@ -15,10 +15,11 @@ export default compose(
1515
isSuccess: state.addClient.isSuccess,
1616
user_clients: state.client.user_clients
1717
}),
18-
{ addClient, resetError, getUserClients }
18+
{ addClient, resetError, getUserClients, editClient }
1919
),
2020
withState("version", "setVersion", "1.0.0"),
2121
withState("clientName", "setClientName", ""),
22+
withState("clientExist", "setClientExist", false),
2223
withRouter,
2324
withHandlers({
2425
handleAddClientButtonClick: props => async () => {
@@ -31,10 +32,18 @@ export default compose(
3132
return;
3233
} else {
3334
props.addClient({
34-
clientName: props.clientName,
35-
version: props.version
35+
clientName: props.clientName,
36+
version: props.version
3637
});
3738
}
39+
},
40+
handleEditClientButtonClick: props => async () => {
41+
42+
props.editClient({
43+
clientData: props.clientData,
44+
clientName: props.clientName,
45+
version: props.version
46+
});
3847
}
3948
}),
4049
lifecycle({

src/components/add-client/addClientState.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ export const addClient = ({ clientName, version }) => async (dispatch, getState)
6060
}
6161
};
6262

63+
64+
export const editClient = ({clientData, clientName, version }) => async (dispatch, getState) => {
65+
dispatch(startAddingClient());
66+
67+
try {
68+
const Client = Parse.Object.extend(username);
69+
let query = new Parse.Query(Client);
70+
let client = await query.get(clientData.objectId);
71+
let adapters = clientData.adapters;
72+
73+
let new_client = await client.save({
74+
clientName,
75+
adapters,
76+
ver: version,
77+
realm: Parse.User.current().get("username")
78+
});
79+
const {user_clients} = getState().client
80+
const new_user_clients = user_clients.map(value=>value)
81+
const clientIndex = new_user_clients.findIndex(value=>value.clientName===clientData.clientName)
82+
new_user_clients[clientIndex]=new_client.toJSON()
83+
dispatch(addingClientSuccess(true));
84+
dispatch(openAddClient(false));
85+
dispatch(gettingUserClientSuccessfull(new_user_clients))
86+
} catch (error) {
87+
dispatch(addingClientFailure(error.message));
88+
}
89+
};
90+
6391
export default function AddClientReducer(
6492
state = initialState,
6593
{ type, payload }

src/components/add-client/addClientView.js

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,31 @@ import {
1313
import NotificationCustomComponent from "../../components/Notification";
1414
import classnames from "classnames";
1515

16-
const AddClient = ({ classes, ...props }) => {
16+
const AddClient = ({ classes, clientData, ...props }) => {
17+
React.useEffect(() => {
18+
if (clientData) {
19+
props.setClientName(clientData.clientName);
20+
props.setVersion(clientData.ver);
21+
}
22+
}, []);
23+
24+
React.useEffect(() => {
25+
let clientExist;
26+
if (!clientData) {
27+
clientExist = props.user_clients.find(
28+
value =>
29+
value.clientName.toLowerCase() === props.clientName.toLowerCase()
30+
);
31+
} else {
32+
clientExist = props.user_clients.find(
33+
value =>
34+
value.clientName.toLowerCase() === props.clientName.toLowerCase() &&
35+
props.clientName.toLowerCase() !== clientData.clientName.toLowerCase()
36+
);
37+
}
38+
39+
props.setClientExist(clientExist ? true : false);
40+
}, [props.clientName]);
1741
return (
1842
<Fragment>
1943
{/* <Header /> */}
@@ -40,21 +64,22 @@ const AddClient = ({ classes, ...props }) => {
4064
color="primary"
4165
className={classnames(classes.textRow)}
4266
>
43-
{props.adapterClientName ? "Add New Adapter" : " Add New Client"}
67+
{clientData ? "Edit Client" : " Add New Client"}
4468
</Typography>
4569
<div style={{ width: "400px" }}>
4670
<TextField
4771
required
4872
id="standard-required"
49-
label="Required"
73+
label={props.clientExist?"Errpr":"Required"}
5074
placeholder="Client Name"
5175
value={props.clientName}
52-
onChange={(e)=>props.setClientName(e.target.value)
53-
}
76+
onChange={e => props.setClientName(e.target.value)}
5477
fullWidth
5578
InputLabelProps={{
5679
shrink: true
5780
}}
81+
error={props.clientExist}
82+
helperText={props.clientExist?"Client already exist":null}
5883
/>
5984
<TextField
6085
className={classnames(classes.textField)}
@@ -63,7 +88,7 @@ const AddClient = ({ classes, ...props }) => {
6388
label="Required"
6489
placeholder="Version"
6590
value={props.version}
66-
onChange={(e)=>props.setVersion(e.target.value)}
91+
onChange={e => props.setVersion(e.target.value)}
6792
fullWidth
6893
InputLabelProps={{
6994
shrink: true
@@ -72,6 +97,20 @@ const AddClient = ({ classes, ...props }) => {
7297
</div>
7398
{props.isLoading ? (
7499
<CircularProgress size={26} />
100+
) : clientData ? (
101+
<Button
102+
style={{ marginTop: "25px" }}
103+
variant="contained"
104+
color="primary"
105+
size="small"
106+
className={classes.backButton}
107+
onClick={props.handleEditClientButtonClick}
108+
disabled={
109+
!props.clientName || !props.version || props.clientExist
110+
}
111+
>
112+
Edit Client
113+
</Button>
75114
) : (
76115
<Button
77116
style={{ marginTop: "25px" }}
@@ -80,7 +119,9 @@ const AddClient = ({ classes, ...props }) => {
80119
size="small"
81120
className={classes.backButton}
82121
onClick={props.handleAddClientButtonClick}
83-
disabled={!props.clientName || !props.version}
122+
disabled={
123+
!props.clientName || !props.version || props.clientExist
124+
}
84125
>
85126
Add Client
86127
</Button>

src/components/add-topics/addTopicContainer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default compose(
2323
withState("topic", "setTopic", ""),
2424
withState("oldTopic", "setOldTopic", ""),
2525
withState("adapter", "setAdapter", ""),
26+
withState("topicExist", "setTopicExist", false),
2627
withRouter,
2728
withHandlers({
2829
handleAddAdapterButtonClick: props => async () => {
@@ -38,7 +39,7 @@ export default compose(
3839
props.editTopic({
3940
clientData: props.client,
4041
type: props.type,
41-
oldTopic:props.oldTopic,
42+
oldTopic: props.oldTopic,
4243
action: props.action,
4344
adapter: props.adapter,
4445
topic: props.topic

0 commit comments

Comments
 (0)