import getAllocations from '@/api/admin/nodes/getAllocations'; import { Server } from '@/api/admin/servers/getServers'; import ServerDeleteButton from '@/components/admin/servers/ServerDeleteButton'; import Label from '@/components/elements/Label'; import Select from '@/components/elements/Select'; import SelectField, { AsyncSelectField, Option } from '@/components/elements/SelectField'; import { faBalanceScale, faCogs, faConciergeBell, faNetworkWired } from '@fortawesome/free-solid-svg-icons'; import React from 'react'; import AdminBox from '@/components/admin/AdminBox'; import { useHistory } from 'react-router-dom'; import tw from 'twin.macro'; import { object } from 'yup'; import updateServer, { Values } from '@/api/admin/servers/updateServer'; import Field from '@/components/elements/Field'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import { Form, Formik, FormikHelpers, useFormikContext } from 'formik'; import { Context, ServerIncludes } from '@/components/admin/servers/ServerRouter'; import { ApplicationStore } from '@/state'; import { Actions, useStoreActions } from 'easy-peasy'; import OwnerSelect from '@/components/admin/servers/OwnerSelect'; import Button from '@/components/elements/Button'; import FormikSwitch from '@/components/elements/FormikSwitch'; export function ServerSettingsContainer ({ server }: { server?: Server }) { const { isSubmitting } = useFormikContext(); return ( ); } export function ServerFeatureContainer () { const { isSubmitting } = useFormikContext(); return ( ); } export function ServerAllocationsContainer ({ server }: { server: Server }) { const { isSubmitting } = useFormikContext(); const loadOptions = async (inputValue: string, callback: (options: Option[]) => void) => { const allocations = await getAllocations(server.nodeId, { ip: inputValue, server_id: '0' }); callback(allocations.map(a => { return { value: a.id.toString(), label: a.getDisplayText() }; })); }; return ( Primary Allocation {server.relations?.allocations?.map(a => ( {a.getDisplayText()} ))} { return { value: a.id.toString(), label: a.getDisplayText() }; }) || []} isMulti isSearchable css={tw`mb-2`} /> ); } export function ServerResourceContainer () { const { isSubmitting } = useFormikContext(); return ( ); } export default function ServerSettingsContainer2 ({ server }: { server: Server }) { const history = useHistory(); const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); const setServer = Context.useStoreActions(actions => actions.setServer); const submit = (values: Values, { setSubmitting, setFieldValue }: FormikHelpers) => { clearFlashes('server'); // This value is inverted to have the switch be on when the // OOM Killer is enabled, rather than when disabled. values.limits.oomDisabled = !values.limits.oomDisabled; updateServer(server.id, values, ServerIncludes) .then(s => { setServer({ ...server, ...s }); // TODO: Figure out how to properly clear react-selects for allocations. setFieldValue('addAllocations', []); setFieldValue('removeAllocations', []); }) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'server', error }); }) .then(() => setSubmitting(false)); }; return ( {({ isSubmitting, isValid }) => ( history.push('/admin/servers')} /> Save Changes )} ); }