/* ============================================================================ * Copyright (c) Cloud Annotations * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ import React, { useState, useEffect } from "react"; import { nanoid } from "@reduxjs/toolkit"; import { useTypedDispatch, useTypedSelector } from "../hooks"; import FormItem from "./../FormItem"; import FormMultiSelect from "./../FormMultiSelect"; import FormSelect from "./../FormSelect"; import FormTextInput from "./../FormTextInput"; import { Param, setParam } from "./slice"; import styles from "./styles.module.css"; interface ParamProps { param: Param; } function ParamOption({ param }: ParamProps) { if (param.schema?.type === "array" && param.schema.items?.enum) { return ; } if (param.schema?.type === "array") { return ; } if (param.schema?.enum) { return ; } if (param.schema?.type === "boolean") { return ; } // integer, number, string, int32, int64, float, double, object, byte, binary, // date-time, date, password return ; } function ParamOptionWrapper({ param }: ParamProps) { return ( ); } function ParamOptions() { const [showOptional, setShowOptional] = useState(false); const pathParams = useTypedSelector((state) => state.params.path); const queryParams = useTypedSelector((state) => state.params.query); const cookieParams = useTypedSelector((state) => state.params.cookie); const headerParams = useTypedSelector((state) => state.params.header); const allParams = [ ...pathParams, ...queryParams, ...cookieParams, ...headerParams, ]; const requiredParams = allParams.filter((p) => p.required); const optionalParams = allParams.filter((p) => !p.required); return ( <> {/* Required Parameters */} {requiredParams.map((param) => ( ))} {/* Optional Parameters */} {optionalParams.length > 0 && ( <>
{optionalParams.map((param) => ( ))}
)} ); } function ArrayItem({ param, onChange, }: ParamProps & { onChange(value?: string): any }) { if (param.schema?.items?.type === "boolean") { return ( { const val = e.target.value; onChange(val === "---" ? undefined : val); }} /> ); } return ( { onChange(e.target.value); }} /> ); } function ParamArrayFormItem({ param }: ParamProps) { const [items, setItems] = useState<{ id: string; value?: string }[]>([]); const dispatch = useTypedDispatch(); function handleAddItem() { setItems((i) => [ ...i, { id: nanoid(), }, ]); } useEffect(() => { const values = items .map((item) => item.value) .filter((item): item is string => !!item); dispatch( setParam({ ...param, value: values.length > 0 ? values : undefined, }) ); // eslint-disable-next-line react-hooks/exhaustive-deps }, [items]); function handleDeleteItem(itemToDelete: { id: string }) { return () => { const newItems = items.filter((i) => i.id !== itemToDelete.id); setItems(newItems); }; } function handleChangeItem(itemToUpdate: { id: string }) { return (value: string) => { const newItems = items.map((i) => { if (i.id === itemToUpdate.id) { return { ...i, value: value }; } return i; }); setItems(newItems); }; } return ( <> {items.map((item) => (
))} ); } function ParamSelectFormItem({ param }: ParamProps) { const dispatch = useTypedDispatch(); const options = param.schema?.enum ?? []; return ( { const val = e.target.value; dispatch( setParam({ ...param, value: val === "---" ? undefined : val, }) ); }} /> ); } function ParamBooleanFormItem({ param }: ParamProps) { const dispatch = useTypedDispatch(); return ( { const val = e.target.value; dispatch( setParam({ ...param, value: val === "---" ? undefined : val, }) ); }} /> ); } function ParamMultiSelectFormItem({ param }: ParamProps) { const dispatch = useTypedDispatch(); const options = param.schema?.items?.enum ?? []; return ( { const values = Array.prototype.filter .call(e.target.options, (o) => o.selected) .map((o) => o.value); dispatch( setParam({ ...param, value: values.length > 0 ? values : undefined, }) ); }} /> ); } function ParamTextFormItem({ param }: ParamProps) { const dispatch = useTypedDispatch(); return ( dispatch(setParam({ ...param, value: e.target.value }))} /> ); } export default ParamOptions;