-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use AuthFields to for git password and username
fix: fix issue withEnvVarSource not working
- Loading branch information
1 parent
6509482
commit 178444f
Showing
7 changed files
with
339 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/components/Forms/Formik/FormikCompactEnvVarSource.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { useField } from "formik"; | ||
import { useEffect, useState } from "react"; | ||
import { Switch } from "../../Switch"; | ||
import { | ||
EnvVarSourceType, | ||
FormikEnvVarSourceProps, | ||
configmapValueRegex, | ||
secretValueRegex | ||
} from "./FormikEnvVarSource"; | ||
import FormikEnvVarK8SView from "./utils/FormikEnvVarK8SView"; | ||
import FormikEnvVarStaticView from "./utils/FormikEnvVarStaticView"; | ||
|
||
export function FormikCompactEnvVarSource({ | ||
className, | ||
variant = "small", | ||
name, | ||
label, | ||
hint, | ||
disabled, | ||
readOnly, | ||
required, | ||
...props | ||
}: FormikEnvVarSourceProps) { | ||
const [type, setType] = useState<EnvVarSourceType>("Static"); | ||
const prefix = `${name}.${type === "ConfigMap" ? "configmap" : "secret"}`; | ||
const [data, setData] = useState({ | ||
static: "", | ||
name: "", | ||
key: "" | ||
}); | ||
const [field, meta] = useField({ | ||
name: name!, | ||
type: "text", | ||
required, | ||
validate: (value) => { | ||
if (required && !value) { | ||
return "This field is required"; | ||
} | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
const value = getValue(); | ||
if (field.value !== value) { | ||
field.onChange({ | ||
target: { | ||
name, | ||
value | ||
} | ||
}); | ||
} | ||
}, [data]); | ||
|
||
useEffect(() => { | ||
if (field.value === getValue()) { | ||
return; | ||
} | ||
let value = | ||
field.value?.match(configmapValueRegex) || | ||
field.value?.match(secretValueRegex); | ||
if (value?.length === 3 && field.value?.includes("configmap")) { | ||
setData({ | ||
static: "", | ||
key: value[2], | ||
name: value[1] | ||
}); | ||
setType("ConfigMap"); | ||
return; | ||
} | ||
if (value?.length === 3 && field.value?.includes("secret")) { | ||
setData({ | ||
static: "", | ||
key: value[2], | ||
name: value[1] | ||
}); | ||
setType("Secret"); | ||
return; | ||
} | ||
setData({ | ||
static: field.value, | ||
key: "", | ||
name: "" | ||
}); | ||
setType("Static"); | ||
}, []); | ||
|
||
const getValue = () => { | ||
let value = ""; | ||
if (type === "Static") { | ||
value = data.static; | ||
} | ||
if (type === "Secret" && data.name && data.key) { | ||
value = `secret://${data.name}/${data.key}`; | ||
} | ||
if (type === "ConfigMap" && data.name && data.key) { | ||
value = `configmap://${data.name}/${data.key}`; | ||
} | ||
return value; | ||
}; | ||
|
||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<label className="font-semibold text-sm">{label}</label> | ||
<div className="flex flex-row gap-2"> | ||
<div className="w-full"> | ||
{type === "Static" ? ( | ||
<FormikEnvVarStaticView | ||
name="name" | ||
variant="small" | ||
disabled={disabled} | ||
readOnly={readOnly} | ||
data={data} | ||
setData={setData} | ||
/> | ||
) : ( | ||
<FormikEnvVarK8SView | ||
prefix={prefix} | ||
data={data} | ||
setData={setData} | ||
className="flex flex-row gap-2 items-center" | ||
/> | ||
)} | ||
{meta.touched && meta.error ? ( | ||
<p className="text-sm text-red-500 w-full py-1">{meta.error}</p> | ||
) : null} | ||
</div> | ||
<Switch | ||
options={["Static", "Secret", "ConfigMap"]} | ||
defaultValue={"None"} | ||
value={type} | ||
onChange={(v) => { | ||
if (readOnly || disabled) { | ||
return; | ||
} | ||
setData({ | ||
static: "", | ||
key: "", | ||
name: "" | ||
}); | ||
setType(v as EnvVarSourceType); | ||
}} | ||
className="w-[24rem]" | ||
/> | ||
</div> | ||
{hint && <p className="text-sm text-gray-500 py-1">{hint}</p>} | ||
</div> | ||
); | ||
} |
Oops, something went wrong.