import React, { useState, useEffect } from "react"; import theme from '../theme.jsx'; import { toast } from 'react-toastify'; import { Tooltip, IconButton, ListItem, ListItemText, FormGroup, FormControl, InputLabel, FormLabel, FormControlLabel, Select, MenuItem, Grid, Paper, Typography, Zoom, } from "@mui/material"; import { Edit as EditIcon, Delete as DeleteIcon, SelectAll as SelectAllIcon, } from "@mui/icons-material"; const AuthenticationItem = (props) => { const { data, index, globalUrl, getAppAuthentication } = props const [selectedAuthentication, setSelectedAuthentication] = React.useState({}) const [selectedAuthenticationModalOpen, setSelectedAuthenticationModalOpen] = React.useState(false); const [authenticationFields, setAuthenticationFields] = React.useState([]); //const alert = useAlert(); var bgColor = "#27292d"; if (index % 2 === 0) { bgColor = "#1f2023"; } //console.log("Auth data: ", data) if (data.type === "oauth2") { data.fields = [ { key: "url", value: "Secret. Replaced during app execution!", }, { key: "client_id", value: "Secret. Replaced during app execution!", }, { key: "client_secret", value: "Secret. Replaced during app execution!", }, { key: "scope", value: "Secret. Replaced during app execution!", }, ]; } const deleteAuthentication = (data) => { toast("Deleting auth " + data.label); // Just use this one? const url = globalUrl + "/api/v1/apps/authentication/" + data.id; console.log("URL: ", url); fetch(url, { method: "DELETE", credentials: "include", headers: { "Content-Type": "application/json", }, }) .then((response) => response.json().then((responseJson) => { console.log("RESP: ", responseJson); if (responseJson["success"] === false) { toast("Failed deleting auth"); } else { // Need to wait because query in ES is too fast setTimeout(() => { getAppAuthentication(); }, 1000); //toast("Successfully deleted authentication!") } }) ) .catch((error) => { console.log("Error in userdata: ", error); }); } const editAuthenticationConfig = (id) => { const data = { id: id, action: "assign_everywhere", }; const url = globalUrl + "/api/v1/apps/authentication/" + id + "/config"; fetch(url, { mode: "cors", method: "POST", body: JSON.stringify(data), credentials: "include", crossDomain: true, withCredentials: true, headers: { "Content-Type": "application/json; charset=utf-8", }, }) .then((response) => response.json().then((responseJson) => { if (responseJson["success"] === false) { toast("Failed overwriting appauth in workflows"); } else { toast("Successfully updated auth everywhere!"); //setSelectedUserModalOpen(false); setTimeout(() => { getAppAuthentication(); }, 1000); } }) ) .catch((error) => { toast("Err: " + error.toString()); }); }; const updateAppAuthentication = (field) => { setSelectedAuthenticationModalOpen(true); setSelectedAuthentication(field); //{selectedAuthentication.fields.map((data, index) => { var newfields = []; for (var key in field.fields) { newfields.push({ key: field.fields[key].key, value: "", }); } setAuthenticationFields(newfields); } return ( style={{ minWidth: 75, maxWidth: 75 }} /> {/* */} {/* */} { return data.key; }) .join(", ") } style={{ minWidth: 125, maxWidth: 125, overflow: "hidden", }} /> { updateAppAuthentication(data); }} > {data.defined ? ( { editAuthenticationConfig(data.id); }} > ) : ( {}} > )} { deleteAuthentication(data); }} > ) } export default AuthenticationItem