Skip to content

Commit fc1167b

Browse files
committed
Updated PropTypes, added feedSourceTypeOptions and dynamic form handling in FeedSourceManager
1 parent cd17921 commit fc1167b

File tree

1 file changed

+87
-15
lines changed

1 file changed

+87
-15
lines changed

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

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { React, useEffect, useState } from "react";
1+
import { cloneElement, React, useEffect, useState } from "react";
22
import { useTranslation } from "react-i18next";
33
import PropTypes from "prop-types";
44
import { useNavigate } from "react-router-dom";
55
import FeedSourceForm from "./feed-source-form";
66
import {
77
usePostV2FeedSourcesMutation,
8-
usePutV2FeedSourcesByIdMutation
8+
usePutV2FeedSourcesByIdMutation,
99
} from "../../redux/api/api.generated.ts";
1010
import {
1111
displaySuccess,
1212
displayError,
1313
} from "../util/list/toast-component/display-toast";
14+
import EventDatabaseApiFeedTypeTemplate from "./feed-source-type-templates/EventDatabaseApiFeedType.template";
15+
import NotifiedFeedTypeTemplate from "./feed-source-type-templates/NotifiedFeedType.template";
16+
import SparkleIOFeedTypeTemplate from "./feed-source-type-templates/SparkleIOFeedType.template";
17+
import CalendarFeedTypeTemplate from "./feed-source-type-templates/CalendarFeedType.template";
1418

1519
/**
1620
* The theme manager component.
@@ -31,7 +35,9 @@ function FeedSourceManager({
3135
initialState = null,
3236
}) {
3337
// Hooks
34-
const { t } = useTranslation("common", { keyPrefix: "feed-source-manager" });
38+
const { t } = useTranslation("common", {
39+
keyPrefix: "feed-source-manager",
40+
});
3541
const navigate = useNavigate();
3642

3743
// State
@@ -43,28 +49,58 @@ function FeedSourceManager({
4349
t("loading-messages.loading-feed-source")
4450
);
4551

52+
const [dynamicFormElement, setDynamicFormElement] = useState();
4653
const [submitting, setSubmitting] = useState(false);
47-
const [formStateObject, setFormStateObject] = useState({
48-
title: "",
49-
description: "",
50-
modifiedBy: "",
51-
createdBy: "",
52-
css: "",
53-
});
54+
const [formStateObject, setFormStateObject] = useState();
5455

55-
const [postV2FeedSources, { error: saveErrorPost, isSuccess: isSaveSuccessPost }] = usePostV2FeedSourcesMutation();
56+
const [
57+
postV2FeedSources,
58+
{ error: saveErrorPost, isSuccess: isSaveSuccessPost },
59+
] = usePostV2FeedSourcesMutation();
5660

5761
const [
5862
PutV2FeedSourcesById,
5963
{ error: saveErrorPut, isSuccess: isSaveSuccessPut },
6064
] = usePutV2FeedSourcesByIdMutation();
6165

66+
const feedSourceTypeOptions = [
67+
{
68+
value: "App\\Feed\\EventDatabaseApiFeedType",
69+
title: "Eventdatabase feed",
70+
key: "1",
71+
template: <EventDatabaseApiFeedTypeTemplate />,
72+
},
73+
{
74+
value: "App\\Feed\\NotifiedFeedType",
75+
title: "Notified feed",
76+
key: "2",
77+
template: <NotifiedFeedTypeTemplate />,
78+
},
79+
{
80+
value: "App\\Feed\\CalendarApiFeedType",
81+
title: "Kalender feed",
82+
key: "3",
83+
template: <CalendarFeedTypeTemplate />,
84+
},
85+
{
86+
value: "App\\Feed\\SparkeIOFeedType",
87+
title: "Instagram feed",
88+
key: "4",
89+
template: <SparkleIOFeedTypeTemplate />,
90+
},
91+
{
92+
value: "App\\Feed\\RssFeedType",
93+
title: "RSS feed",
94+
key: "5",
95+
template: null,
96+
},
97+
];
98+
6299
/** Set loaded data into form state. */
63100
useEffect(() => {
64101
setFormStateObject(initialState);
65102
}, [initialState]);
66103

67-
68104
/** Save feed source. */
69105
function saveFeedSource() {
70106
setLoadingMessage(t("loading-messages.saving-feed-source"));
@@ -75,9 +111,14 @@ function FeedSourceManager({
75111
supportedFeedOutputType: formStateObject.supportedFeedOutputType,
76112
};
77113
if (saveMethod === "POST") {
78-
postV2FeedSources({ feedSourceFeedSourceInput: JSON.stringify(saveData) });
114+
postV2FeedSources({
115+
feedSourceFeedSourceInput: JSON.stringify(saveData),
116+
});
79117
} else if (saveMethod === "PUT") {
80-
PutV2FeedSourcesById({ feedSourceFeedSourceInput: JSON.stringify(saveData), id });
118+
PutV2FeedSourcesById({
119+
feedSourceFeedSourceInput: JSON.stringify(saveData),
120+
id,
121+
});
81122
}
82123
}
83124

@@ -93,6 +134,26 @@ function FeedSourceManager({
93134
setFormStateObject(localFormStateObject);
94135
};
95136

137+
138+
useEffect(() => {
139+
if (formStateObject) {
140+
const option = feedSourceTypeOptions.find(
141+
(opt) => opt.value === formStateObject.feedType
142+
);
143+
if (option && option.template) {
144+
setDynamicFormElement(
145+
cloneElement(option.template, {
146+
handleInput,
147+
formStateObject,
148+
t,
149+
})
150+
);
151+
} else {
152+
setDynamicFormElement(null);
153+
}
154+
}
155+
}, [formStateObject ? formStateObject : null]);
156+
96157
/** If the feed source is not loaded, display the error message */
97158
useEffect(() => {
98159
if (loadingError) {
@@ -137,6 +198,8 @@ function FeedSourceManager({
137198
handleSubmit={handleSubmit}
138199
isLoading={isLoading || submitting}
139200
loadingMessage={loadingMessage}
201+
feedSourceTypeOptions={feedSourceTypeOptions}
202+
dynamicFormElement={dynamicFormElement}
140203
/>
141204
)}
142205
</>
@@ -145,7 +208,16 @@ function FeedSourceManager({
145208

146209
FeedSourceManager.propTypes = {
147210
initialState: PropTypes.shape({
148-
logo: PropTypes.shape({}),
211+
title: PropTypes.string,
212+
description: PropTypes.string,
213+
feedType: PropTypes.string,
214+
feedSourceType: PropTypes.string,
215+
host: PropTypes.string,
216+
token: PropTypes.string,
217+
baseUrl: PropTypes.string,
218+
clientId: PropTypes.string,
219+
clientSecret: PropTypes.string,
220+
feedSources: PropTypes.string,
149221
}),
150222
saveMethod: PropTypes.string.isRequired,
151223
id: PropTypes.string,

0 commit comments

Comments
 (0)