Skip to content

Commit

Permalink
fixed issue #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Frode Sjovatsen committed Sep 27, 2018
1 parent cf97933 commit bc7bf21
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 43 deletions.
48 changes: 36 additions & 12 deletions client/src/common/input-validation/UsernameValidationInput.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import React from "react";
import {FormControl, FormHelperText, Input, InputLabel, withStyles} from "@material-ui/core";
import PropTypes from "prop-types";
import Grid from "@material-ui/core/Grid/Grid";
import Typography from "@material-ui/core/Typography/Typography";


const styles = () => {
};
const styles = (theme) => ({
realm: {
width: '50%',
display: 'box',
boxAlign: 'center',
fontSize: '1rem',
},
label: {
width: '50%',
position: 'inherit'
},
input: {
width: '50%',
},
});

class UsernameValidationInput extends React.Component {

Expand All @@ -31,26 +46,35 @@ class UsernameValidationInput extends React.Component {
}

render() {
const {name, title} = this.props;
const {name, title, realm, classes} = this.props;
return (
<FormControl fullWidth error={!this.state.usernameValid} required>
<InputLabel htmlFor={name}>{title}</InputLabel>
<Input
autoFocus
fullWidth
name={name}
onChange={this.onChangeUsername}
/>
<InputLabel className={classes.label} htmlFor={name}>{title}</InputLabel>
<Grid container>
<Input
autoFocus
name={name}
onChange={this.onChangeUsername}
className={classes.input}
/>
<Typography variant="caption" className={classes.realm}>{realm}</Typography>
</Grid>

<FormHelperText>{this.state.usernameValid ? '' : 'Brukernavnet kan bare inneholde a-z, A-Z, 0-9, - og _. Det kan fra 3-128 tegn langt.'}</FormHelperText>
</FormControl>
);
}
}

UsernameValidationInput.defaultProps = {
realm: "@dummy.no"
};
UsernameValidationInput.propTypes = {
name: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
usernameIsValid: PropTypes.func.isRequired,
realm: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
usernameIsValid: PropTypes.func.isRequired
};
export default withStyles(styles)(UsernameValidationInput);

8 changes: 8 additions & 0 deletions client/src/data/api/AssetApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class AssetApi {

}

static getPrimaryAsset(organisation) {
return fetch(`/api/assets/${organisation}/primary`, {
method: 'GET',
credentials: 'same-origin'
})
.then(response => Promise.all([response, response.json()]));
}

static createAsset(asset, organisation) {

const request = new Request(`/api/assets/${organisation}/`, {
Expand Down
24 changes: 15 additions & 9 deletions client/src/features/adapter/add/AdapterAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TextField from "@material-ui/core/TextField";
import {Add} from "@material-ui/icons";
import UsernameValidationInput from "../../../common/input-validation/UsernameValidationInput";
import AdapterApi from "../../../data/api/AdapterApi";
import AssetApi from "../../../data/api/AssetApi";

const styles = () => ({
addButton: {
Expand Down Expand Up @@ -56,21 +57,25 @@ class AdapterAdd extends React.Component {
};

openAddDialog = () => {
this.setState({showAdapterAdd: true, notify: false});
AssetApi.getPrimaryAsset(this.props.organisation.name)
.then(([response, json]) => {
if (response.status === 200) {
this.setState({
showAdapterAdd: true,
notify: false,
realm: `@adapter.${json.assetId}`
});
}
else {
this.props.notify("Det oppstod problemer med å hente primær ressurs id.");
}
});
};

handleCancel = () => {
this.setState({showAdapterAdd: false, notify: false});
};

/*
onCloseNotification = () => {
this.setState({
notify: false,
});
};
*/

getEmptyAdapter = () => {
return {
name: '',
Expand Down Expand Up @@ -114,6 +119,7 @@ class AdapterAdd extends React.Component {
name="name"
onChange={this.updateAdapterState}
usernameIsValid={this.usernameIsValid}
realm={this.state.realm}
/>
<TextField
name="shortDescription"
Expand Down
39 changes: 17 additions & 22 deletions client/src/features/clients/add/ClientAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TextField from "@material-ui/core/TextField";
import {Add} from "@material-ui/icons";
import UsernameValidationInput from "../../../common/input-validation/UsernameValidationInput";
import ClientApi from "../../../data/api/ClientApi";
import AssetApi from "../../../data/api/AssetApi";

const styles = () => ({
addButton: {
Expand All @@ -14,8 +15,7 @@ const styles = () => ({
bottom: 'auto',
right: 50,
position: 'fixed',
}

},
});

class ClientAdd extends React.Component {
Expand Down Expand Up @@ -51,39 +51,32 @@ class ClientAdd extends React.Component {
this.props.notify("Oisann, dette gikk ikke helt etter planen! Prøv igjen ;)");
});

/*
this.props.createClient(this.state.client, this.props.organisation).then(() => {
this.setState({
showClientAdd: false,
notify: true,
clientAddedName: this.state.client.name,
client: this.getEmptyClient(),
});
});
*/

};

usernameIsValid = (valid) => {
this.setState({usernameIsValid: valid});
};

openAddDialog = () => {
this.setState({showClientAdd: true, notify: false});
AssetApi.getPrimaryAsset(this.props.organisation.name)
.then(([response, json]) => {
if (response.status === 200) {
this.setState({
showClientAdd: true,
notify: false,
realm: `@client.${json.assetId}`
});
}
else {
this.props.notify("Det oppstod problemer med å hente primær ressurs id.");
}
});
};

handleCancel = () => {
this.setState({showClientAdd: false, notify: false});
};

/*
onCloseNotification = () => {
this.setState({
notify: false,
});
};
*/

getEmptyClient = () => {
return {
name: '',
Expand Down Expand Up @@ -127,6 +120,8 @@ class ClientAdd extends React.Component {
name="name"
onChange={this.updateClientState}
usernameIsValid={this.usernameIsValid}
className={classes.username}
realm={this.state.realm}
/>
<TextField
name="shortDescription"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public ResponseEntity getAssets(@PathVariable("orgName") String orgName) {
return ResponseEntity.ok(assets);
}

@ApiOperation("Get Primary Asset ID")
@GetMapping("/primary")
public ResponseEntity<Asset> getPrimaryAsset(@PathVariable("orgName") String orgName) {
Organisation organisation = portalApiService.getOrganisation(orgName);
Asset primaryAsset = assetService.getPrimaryAsset(organisation);

return ResponseEntity.ok(primaryAsset);

}

@ApiOperation("Create Asset")
@PostMapping("/")
public ResponseEntity addAsset(@PathVariable String orgName,
Expand Down

0 comments on commit bc7bf21

Please sign in to comment.