Skip to content

Commit 265af91

Browse files
committed
Refactor secrets setting and translate string IDs
1 parent 232b931 commit 265af91

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

src/components/feed-sources/feed-source-manager.jsx

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
usePutV2FeedSourcesByIdMutation,
99
} from "../../redux/api/api.generated.ts";
1010
import {
11-
displaySuccess,
1211
displayError,
12+
displaySuccess,
1313
} from "../util/list/toast-component/display-toast";
1414
import EventDatabaseApiFeedTypeTemplate from "./feed-source-type-templates/EventDatabaseApiFeedType.template";
1515
import NotifiedFeedTypeTemplate from "./feed-source-type-templates/NotifiedFeedType.template";
@@ -22,9 +22,8 @@ import CalendarFeedTypeTemplate from "./feed-source-type-templates/CalendarFeedT
2222
* @param {object} props.initialState Initial theme state.
2323
* @param {string} props.saveMethod POST or PUT.
2424
* @param {string | null} props.id Theme id.
25-
* @param {boolean} props.isLoading Is the theme state loading?
25+
* @param {boolean} props.isLoading Is the theme state loading
2626
* @param {object} props.loadingError Loading error.
27-
* @param props.mode
2827
* @returns {object} The theme form.
2928
*/
3029
function FeedSourceManager({
@@ -66,25 +65,28 @@ function FeedSourceManager({
6665
const feedSourceTypeOptions = [
6766
{
6867
value: "App\\Feed\\EventDatabaseApiFeedType",
69-
title: t("dynamic-fields.EventDatabaseApiFeedType.title"),
68+
title: t("dynamic-fields.event-database-api-feed-type.title"),
7069
key: "1",
70+
secrets: "host, test",
7171
template: <EventDatabaseApiFeedTypeTemplate mode={saveMethod} />,
7272
},
7373
{
7474
value: "App\\Feed\\NotifiedFeedType",
75-
title: t("dynamic-fields.NotifiedFeedType.title"),
75+
title: t("dynamic-fields.notified-feed-type.title"),
7676
key: "2",
77+
secrets: "token",
7778
template: <NotifiedFeedTypeTemplate mode={saveMethod} />,
7879
},
7980
{
8081
value: "App\\Feed\\CalendarApiFeedType",
81-
title: t("dynamic-fields.CalendarApiFeedType.title"),
82+
title: t("dynamic-fields.calendar-api-feed-type.title"),
8283
key: "3",
84+
secrets: "resources",
8385
template: <CalendarFeedTypeTemplate mode={saveMethod} />,
8486
},
8587
{
8688
value: "App\\Feed\\RssFeedType",
87-
title: t("dynamic-fields.RssFeedType.title"),
89+
title: t("dynamic-fields.rss-feed-type.title"),
8890
key: "5",
8991
template: null,
9092
},
@@ -96,33 +98,55 @@ function FeedSourceManager({
9698
}, [initialState]);
9799

98100
useEffect(() => {
99-
if (formStateObject && formStateObject.feedType) {
100-
let newSecrets = {};
101-
102-
switch (formStateObject.feedType) {
103-
case "App\\Feed\\EventDatabaseApiFeedType":
104-
newSecrets =
105-
formStateObject.host === "" ? [] : { host: formStateObject.host };
106-
break;
107-
case "App\\Feed\\NotifiedFeedType":
108-
newSecrets =
109-
formStateObject.token === ""
110-
? []
111-
: { token: formStateObject.token };
112-
break;
113-
case "App\\Feed\\CalendarApiFeedType":
114-
newSecrets =
115-
formStateObject.resources === ""
116-
? []
117-
: { resources: formStateObject.resources };
118-
break;
119-
default:
120-
break;
101+
if (formStateObject) {
102+
const option = feedSourceTypeOptions.find(
103+
(opt) => opt.value === formStateObject.feedType
104+
);
105+
if (option && option.template) {
106+
setDynamicFormElement(
107+
cloneElement(option.template, {
108+
handleInput,
109+
formStateObject,
110+
})
111+
);
112+
} else {
113+
setDynamicFormElement(null);
121114
}
122-
formStateObject.secrets = newSecrets;
123115
}
124116
}, [formStateObject]);
125117

118+
/** When a feedType is selected, add the required secrets to formStateObject */
119+
/* useEffect(() => {
120+
if (formStateObject?.feedType) {
121+
const selectedFeedTypeSecret =
122+
formStateObject?.feedType &&
123+
feedSourceTypeOptions.find(
124+
(option) => option.value === formStateObject.feedType
125+
)?.secret;
126+
127+
formStateObject.secrets = selectedFeedTypeSecret
128+
? { [selectedFeedTypeSecret]: "" }
129+
: [];
130+
}
131+
}, [formStateObject?.feedType]);*/
132+
133+
useEffect(() => {
134+
if (formStateObject?.feedType) {
135+
const selectedFeedTypeSecret = feedSourceTypeOptions.find(
136+
(option) => option.value === formStateObject.feedType
137+
).secrets;
138+
139+
const secretsArray = selectedFeedTypeSecret
140+
.split(",")
141+
.map((prop) => prop.trim());
142+
143+
formStateObject.secrets = secretsArray?.reduce((acc, secret) => {
144+
acc[secret] = formStateObject[secret] || "";
145+
return acc;
146+
}, {});
147+
}
148+
}, [formStateObject?.feedType]);
149+
126150
/** Save feed source. */
127151
function saveFeedSource() {
128152
setLoadingMessage(t("loading-messages.saving-feed-source"));
@@ -150,25 +174,6 @@ function FeedSourceManager({
150174
setFormStateObject(localFormStateObject);
151175
};
152176

153-
useEffect(() => {
154-
if (formStateObject) {
155-
const option = feedSourceTypeOptions.find(
156-
(opt) => opt.value === formStateObject.feedType
157-
);
158-
if (option && option.template) {
159-
setDynamicFormElement(
160-
cloneElement(option.template, {
161-
handleInput,
162-
formStateObject,
163-
t,
164-
})
165-
);
166-
} else {
167-
setDynamicFormElement(null);
168-
}
169-
}
170-
}, [formStateObject || null]);
171-
172177
/** If the feed source is not loaded, display the error message */
173178
useEffect(() => {
174179
if (loadingError) {

0 commit comments

Comments
 (0)