2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { translate, TFunction } from "react-i18next";
|
|
|
|
|
import { Select } from "@scm-manager/ui-components";
|
|
|
|
|
import { NamespaceStrategies } from "@scm-manager/ui-types";
|
2019-03-11 14:48:48 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
namespaceStrategies: NamespaceStrategies;
|
|
|
|
|
label: string;
|
|
|
|
|
value?: string;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
helpText?: string;
|
|
|
|
|
onChange: (value: string, name?: string) => void;
|
2019-03-11 14:48:48 +01:00
|
|
|
// context props
|
2019-10-19 16:38:07 +02:00
|
|
|
t: TFunction;
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class NamespaceStrategySelect extends React.Component<Props> {
|
|
|
|
|
createNamespaceOptions = () => {
|
|
|
|
|
const { namespaceStrategies, t } = this.props;
|
|
|
|
|
let available = [];
|
|
|
|
|
if (namespaceStrategies && namespaceStrategies.available) {
|
|
|
|
|
available = namespaceStrategies.available;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return available.map(ns => {
|
2019-10-20 18:02:52 +02:00
|
|
|
const key = "namespaceStrategies." + ns;
|
2019-03-13 09:38:56 +01:00
|
|
|
let label = t(key);
|
2019-03-11 14:48:48 +01:00
|
|
|
if (label === key) {
|
|
|
|
|
label = ns;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
value: ns,
|
2019-10-20 18:02:52 +02:00
|
|
|
label: label
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-12 14:35:22 +01:00
|
|
|
findSelected = () => {
|
|
|
|
|
const { namespaceStrategies, value } = this.props;
|
2019-10-21 10:57:56 +02:00
|
|
|
if (!namespaceStrategies || !namespaceStrategies.available || namespaceStrategies.available.indexOf(value) < 0) {
|
2019-03-12 14:35:22 +01:00
|
|
|
return namespaceStrategies.current;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-11 14:48:48 +01:00
|
|
|
render() {
|
2019-03-12 14:35:22 +01:00
|
|
|
const { label, helpText, disabled, onChange } = this.props;
|
2019-03-11 14:48:48 +01:00
|
|
|
const nsOptions = this.createNamespaceOptions();
|
|
|
|
|
return (
|
|
|
|
|
<Select
|
|
|
|
|
label={label}
|
|
|
|
|
onChange={onChange}
|
2019-03-12 14:35:22 +01:00
|
|
|
value={this.findSelected()}
|
2019-03-11 14:48:48 +01:00
|
|
|
disabled={disabled}
|
|
|
|
|
options={nsOptions}
|
|
|
|
|
helpText={helpText}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
export default translate("plugins")(NamespaceStrategySelect);
|