diff --git a/resources/scripts/api/admin/eggs/createEggVariable.ts b/resources/scripts/api/admin/eggs/createEggVariable.ts index 4226eef15..dd5ee38fe 100644 --- a/resources/scripts/api/admin/eggs/createEggVariable.ts +++ b/resources/scripts/api/admin/eggs/createEggVariable.ts @@ -4,7 +4,7 @@ import { EggVariable, rawDataToEggVariable } from '@/api/admin/eggs/getEgg'; export default (eggId: number, variable: Omit): Promise => { return new Promise((resolve, reject) => { http.post( - `/api/application/eggs/${eggId}`, + `/api/application/eggs/${eggId}/variables`, { name: variable.name, description: variable.description, diff --git a/resources/scripts/api/admin/eggs/getEgg.ts b/resources/scripts/api/admin/eggs/getEgg.ts index be0ca0395..5e380e113 100644 --- a/resources/scripts/api/admin/eggs/getEgg.ts +++ b/resources/scripts/api/admin/eggs/getEgg.ts @@ -1,6 +1,7 @@ import { Nest } from '@/api/admin/nests/getNests'; import { rawDataToServer, Server } from '@/api/admin/servers/getServers'; import http, { FractalResponseData, FractalResponseList } from '@/api/http'; +import useSWR from 'swr'; export interface EggVariable { id: number; @@ -88,10 +89,16 @@ export const rawDataToEgg = ({ attributes }: FractalResponseData): Egg => ({ }, }); -export default (id: number, include: string[] = []): Promise => { - return new Promise((resolve, reject) => { - http.get(`/api/application/eggs/${id}`, { params: { include: include.join(',') } }) - .then(({ data }) => resolve(rawDataToEgg(data))) - .catch(reject); +export const getEgg = async (id: number): Promise => { + const { data } = await http.get(`/api/application/eggs/${id}`, { params: { include: [ 'variables' ] } }); + + return rawDataToEgg(data); +}; + +export default (id: number) => { + return useSWR(`egg:${id}`, async () => { + const { data } = await http.get(`/api/application/eggs/${id}`, { params: { include: [ 'variables' ] } }); + + return rawDataToEgg(data); }); }; diff --git a/resources/scripts/api/admin/eggs/updateEggVariables.ts b/resources/scripts/api/admin/eggs/updateEggVariables.ts index 498fda0ba..3ccfe3d38 100644 --- a/resources/scripts/api/admin/eggs/updateEggVariables.ts +++ b/resources/scripts/api/admin/eggs/updateEggVariables.ts @@ -1,22 +1,20 @@ import http from '@/api/http'; import { EggVariable, rawDataToEggVariable } from '@/api/admin/eggs/getEgg'; -export default (eggId: number, variables: Omit[]): Promise => { +export default (eggId: number, variables: Omit[]): Promise => { return new Promise((resolve, reject) => { http.patch( `/api/application/eggs/${eggId}/variables`, - variables.map(variable => { - return { - id: variable.id, - name: variable.name, - description: variable.description, - env_variable: variable.envVariable, - default_value: variable.defaultValue, - user_viewable: variable.userViewable, - user_editable: variable.userEditable, - rules: variable.rules, - }; - }), + variables.map(variable => ({ + id: variable.id, + name: variable.name, + description: variable.description, + env_variable: variable.envVariable, + default_value: variable.defaultValue, + user_viewable: variable.userViewable, + user_editable: variable.userEditable, + rules: variable.rules, + })), ) .then(({ data }) => resolve((data.data || []).map(rawDataToEggVariable))) .catch(reject); diff --git a/resources/scripts/components/admin/nests/eggs/EggRouter.tsx b/resources/scripts/components/admin/nests/eggs/EggRouter.tsx index 1d591869e..0e11921fa 100644 --- a/resources/scripts/components/admin/nests/eggs/EggRouter.tsx +++ b/resources/scripts/components/admin/nests/eggs/EggRouter.tsx @@ -1,54 +1,23 @@ import EggInstallContainer from '@/components/admin/nests/eggs/EggInstallContainer'; import EggVariablesContainer from '@/components/admin/nests/eggs/EggVariablesContainer'; -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { useLocation } from 'react-router'; import tw from 'twin.macro'; import { Route, Switch, useRouteMatch } from 'react-router-dom'; -import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy'; -import getEgg, { Egg } from '@/api/admin/eggs/getEgg'; +import getEgg from '@/api/admin/eggs/getEgg'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import Spinner from '@/components/elements/Spinner'; import FlashMessageRender from '@/components/FlashMessageRender'; -import { ApplicationStore } from '@/state'; import { SubNavigation, SubNavigationLink } from '@/components/admin/SubNavigation'; import EggSettingsContainer from '@/components/admin/nests/eggs/EggSettingsContainer'; -interface ctx { - egg: Egg | undefined; - setEgg: Action; -} - -export const Context = createContextStore({ - egg: undefined, - - setEgg: action((state, payload) => { - state.egg = payload; - }), -}); - const EggRouter = () => { const location = useLocation(); const match = useRouteMatch<{ id?: string }>(); - const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); - const [ loading, setLoading ] = useState(true); + const { data: egg } = getEgg(Number(match.params?.id)); - const egg = Context.useStoreState(state => state.egg); - const setEgg = Context.useStoreActions(actions => actions.setEgg); - - useEffect(() => { - clearFlashes('egg'); - - getEgg(Number(match.params?.id), [ 'variables' ]) - .then(egg => setEgg(egg)) - .catch(error => { - console.error(error); - clearAndAddHttpError({ key: 'egg', error }); - }) - .then(() => setLoading(false)); - }, []); - - if (loading || egg === undefined) { + if (egg === undefined) { return ( @@ -109,9 +78,5 @@ const EggRouter = () => { }; export default () => { - return ( - - - - ); + return ; }; diff --git a/resources/scripts/components/admin/nests/eggs/EggSettingsContainer.tsx b/resources/scripts/components/admin/nests/eggs/EggSettingsContainer.tsx index f8dc1c365..9ee665f15 100644 --- a/resources/scripts/components/admin/nests/eggs/EggSettingsContainer.tsx +++ b/resources/scripts/components/admin/nests/eggs/EggSettingsContainer.tsx @@ -162,7 +162,7 @@ const EggProcessContainer = forwardRef( -
+
{name}

}> - - + <> @@ -52,26 +59,38 @@ function EggVariableForm ({ variable: { name }, i }: { variable: EggVariable, i:
+ + ); +} + +function EggVariableBox ({ variable, prefix }: { variable: EggVariable, prefix: string }) { + const { isSubmitting } = useFormikContext(); + + return ( + {variable.name}

}> + + +
); } @@ -79,43 +98,33 @@ function EggVariableForm ({ variable: { name }, i }: { variable: EggVariable, i: export default function EggVariablesContainer ({ egg }: { egg: Egg }) { const { clearAndAddHttpError } = useFlash(); + const { mutate } = getEgg(egg.id); + const submit = (values: EggVariable[], { setSubmitting }: FormikHelpers) => { updateEggVariables(egg.id, values) - .then(variables => console.log(variables)) + .then(async (variables) => await mutate(egg => ({ ...egg!, relations: { variables: variables } }))) .catch(error => clearAndAddHttpError({ key: 'egg', error })) .then(() => setSubmitting(false)); }; - useEffect(() => { - console.log(egg.relations?.variables || []); - }, []); - return ( {({ isSubmitting, isValid }) => (
- {egg.relations?.variables?.map((v, i) => )} + {egg.relations?.variables?.map((v, i) => )}
-
+
-
diff --git a/resources/scripts/components/admin/nests/eggs/NewVariableButton.tsx b/resources/scripts/components/admin/nests/eggs/NewVariableButton.tsx new file mode 100644 index 000000000..45058c7bb --- /dev/null +++ b/resources/scripts/components/admin/nests/eggs/NewVariableButton.tsx @@ -0,0 +1,87 @@ +import { Form, Formik, FormikHelpers } from 'formik'; +import React, { useState } from 'react'; +import tw from 'twin.macro'; +import createEggVariable from '@/api/admin/eggs/createEggVariable'; +import getEgg from '@/api/admin/eggs/getEgg'; +import { EggVariableForm, validationSchema } from '@/components/admin/nests/eggs/EggVariablesContainer'; +import Modal from '@/components/elements/Modal'; +import FlashMessageRender from '@/components/FlashMessageRender'; +import Button from '@/components/elements/Button'; +import useFlash from '@/plugins/useFlash'; + +export default function NewVariableButton ({ eggId }: { eggId: number }) { + const [ visible, setVisible ] = useState(false); + const { clearFlashes, clearAndAddHttpError } = useFlash(); + + const { mutate } = getEgg(eggId); + + const submit = (values: any, { setSubmitting }: FormikHelpers) => { + clearFlashes('variable:create'); + + createEggVariable(eggId, values) + .then(async (v) => { + await mutate(egg => ({ ...egg!, relations: { variables: [ ...egg!.relations.variables!, v ] } })); + setVisible(false); + }) + .catch(error => { + clearAndAddHttpError({ key: 'variable:create', error }); + setSubmitting(false); + }); + }; + + return ( + <> + + {({ isSubmitting, resetForm }) => ( + { + resetForm(); + setVisible(false); + }} + > + + +

New Variable

+ + + + +
+ + +
+ +
+ )} +
+ + + + ); +} diff --git a/resources/scripts/components/admin/servers/ServerStartupContainer.tsx b/resources/scripts/components/admin/servers/ServerStartupContainer.tsx index d89230974..2184b217a 100644 --- a/resources/scripts/components/admin/servers/ServerStartupContainer.tsx +++ b/resources/scripts/components/admin/servers/ServerStartupContainer.tsx @@ -1,4 +1,4 @@ -import getEgg, { Egg, EggVariable } from '@/api/admin/eggs/getEgg'; +import { getEgg, Egg, EggVariable } from '@/api/admin/eggs/getEgg'; import { Server } from '@/api/admin/servers/getServers'; import updateServerStartup, { Values } from '@/api/admin/servers/updateServerStartup'; import EggSelect from '@/components/admin/servers/EggSelect'; @@ -187,7 +187,7 @@ export default function ServerStartupContainer ({ server }: { server: Server }) const setServer = Context.useStoreActions(actions => actions.setServer); useEffect(() => { - getEgg(server.eggId, [ 'variables' ]) + getEgg(server.eggId) .then(egg => setEgg(egg)) .catch(error => console.error(error)); }, []);