jscheffl commented on code in PR #44942: URL: https://github.com/apache/airflow/pull/44942#discussion_r1885759686
########## airflow/ui/src/pages/Variables/AddVariableForm.tsx: ########## @@ -0,0 +1,119 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Box, HStack, Input, Spacer, Text } from "@chakra-ui/react"; +import { useEffect, useMemo } from "react"; +import { Controller, useForm } from "react-hook-form"; +import { FiSave } from "react-icons/fi"; + +import { ErrorAlert } from "src/components/ErrorAlert"; +import { Button } from "src/components/ui"; +import { useAddVariable } from "src/queries/useAddVariable"; + +export type AddVariableBody = { + description: string | undefined; + key: string; + value: string; +}; + +type AddVariableFormProps = { + onClose: () => void; +}; + +const AddVariableForm: React.FC<AddVariableFormProps> = ({ onClose }) => { + const { addVariable, error, isPending } = useAddVariable(onClose); + const addVariableParams: AddVariableBody = useMemo( + () => ({ + description: undefined, + key: "", + value: "", + }), + [], + ); + + const { + control, + formState: { isDirty }, + handleSubmit, + reset, + watch, + } = useForm<AddVariableBody>({ + defaultValues: addVariableParams, + }); + + const { key, value } = watch(); + + useEffect(() => { + reset(addVariableParams); + }, [addVariableParams, reset]); + + const onSubmit = (data: AddVariableBody) => { + addVariable(data); + }; + + return ( + <> + <ErrorAlert error={error} /> + <Text fontSize="md" mb={2} mt={4}> + Key <span style={{ color: "red" }}>*</span> + </Text> + <Controller + control={control} + name="key" + render={({ field }) => <Input {...field} required size="sm" />} + /> + + <Text fontSize="md" mb={2} mt={4}> + Value <span style={{ color: "red" }}>*</span> + </Text> + <Controller + control={control} + name="value" + render={({ field }) => <Input {...field} required size="sm" />} + /> + + <Text fontSize="md" mb={2} mt={4}> + Description + </Text> + <Controller + control={control} + name="description" + render={({ field }) => <Input {...field} required size="sm" />} Review Comment: Descriptions should not be required... which by surprise it is not ad point of display. It is accepted if I leave it empty. ```suggestion render={({ field }) => <Input {...field} size="sm" />} ``` ########## airflow/ui/src/queries/useAddVariable.ts: ########## @@ -0,0 +1,67 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useQueryClient } from "@tanstack/react-query"; +import { useState } from "react"; + +import { + useVariableServiceGetVariablesKey, + useVariableServicePostVariable, +} from "openapi/queries"; +import { toaster } from "src/components/ui"; +import type { AddVariableBody } from "src/pages/Variables/AddVariableForm"; + +export const useAddVariable = (onClose: () => void) => { + const queryClient = useQueryClient(); + const [error, setError] = useState<unknown>(undefined); + + const onSuccess = async () => { + await queryClient.invalidateQueries({ + queryKey: [useVariableServiceGetVariablesKey], + }); + + toaster.create({ + description: "Variable has been added successfully", + title: "Variable Add Request Submitted", + type: "success", + }); + + onClose(); + }; + + const onError = (_error: unknown) => { + setError(_error); + }; + + const { isPending, mutate } = useVariableServicePostVariable({ + onError, + onSuccess, + }); + + const addVariable = (addVariableRequestBody: AddVariableBody) => { + mutate({ Review Comment: If I add a variable which is already existing it is silently overwritten w/o showing an error. Can you please change the behavior such that adding is not silently overwriting an existing? Showing an error like the legacy might be a good option. ########## airflow/ui/src/pages/Variables/AddVariableForm.tsx: ########## @@ -0,0 +1,119 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Box, HStack, Input, Spacer, Text } from "@chakra-ui/react"; +import { useEffect, useMemo } from "react"; +import { Controller, useForm } from "react-hook-form"; +import { FiSave } from "react-icons/fi"; + +import { ErrorAlert } from "src/components/ErrorAlert"; +import { Button } from "src/components/ui"; +import { useAddVariable } from "src/queries/useAddVariable"; + +export type AddVariableBody = { + description: string | undefined; + key: string; + value: string; +}; + +type AddVariableFormProps = { + onClose: () => void; +}; + +const AddVariableForm: React.FC<AddVariableFormProps> = ({ onClose }) => { + const { addVariable, error, isPending } = useAddVariable(onClose); + const addVariableParams: AddVariableBody = useMemo( + () => ({ + description: undefined, + key: "", + value: "", + }), + [], + ); + + const { + control, + formState: { isDirty }, + handleSubmit, + reset, + watch, + } = useForm<AddVariableBody>({ + defaultValues: addVariableParams, + }); + + const { key, value } = watch(); + + useEffect(() => { + reset(addVariableParams); + }, [addVariableParams, reset]); + + const onSubmit = (data: AddVariableBody) => { + addVariable(data); + }; + + return ( + <> + <ErrorAlert error={error} /> + <Text fontSize="md" mb={2} mt={4}> + Key <span style={{ color: "red" }}>*</span> + </Text> + <Controller + control={control} + name="key" + render={({ field }) => <Input {...field} required size="sm" />} Review Comment: The DB schema is limiting the volume, if I add data longer than the DB can hold, an internal server error is raised.  Can you limit the entry boxes to the DB limits? And is there abetter error handling in FastAPI needed (==such that not internal server error is returned, better in a different PR) or is it not correctly handled in the form? ########## airflow/ui/src/pages/Variables/AddVariableForm.tsx: ########## @@ -0,0 +1,119 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Box, HStack, Input, Spacer, Text } from "@chakra-ui/react"; +import { useEffect, useMemo } from "react"; +import { Controller, useForm } from "react-hook-form"; +import { FiSave } from "react-icons/fi"; + +import { ErrorAlert } from "src/components/ErrorAlert"; +import { Button } from "src/components/ui"; +import { useAddVariable } from "src/queries/useAddVariable"; + +export type AddVariableBody = { + description: string | undefined; + key: string; + value: string; +}; + +type AddVariableFormProps = { + onClose: () => void; +}; + +const AddVariableForm: React.FC<AddVariableFormProps> = ({ onClose }) => { + const { addVariable, error, isPending } = useAddVariable(onClose); + const addVariableParams: AddVariableBody = useMemo( + () => ({ + description: undefined, + key: "", + value: "", + }), + [], + ); + + const { + control, + formState: { isDirty }, + handleSubmit, + reset, + watch, + } = useForm<AddVariableBody>({ + defaultValues: addVariableParams, + }); + + const { key, value } = watch(); + + useEffect(() => { + reset(addVariableParams); + }, [addVariableParams, reset]); + + const onSubmit = (data: AddVariableBody) => { + addVariable(data); + }; + + return ( + <> + <ErrorAlert error={error} /> + <Text fontSize="md" mb={2} mt={4}> + Key <span style={{ color: "red" }}>*</span> + </Text> + <Controller + control={control} + name="key" + render={({ field }) => <Input {...field} required size="sm" />} + /> + + <Text fontSize="md" mb={2} mt={4}> + Value <span style={{ color: "red" }}>*</span> + </Text> + <Controller + control={control} + name="value" + render={({ field }) => <Input {...field} required size="sm" />} Review Comment: In the legacy UI buth value and description are multi-line fields. Can you adjust this? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
