|
| 1 | +// This file is part of MinIO Console Server |
| 2 | +// Copyright (c) 2021 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import React, {useState} from "react"; |
| 18 | +import ModalWrapper from "../../Common/ModalWrapper/ModalWrapper"; |
| 19 | +import { Button, Grid } from "@material-ui/core"; |
| 20 | +import { createStyles, Theme, withStyles } from "@material-ui/core/styles"; |
| 21 | +import { modalBasic } from "../../Common/FormComponents/common/styleLibrary"; |
| 22 | +import { connect } from "react-redux"; |
| 23 | +import api from "../../../../common/api"; |
| 24 | +import { ErrorResponseHandler } from "../../../../common/types"; |
| 25 | +import { setErrorSnackMessage } from "../../../../actions"; |
| 26 | +import { AppState } from "../../../../store"; |
| 27 | +import SelectWrapper from "../../Common/FormComponents/SelectWrapper/SelectWrapper"; |
| 28 | + |
| 29 | +const mapState = (state: AppState) => ({ |
| 30 | + session: state.console.session, |
| 31 | +}); |
| 32 | + |
| 33 | +const connector = connect(mapState, { setErrorSnackMessage }); |
| 34 | + |
| 35 | +interface IEditAccessRule { |
| 36 | + classes: any; |
| 37 | + modalOpen: boolean; |
| 38 | + onClose: () => any; |
| 39 | + bucket: string; |
| 40 | + toEdit: string; |
| 41 | + initial: string; |
| 42 | +} |
| 43 | + |
| 44 | +const styles = (theme: Theme) => |
| 45 | + createStyles({ |
| 46 | + buttonContainer: { |
| 47 | + textAlign: "right", |
| 48 | + }, |
| 49 | + pathLabel: { |
| 50 | + marginTop: 0, |
| 51 | + marginBottom: 32, |
| 52 | + }, |
| 53 | + ...modalBasic, |
| 54 | + }); |
| 55 | + |
| 56 | +const EditAccessRule = ({ |
| 57 | + modalOpen, |
| 58 | + onClose, |
| 59 | + classes, |
| 60 | + bucket, |
| 61 | + toEdit, |
| 62 | + initial |
| 63 | +}: IEditAccessRule) => { |
| 64 | + const [selectedAccess, setSelectedAccess] = useState<any>(initial); |
| 65 | + |
| 66 | + const accessOptions = [ |
| 67 | + { label: "readonly", value: "readonly" }, |
| 68 | + { label: "writeonly", value: "writeonly" }, |
| 69 | + { label: "readwrite", value: "readwrite" }, |
| 70 | + ]; |
| 71 | + |
| 72 | + const resetForm = () => { |
| 73 | + setSelectedAccess(initial); |
| 74 | + }; |
| 75 | + |
| 76 | + const createProcess = () => { |
| 77 | + api |
| 78 | + .invoke("PUT", `/api/v1/bucket/${bucket}/access-rules`, { |
| 79 | + prefix: toEdit, |
| 80 | + access: selectedAccess, |
| 81 | + }) |
| 82 | + .then((res: any) => { |
| 83 | + onClose(); |
| 84 | + }) |
| 85 | + .catch((err: ErrorResponseHandler) => { |
| 86 | + setErrorSnackMessage(err); |
| 87 | + onClose(); |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <React.Fragment> |
| 93 | + <ModalWrapper |
| 94 | + modalOpen={modalOpen} |
| 95 | + title={`Edit Access Rule for ${toEdit}`} |
| 96 | + onClose={onClose} |
| 97 | + > |
| 98 | + <Grid container> |
| 99 | + <Grid item xs={12}> |
| 100 | + <SelectWrapper |
| 101 | + id="access" |
| 102 | + name="Access" |
| 103 | + onChange={(e) => { |
| 104 | + setSelectedAccess(e.target.value); |
| 105 | + }} |
| 106 | + label="Access" |
| 107 | + value={selectedAccess} |
| 108 | + options={accessOptions} |
| 109 | + disabled={false} |
| 110 | + /> |
| 111 | + </Grid> |
| 112 | + <Grid item xs={12} className={classes.buttonContainer}> |
| 113 | + <button |
| 114 | + type="button" |
| 115 | + color="primary" |
| 116 | + className={classes.clearButton} |
| 117 | + onClick={resetForm} |
| 118 | + > |
| 119 | + Clear |
| 120 | + </button> |
| 121 | + <Button |
| 122 | + type="submit" |
| 123 | + variant="contained" |
| 124 | + color="primary" |
| 125 | + onClick={createProcess} |
| 126 | + > |
| 127 | + Save |
| 128 | + </Button> |
| 129 | + </Grid> |
| 130 | + </Grid> |
| 131 | + </ModalWrapper> |
| 132 | + </React.Fragment> |
| 133 | + ); |
| 134 | +}; |
| 135 | + |
| 136 | +export default withStyles(styles)(connector(EditAccessRule)); |
0 commit comments