import React from "react"; import MaterialButton from "@material-ui/core/Button"; import Grid from "@material-ui/core/Grid"; import Switch from "@material-ui/core/Switch"; import FormControlLabel from "@material-ui/core/FormControlLabel"; import { Dialog, DialogBody, DialogTitle, Button, TextInput, } from "@sc/components/ui"; import { DisplayItems } from "@sc/modules/v2/CRUD/DisplayItems/"; import { DisplayItemsFieldDataType, DisplayItemsPropsType, DisplayItemsFieldData, } from "@sc/modules/v2/CRUD/DisplayItems/components/types"; import Page from "@sc/modules/app/Page"; import theme from "@sc/components/ui/theme"; import { client } from "@sc/api/gql/client"; import gql from "graphql-tag"; import AdminSettings from "./Settings"; import { app } from "@sc/10mf/src/config"; const NewAccount: React.FC = ({ onCreate, onClose }) => { const [data, setData] = React.useState({}); const [shouldSendEmail, setShouldSendEmail] = React.useState(false); return ( Create New Account
setData({ ...data, firstName: e.target.value })} /> setData({ ...data, lastName: e.target.value })} /> setData({ ...data, email: e.target.value })} /> setData({ ...data, password: e.target.value })} /> {app.id === "10mf" && ( { setShouldSendEmail(e.target.checked); }} name="activeShouldSendEmail" /> } label="Send Welcome Email?" /> )} onCreate({ ...data, emailUser: shouldSendEmail }) } > Create Account
); }; const Administration: React.FC = () => { const [users, setUsers] = React.useState([]); const [activeTab, setActiveTab] = React.useState("accounts"); const [isLoading, setIsLoading] = React.useState(true); const [newAccountIsShowing, setNewAccountIsShowing] = React.useState(false); const [onlyShowActiveUsers, setOnlyShowActiveUsers] = React.useState(true); // grab list of users const getUsers = async () => { const { data } = await client.query({ query: gql` query getUsers { users( orderBy: createdAt_DESC where: { # deleted: null apps_some: { name: "${app.id}" } } ) { id email firstName lastName deleted createdAt } } `, }); // console.log({ data }); setUsers(data.users); setIsLoading(false); }; React.useEffect(() => { getUsers(); // console.log({ app }); }, []); const fields: DisplayItemsFieldData[] = [ { name: "firstName", label: "First Name", type: DisplayItemsFieldDataType.STRING, }, { name: "lastName", label: "Last Name", type: DisplayItemsFieldDataType.STRING, }, { name: "email", label: "Email", type: DisplayItemsFieldDataType.EMAIL, }, { name: "createdAt", label: "Join Date", type: DisplayItemsFieldDataType.DATE, }, { name: "deleted", label: "Canceled", type: DisplayItemsFieldDataType.BOOLEAN, }, ]; const createNewUser = async (data) => { console.log({ data }); const host = process.env.REACT_APP_SERVER_ENDPOINT; const options = { method: "POST", mode: "cors", cache: "no-cache", headers: { "Content-Type": "application/json", Origin: window.location.origin, }, body: JSON.stringify({ ...data, appName: app.id, permissions: [app.id], }), }; const res = await fetch(`http://${host}:8091/v1/users/createUser`, options); const response = await res.json(); console.log({ response }); window.location.href = "/admin"; // setNewAccountIsShowing(false); }; const cancelUsers = async (users) => { if ( window.confirm(`Are you sure you want to cancel ${users.length} users?`) ) { // loop through users for (let i = 0; i < users.length; i++) { // cancel each one console.log(users[i].id); await client.mutate({ mutation: gql` mutation cancelUser($id: ID!) { updateUser(data: { deleted: true }, where: { id: $id }) { id } } `, variables: { id: users[i].id, }, }); } // refresh the page window.location.href = "/admin"; } }; const activateUsers = async (users) => { if ( window.confirm(`Are you sure you want to activate ${users.length} users?`) ) { // loop through users for (let i = 0; i < users.length; i++) { // cancel each one console.log(users[i].id); await client.mutate({ mutation: gql` mutation activateUser($id: ID!) { updateUser(data: { deleted: false }, where: { id: $id }) { id } } `, variables: { id: users[i].id, }, }); } // refresh the page window.location.href = "/admin"; } }; return ( {/* setActiveTab("accounts")} > Accounts setActiveTab("settings")} > Settings setActiveTab("integration")} > Integration */} {newAccountIsShowing && ( setNewAccountIsShowing(false)} /> )}
{activeTab === "accounts" && (

Account Administration

{ setOnlyShowActiveUsers(e.target.checked); }} name="activeOnlyChecked" /> } label="Only Show Active Users" />
{ // user.deleted == onlyShowActiveUsers if (onlyShowActiveUsers) return !user.deleted; return true; })} loading={isLoading} fields={fields} // title="Administration" onFilterChange={(e) => console.log("changed filter", e)} dataItemActions={[]} paginationSettings={{}} type={DisplayItemsPropsType.TABLE} showCheckboxes showFilterControls={false} bulkActions={[ (props) => ( cancelUsers(props.data)} > Cancel Account ), (props) => ( activateUsers(props.data)} > Activate ), ]} />
)} {activeTab === "settings" && (

Account Settings

)} {activeTab === "integration" && (

Integration

)}
); }; export default ;