Skip to content

Commit

Permalink
Added file name visualization in file select (#289)
Browse files Browse the repository at this point in the history
* Added missing validations in add tenant modal

* Added file name visualization in file selector

Co-authored-by: Benjamin Perez <benjamin@bexsoft.net>
  • Loading branch information
bexsoft and Benjamin Perez committed Sep 28, 2020
1 parent 858d363 commit 459e2bf
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import React from "react";
import get from "lodash/get";
import { createStyles, Theme, withStyles } from "@material-ui/core/styles";
import { NewServiceAccount } from "./types";
import { Button } from "@material-ui/core";
Expand Down Expand Up @@ -67,6 +68,8 @@ const CredentialsPrompt = ({
return null;
}

const consoleCreds = get(newServiceAccount, "console", null);

return (
<ModalWrapper
modalOpen={open}
Expand All @@ -87,6 +90,21 @@ const CredentialsPrompt = ({
<b>Secret Key:</b> {newServiceAccount.secretKey}
</li>
</ul>
{consoleCreds && (
<React.Fragment>
<Grid item xs={12}>
<strong>Console Credentials</strong>
<ul>
<li>
<b>Access Key:</b> {consoleCreds.accessKey}
</li>
<li>
<b>Secret Key:</b> {consoleCreds.secretKey}
</li>
</ul>
</Grid>
</React.Fragment>
)}
<Typography
component="p"
variant="body1"
Expand All @@ -99,11 +117,23 @@ const CredentialsPrompt = ({
<Grid item xs={12} className={classes.buttonContainer}>
<Button
onClick={() => {
let consoleExtras = {};

if (consoleCreds) {
consoleExtras = {
console: {
access_key: consoleCreds.accessKey,
secret_key: consoleCreds.secretKey,
},
};
}

download(
"credentials.json",
JSON.stringify({
access_key: newServiceAccount.accessKey,
secret_key: newServiceAccount.secretKey,
...consoleExtras,
})
);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
export interface NewServiceAccount {
accessKey: string;
secretKey: string;
console?: ConsoleSA;
}

export interface ConsoleSA {
accessKey: string;
secretKey: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import React from "react";
import React, { useState } from "react";
import get from "lodash/get";
import { Grid, InputLabel, Tooltip } from "@material-ui/core";
import IconButton from "@material-ui/core/IconButton";
import AttachFileIcon from "@material-ui/icons/AttachFile";
import CancelIcon from "@material-ui/icons/Cancel";
import HelpIcon from "@material-ui/icons/Help";
import { createStyles, Theme, withStyles } from "@material-ui/core/styles";
import { fieldBasic, tooltipHelper } from "../common/styleLibrary";
Expand All @@ -24,14 +28,15 @@ import { fileProcess } from "./utils";
interface InputBoxProps {
label: string;
classes: any;
onChange: (e: string) => void;
onChange: (e: string, i: string) => void;
id: string;
name: string;
disabled?: boolean;
tooltip?: string;
required?: boolean;
error?: string;
accept?: string;
value?: string;
}

const styles = (theme: Theme) =>
Expand All @@ -41,6 +46,7 @@ const styles = (theme: Theme) =>
textBoxContainer: {
flexGrow: 1,
position: "relative",
flexDirection: "column",
},
errorState: {
color: "#b53b4b",
Expand All @@ -49,6 +55,27 @@ const styles = (theme: Theme) =>
top: 7,
right: 7,
},
errorText: {
margin: "0",
fontSize: "0.75rem",
marginTop: 3,
textAlign: "left",
fontFamily: "Lato,sans-serif",
fontWeight: 400,
lineHeight: "1.66",
color: "#dc1f2e",
},
valueString: {
maxWidth: 350,
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
marginTop: 2,
},
fileReselect: {
display: "flex",
alignItems: "center",
},
});

const FileSelector = ({
Expand All @@ -62,7 +89,10 @@ const FileSelector = ({
required,
error = "",
accept = "",
value = "",
}: InputBoxProps) => {
const [showFileSelector, setShowSelector] = useState(false);

return (
<React.Fragment>
<Grid
Expand Down Expand Up @@ -92,19 +122,62 @@ const FileSelector = ({
)}
</InputLabel>
)}
<div className={classes.textBoxContainer}>
<input
type="file"
name={name}
onChange={(e) => {
fileProcess(e, (data: any) => {
onChange(data);
});
}}
accept={accept}
required
/>
</div>

{showFileSelector || value === "" ? (
<div className={classes.textBoxContainer}>
<input
type="file"
name={name}
onChange={(e) => {
const fileName = get(e, "target.files[0].name", "");
fileProcess(e, (data: any) => {
onChange(data, fileName);
});
}}
accept={accept}
required={required}
disabled={disabled}
/>

{value !== "" && (
<IconButton
color="primary"
aria-label="upload picture"
component="span"
onClick={() => {
setShowSelector(false);
}}
disableRipple={false}
disableFocusRipple={false}
>
<CancelIcon />
</IconButton>
)}

{error !== "" && (
<React.Fragment>
<br />
<span className={classes.errorText}>{error}</span>
</React.Fragment>
)}
</div>
) : (
<div className={classes.fileReselect}>
<div className={classes.valueString}>{value}</div>
<IconButton
color="primary"
aria-label="upload picture"
component="span"
onClick={() => {
setShowSelector(true);
}}
disableRipple={false}
disableFocusRipple={false}
>
<AttachFileIcon />
</IconButton>
</div>
)}
</Grid>
</React.Fragment>
);
Expand Down

0 comments on commit 459e2bf

Please sign in to comment.