Skip to content

Commit fb58dfe

Browse files
authored
Merge pull request #269 from os2display/feature/2826-2
Adjustments
2 parents 66f7c24 + f79221b commit fb58dfe

File tree

8 files changed

+91
-92
lines changed

8 files changed

+91
-92
lines changed

e2e/feed-sources.spec.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const feedSourcesJson = {
2626
{
2727
"@id": "/v2/feed-sources/01JB9MSQEH75HC3GG75XCVP2WH",
2828
"@type": "FeedSource",
29-
title: "Ny feed source test 3",
30-
description: "Ny feed source test 3",
29+
title: "Ny datakilde test 3",
30+
description: "Ny datakilde test 3",
3131
outputType: "",
3232
feedType: "App\\Feed\\RssFeedType",
3333
secrets: [],
@@ -75,7 +75,7 @@ const feedSourcesJson = {
7575
{
7676
"@id": "/v2/feed-sources/01JB1DH8G4CXKGX5JRTYDHDPSP",
7777
"@type": "FeedSource",
78-
title: "Calendar feed source test",
78+
title: "Calendar datakilde test",
7979
description: "test",
8080
outputType: "",
8181
feedType: "App\\Feed\\CalendarApiFeedType",
@@ -217,8 +217,8 @@ test.describe("fest", () => {
217217
await page.locator("#login").click();
218218
});
219219

220-
test("It loads create feed source page", async ({ page }) => {
221-
page.getByText("Opret ny feed source").click();
220+
test("It loads create datakilde page", async ({ page }) => {
221+
page.getByText("Opret ny datakilde").click();
222222
await expect(page.locator("#save_feed-source")).toBeVisible();
223223
});
224224

@@ -232,7 +232,7 @@ test.describe("fest", () => {
232232
};
233233
await route.fulfill({ status: 500, json });
234234
});
235-
page.getByText("Opret ny feed source").click();
235+
page.getByText("Opret ny datakilde").click();
236236

237237
// Displays error toast and stays on page
238238
await expect(
@@ -251,16 +251,16 @@ test.describe("fest", () => {
251251
).toBeVisible();
252252
await expect(page).toHaveURL(/feed-sources\/create/);
253253
});
254-
test("Cancel create feed source", async ({ page }) => {
255-
page.getByText("Opret ny feed source").click();
254+
test("Cancel create datakilde", async ({ page }) => {
255+
page.getByText("Opret ny datakilde").click();
256256
await expect(page.locator("#cancel_feed-source")).toBeVisible();
257257
await page.locator("#cancel_feed-source").click();
258258
await expect(page.locator("#cancel_feed-source")).not.toBeVisible();
259259
});
260260
});
261261

262262

263-
test.describe("Feed source list work", () => {
263+
test.describe("datakilde list work", () => {
264264
test.beforeEach(async ({ page }) => {
265265
await page.goto("/admin/feed-sources/list");
266266
await page.route("**/token", async (route) => {
@@ -291,7 +291,7 @@ test.describe("Feed source list work", () => {
291291
await page.locator("#login").click();
292292
});
293293

294-
test("It loads feed source list", async ({ page }) => {
294+
test("It loads datakilde list", async ({ page }) => {
295295
await expect(page.locator("table").locator("tbody")).not.toBeEmpty();
296296
await expect(page.locator("tbody").locator("tr td").first()).toBeVisible();
297297
});

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import FormSelect from "../util/forms/select";
1010
import ContentBody from "../util/content-body/content-body";
1111
import ContentFooter from "../util/content-footer/content-footer";
1212
import FormInput from "../util/forms/form-input";
13+
import CalendarFeedType from "./templates/calendar-feed-type.jsx";
14+
import NotifiedFeedType from "./templates/notified-feed-type.jsx";
15+
import EventDatabaseFeedType from "./templates/event-database-feed-type.jsx";
1316

1417
/**
1518
* The feed-source form component.
@@ -25,7 +28,6 @@ import FormInput from "../util/forms/form-input";
2528
* spinner. Default is `""`
2629
* @param {object} props.feedSource The feed source object
2730
* @param {object} props.feedSourceTypeOptions The options for feed source types
28-
* @param {element} props.dynamicFormElement The dynamic form element
2931
* @param {string} props.mode The mode
3032
* @returns {object} The feed-source form.
3133
*/
@@ -37,7 +39,8 @@ function FeedSourceForm({
3739
loadingMessage = "",
3840
feedSource = null,
3941
feedSourceTypeOptions = null,
40-
dynamicFormElement = null,
42+
onFeedTypeChange = () => {},
43+
handleSecretInput = () => {},
4144
mode = null,
4245
}) {
4346
const { t } = useTranslation("common", { keyPrefix: "feed-source-form" });
@@ -67,15 +70,23 @@ function FeedSourceForm({
6770
onChange={handleInput}
6871
/>
6972
<FormSelect
70-
label={t("feed-source-feed-type-label")}
7173
name="feedType"
74+
label={t("feed-source-feed-type-label")}
7275
value={feedSource.feedType}
73-
onChange={handleInput}
76+
onChange={onFeedTypeChange}
7477
disabled={mode === "PUT"}
7578
options={feedSourceTypeOptions}
7679
/>
7780

78-
{dynamicFormElement}
81+
{feedSource?.feedType === "App\\Feed\\CalendarApiFeedType" &&
82+
(<CalendarFeedType handleInput={handleSecretInput} formStateObject={feedSource.secrets} mode={mode} />)
83+
}
84+
{feedSource?.feedType === "App\\Feed\\NotifiedFeedType" &&
85+
(<NotifiedFeedType handleInput={handleSecretInput} formStateObject={feedSource.secrets} mode={mode} />)
86+
}
87+
{feedSource?.feedType === "App\\Feed\\EventDatabaseApiFeedType" &&
88+
(<EventDatabaseFeedType handleInput={handleSecretInput} formStateObject={feedSource.secrets} mode={mode} />)
89+
}
7990
</ContentBody>
8091
<ContentFooter>
8192
<Button

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

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
displayError,
1212
displaySuccess,
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 CalendarFeedTypeTemplate from "./feed-source-type-templates/CalendarFeedType.template";
14+
import EventDatabaseFeedType from "./templates/event-database-feed-type.jsx";
15+
import NotifiedFeedType from "./templates/notified-feed-type.jsx";
16+
import CalendarFeedType from "./templates/calendar-feed-type.jsx";
1717

1818
/**
1919
* The theme manager component.
@@ -50,7 +50,7 @@ function FeedSourceManager({
5050

5151
const [dynamicFormElement, setDynamicFormElement] = useState();
5252
const [submitting, setSubmitting] = useState(false);
53-
const [formStateObject, setFormStateObject] = useState();
53+
const [formStateObject, setFormStateObject] = useState({});
5454

5555
const [
5656
postV2FeedSources,
@@ -67,28 +67,31 @@ function FeedSourceManager({
6767
value: "App\\Feed\\EventDatabaseApiFeedType",
6868
title: t("dynamic-fields.event-database-api-feed-type.title"),
6969
key: "1",
70-
secrets: "host",
71-
template: <EventDatabaseApiFeedTypeTemplate mode={saveMethod} />,
70+
secretsDefault: {
71+
"host": ""
72+
},
7273
},
7374
{
7475
value: "App\\Feed\\NotifiedFeedType",
7576
title: t("dynamic-fields.notified-feed-type.title"),
7677
key: "2",
77-
secrets: "token",
78-
template: <NotifiedFeedTypeTemplate mode={saveMethod} />,
78+
secretsDefault: {
79+
"token": "",
80+
},
7981
},
8082
{
8183
value: "App\\Feed\\CalendarApiFeedType",
8284
title: t("dynamic-fields.calendar-api-feed-type.title"),
8385
key: "3",
84-
secrets: "resources",
85-
template: <CalendarFeedTypeTemplate mode={saveMethod} />,
86+
secretsDefault: {
87+
"resources": []
88+
},
8689
},
8790
{
8891
value: "App\\Feed\\RssFeedType",
8992
title: t("dynamic-fields.rss-feed-type.title"),
90-
key: "5",
91-
template: null,
93+
key: "4",
94+
secretsDefault: {},
9295
},
9396
];
9497

@@ -109,46 +112,28 @@ function FeedSourceManager({
109112
setFormStateObject({ ...initialState });
110113
}, [initialState]);
111114

112-
useEffect(() => {
113-
if (formStateObject) {
114-
const option = feedSourceTypeOptions.find(
115-
(opt) => opt.value === formStateObject.feedType
116-
);
117-
if (option && option.template) {
118-
setDynamicFormElement(
119-
cloneElement(option.template, {
120-
handleInput,
121-
formStateObject,
122-
})
123-
);
124-
} else {
125-
setDynamicFormElement(null);
126-
}
115+
const handleSecretInput = ({target}) => {
116+
const localFormStateObject = { ...formStateObject };
117+
if (!localFormStateObject.secrets) {
118+
localFormStateObject.secrets = {};
127119
}
128-
}, [formStateObject]);
120+
localFormStateObject.secrets[target.id] = target.value;
121+
setFormStateObject(localFormStateObject);
122+
};
129123

130-
useEffect(() => {
131-
if (formStateObject?.feedType) {
132-
const selectedFeedTypeSecret = feedSourceTypeOptions.find(
133-
(option) => option.value === formStateObject.feedType
134-
).secrets;
135-
136-
if (selectedFeedTypeSecret) {
137-
const secretsArray = selectedFeedTypeSecret
138-
.split(",")
139-
.map((prop) => prop.trim());
140-
141-
formStateObject.secrets = secretsArray?.reduce((acc, secret) => {
142-
acc[secret] = formStateObject[secret];
143-
return acc;
144-
}, {});
145-
}
146-
}
147-
}, [formStateObject, formStateObject?.feedType]);
124+
const onFeedTypeChange = ({target}) => {
125+
const value = target.value
126+
const option = feedSourceTypeOptions.find((opt) => opt.value === value);
127+
const newFormStateObject = {...formStateObject};
128+
newFormStateObject.feedType = value;
129+
newFormStateObject.secrets = {...option.secretsDefault};
130+
setFormStateObject(newFormStateObject);
131+
}
148132

149133
/** Save feed source. */
150134
function saveFeedSource() {
151135
setLoadingMessage(t("loading-messages.saving-feed-source"));
136+
152137
if (saveMethod === "POST") {
153138
postV2FeedSources({
154139
feedSourceFeedSourceInput: JSON.stringify(formStateObject),
@@ -205,6 +190,8 @@ function FeedSourceManager({
205190
handleSubmit={handleSubmit}
206191
isLoading={isLoading || submitting}
207192
loadingMessage={loadingMessage}
193+
onFeedTypeChange={onFeedTypeChange}
194+
handleSecretInput={handleSecretInput}
208195
feedSourceTypeOptions={feedSourceTypeOptions}
209196
dynamicFormElement={dynamicFormElement}
210197
mode={saveMethod}

src/components/feed-sources/feed-sources-list.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function FeedSourcesList() {
4545
page: { get: page },
4646
createdBy: { get: createdBy },
4747
} = useContext(ListContext);
48+
4849
const {
4950
data,
5051
error: feedSourcesGetError,
@@ -97,6 +98,7 @@ function FeedSourcesList() {
9798
displayError(t("error-messages.feed-source-delete-error"), isDeleteError);
9899
}
99100
}, [isDeleteError]);
101+
100102
const handleDelete = () => {
101103
setIsDeleting(true);
102104
setLoadingMessage(t("loading-messages.deleting-feed-source"));
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import PropTypes from "prop-types";
33
import { useTranslation } from "react-i18next";
44
import FormInput from "../../util/forms/form-input";
55

6-
const CalendarFeedTypeTemplate = ({ handleInput, formStateObject, mode }) => {
6+
const CalendarFeedType = ({ handleInput, formStateObject, mode }) => {
77
const { t } = useTranslation("common", {
88
keyPrefix: "feed-source-manager.dynamic-fields.calendar-api-feed-type",
99
});
10+
1011
return (
1112
<>
1213
<FormInput
@@ -25,11 +26,12 @@ const CalendarFeedTypeTemplate = ({ handleInput, formStateObject, mode }) => {
2526
);
2627
};
2728

28-
CalendarFeedTypeTemplate.propTypes = {
29+
CalendarFeedType.propTypes = {
2930
handleInput: PropTypes.func,
3031
formStateObject: PropTypes.shape({
3132
resources: PropTypes.string,
3233
}),
3334
mode: PropTypes.string,
3435
};
35-
export default CalendarFeedTypeTemplate;
36+
37+
export default CalendarFeedType;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const EventDatabaseApiTemplate = ({
2121
? t("redacted-value-input-placeholder")
2222
: ""
2323
}
24-
value={formStateObject.host}
24+
value={formStateObject?.host}
2525
/>
2626
</>
2727
);
@@ -36,4 +36,5 @@ EventDatabaseApiTemplate.propTypes = {
3636
}),
3737
mode: PropTypes.string,
3838
};
39+
3940
export default EventDatabaseApiTemplate;
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import PropTypes from "prop-types";
33
import { useTranslation } from "react-i18next";
44
import FormInput from "../../util/forms/form-input";
55

6-
const NotifiedFeedTypeTemplate = ({
6+
const NotifiedFeedType = ({
77
handleInput,
88
formStateObject,
99
mode,
1010
}) => {
1111
const { t } = useTranslation("common", {
1212
keyPrefix: "feed-source-manager.dynamic-fields.notified-feed-type",
1313
});
14+
1415
return (
1516
<>
1617
<FormInput
@@ -29,11 +30,12 @@ const NotifiedFeedTypeTemplate = ({
2930
);
3031
};
3132

32-
NotifiedFeedTypeTemplate.propTypes = {
33+
NotifiedFeedType.propTypes = {
3334
handleInput: PropTypes.func,
3435
formStateObject: PropTypes.shape({
3536
token: PropTypes.string,
3637
}),
3738
mode: PropTypes.string,
3839
};
39-
export default NotifiedFeedTypeTemplate;
40+
41+
export default NotifiedFeedType;

0 commit comments

Comments
 (0)