From 7239f0e33645361bcb2bc44495579536a681c8ce Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Fri, 1 Oct 2021 11:07:48 -0600 Subject: [PATCH] ui(admin): add egg variable elements --- .../components/admin/nests/eggs/EggRouter.tsx | 2 +- .../nests/eggs/EggVariablesContainer.tsx | 95 ++++++++++++++++++- .../scripts/components/elements/Checkbox.tsx | 43 ++++----- .../scripts/components/elements/Field.tsx | 42 ++++++-- .../scripts/components/elements/Label.tsx | 5 +- .../components/server/users/PermissionRow.tsx | 42 +++++++- 6 files changed, 185 insertions(+), 44 deletions(-) diff --git a/resources/scripts/components/admin/nests/eggs/EggRouter.tsx b/resources/scripts/components/admin/nests/eggs/EggRouter.tsx index 8ac6f15de..1d591869e 100644 --- a/resources/scripts/components/admin/nests/eggs/EggRouter.tsx +++ b/resources/scripts/components/admin/nests/eggs/EggRouter.tsx @@ -39,7 +39,7 @@ const EggRouter = () => { useEffect(() => { clearFlashes('egg'); - getEgg(Number(match.params?.id)) + getEgg(Number(match.params?.id), [ 'variables' ]) .then(egg => setEgg(egg)) .catch(error => { console.error(error); diff --git a/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx b/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx index 76a88ed33..41388a07e 100644 --- a/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx +++ b/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx @@ -1,11 +1,96 @@ +import { Form, Formik, useFormikContext } from 'formik'; import React from 'react'; +import tw from 'twin.macro'; +import { object } from 'yup'; +import { Egg, EggVariable } from '@/api/admin/eggs/getEgg'; import AdminBox from '@/components/admin/AdminBox'; -import { Egg } from '@/api/admin/eggs/getEgg'; +import Checkbox from '@/components/elements/Checkbox'; +import Field, { FieldRow, TextareaField } from '@/components/elements/Field'; +import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; + +function EggVariableForm ({ variable: { name } }: { variable: EggVariable }) { + const { isSubmitting } = useFormikContext(); -export default ({ egg }: { egg: Egg }) => { return ( - - {egg.name} + {name}

}> + + + + + + + + + + + + +
+ + + +
+ +
); -}; +} + +export default function EggVariablesContainer ({ egg }: { egg: Egg }) { + const submit = () => { + // + }; + + return ( + +
+
+ {egg.relations?.variables?.map(v => )} +
+
+
+ ); +} diff --git a/resources/scripts/components/elements/Checkbox.tsx b/resources/scripts/components/elements/Checkbox.tsx index 790536489..b3954e708 100644 --- a/resources/scripts/components/elements/Checkbox.tsx +++ b/resources/scripts/components/elements/Checkbox.tsx @@ -1,42 +1,35 @@ +import Label from '@/components/elements/Label'; import React from 'react'; import { Field, FieldProps } from 'formik'; import Input from '@/components/elements/Input'; +import tw from 'twin.macro'; interface Props { name: string; - value: string; + label?: string; className?: string; } -type OmitFields = 'ref' | 'name' | 'value' | 'type' | 'checked' | 'onClick' | 'onChange'; +type OmitFields = 'ref' | 'name' | 'value' | 'type'; type InputProps = Omit; -const Checkbox = ({ name, value, className, ...props }: Props & InputProps) => ( +const Checkbox = ({ name, label, className, ...props }: Props & InputProps) => ( - {({ field, form }: FieldProps) => { - if (!Array.isArray(field.value)) { - console.error('Attempting to mount a checkbox using a field value that is not an array.'); - - return null; - } - + {({ field }: FieldProps) => { return ( - form.setFieldTouched(field.name, true)} - onChange={e => { - const set = new Set(field.value); - set.has(value) ? set.delete(value) : set.add(value); - - field.onChange(e); - form.setFieldValue(field.name, Array.from(set)); - }} - /> +
+ + {label && +
+ +
} +
); }}
diff --git a/resources/scripts/components/elements/Field.tsx b/resources/scripts/components/elements/Field.tsx index 5840983ed..6d35e6b89 100644 --- a/resources/scripts/components/elements/Field.tsx +++ b/resources/scripts/components/elements/Field.tsx @@ -1,9 +1,9 @@ -import React, { forwardRef } from 'react'; import { Field as FormikField, FieldProps } from 'formik'; +import React, { forwardRef } from 'react'; +import tw, { styled } from 'twin.macro'; import Input, { Textarea } from '@/components/elements/Input'; -import Label from '@/components/elements/Label'; import InputError from '@/components/elements/InputError'; -import tw from 'twin.macro'; +import Label from '@/components/elements/Label'; interface OwnProps { name: string; @@ -11,15 +11,17 @@ interface OwnProps { label?: string; description?: string; validate?: (value: any) => undefined | string | Promise; + + className?: string; } type Props = OwnProps & Omit, 'name'>; -const Field = forwardRef(({ id, name, light = false, label, description, validate, ...props }, ref) => ( +const Field = forwardRef(({ id, name, light = false, label, description, validate, className, ...props }, ref) => ( { ({ field, form: { errors, touched } }: FieldProps) => ( -
+
{label &&
@@ -43,15 +45,17 @@ const Field = forwardRef(({ id, name, light = false, la )); Field.displayName = 'Field'; -type Props2 = OwnProps & Omit, 'name'>; +export default Field; -export const TextareaField = forwardRef( - function TextareaField ({ id, name, light = false, label, description, validate, ...props }, ref) { +type TextareaProps = OwnProps & Omit, 'name'>; + +export const TextareaField = forwardRef( + function TextareaField ({ id, name, light = false, label, description, validate, className, ...props }, ref) { return ( { ({ field, form: { errors, touched } }: FieldProps) => ( -
+
{label && }