Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash of the report suites override section. #405

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion scripts/helpers/createExtensionManifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ const createExtensionManifest = ({ version }) => {
type: "integer",
},
},
required: ["channel", "playerName"],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schema was to relaxed. You could have saved an object without channel or playerName. You could have saved also other properties that were not defined.

additionalProperties: false,
},
personalizationStorageEnabled: {
type: "boolean",
Expand Down Expand Up @@ -1172,7 +1174,12 @@ const createExtensionManifest = ({ version }) => {
type: "array",
minItems: 1,
items: {
enum: ["analytics", "target", "audiencemanager", "audienceManager"]
enum: [
"analytics",
"target",
"audiencemanager",
"audienceManager",
],
},
required: ["name"],
additionalProperties: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const FormikNumberField = ({ name, width, validate, ...otherProps }) => {
return (
<NumberField
{...otherProps}
value={value}
value={value === "" ? null : value}
Copy link
Member Author

@dompuiu dompuiu Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you provide an empty string to a Spectrum Number Field, it automatically converts that to 0. For keeping it empty, you need to provide a null value.

Formik on the other hand it, will complain if you initialize a field with null and change the field value at a later point. A warning appears in logs where it says a component was changed from uncontrolled to controlled.

That is what I added this change.

onChange={(val) => {
setValue(val).then(() => {
setTouched(true);
Expand Down
99 changes: 68 additions & 31 deletions src/view/configuration/streamingMediaSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,58 @@ governing permissions and limitations under the License.

import React from "react";
import PropTypes from "prop-types";
import { number, object, string } from "yup";
import { number, object, string, lazy } from "yup";
import { useField } from "formik";
import SectionHeader from "../components/sectionHeader";
import FormElementContainer from "../components/formElementContainer";
import FormikTextField from "../components/formikReactSpectrum3/formikTextField";
import FormikNumberField from "../components/formikReactSpectrum3/formikNumberField";
import isNonEmptyString from "../utils/isNonEmptyString";

const getDefaultSettings = () => {
return {
channel: "",
playerName: "",
appVersion: "",
adPingInterval: 10,
mainPingInterval: 10,
};
};
const getDefaultSettings = () => ({
channel: "",
playerName: "",
appVersion: "",
adPingInterval: 10,
mainPingInterval: 10,
});

export const bridge = {
getInstanceDefaults: () => ({
streamingMedia: getDefaultSettings(),
}),

getInitialInstanceValues: ({ instanceSettings }) => {
if (!instanceSettings.streamingMedia) {
instanceSettings.streamingMedia = getDefaultSettings();
return bridge.getInstanceDefaults();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extension configuration was crashing because the instanceSettings was returned. The instance settings was containing keys that could potentially override other bridge method from different sections. We need to return an object that contains just the streamingMedia key.

}

return instanceSettings;
const streamingMedia = Object.keys(getDefaultSettings()).reduce(
(acc, k) => {
if (instanceSettings.streamingMedia[k] !== undefined) {
acc[k] = instanceSettings.streamingMedia[k];
} else {
acc[k] = "";
}

return acc;
},
{},
);

return { streamingMedia };
},

getInstanceSettings: ({ instanceValues }) => {
const instanceSettings = {};
const { streamingMedia } = instanceValues;

["appVersion", "adPingInterval", "mainPingInterval"].forEach((key) => {
if (!streamingMedia[key]) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I clear the empty values of optional fields. Before, they were saved to the DB.

delete streamingMedia[key];
}
});

if (streamingMedia.channel && streamingMedia.playerName) {
instanceSettings.streamingMedia = streamingMedia;
}
Expand All @@ -66,25 +86,42 @@ export const bridge = {
"Please provide a player name for streaming media.",
),
}),
adPingInterval: number().when(["channel", "playerName"], {
is: (channel, playerName) => channel && playerName,
then: (schema) =>
schema
.min(1, "The Ad Ping Interval must be greater than 1 second.")
.max(10, "The Ad Ping Interval must be less than 10 seconds.")
.default(10),
}),
mainPingInterval: number().when(["channel", "playerName"], {
is: (channel, playerName) => channel && playerName,
then: (schema) =>
schema
.min(
10,
"The Main Ping Interval must be greater than 10 seconds.",
)
.max(60, "The Main Ping Interval must be less than 60 seconds.")
.default(10),
}),
adPingInterval: lazy((value) =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you know a better way to make a field accept the number or an empty string, please let me know.

/^\d+$/.exec(value)
? number().when(["channel", "playerName"], {
is: (channel, playerName) => channel && playerName,
then: (schema) =>
schema
.min(
1,
"The Ad Ping Interval must be greater than 1 second.",
)
.max(
10,
"The Ad Ping Interval must be less than 10 seconds.",
)
.default(10),
})
: string(),
),
mainPingInterval: lazy((value) =>
/^\d+$/.exec(value)
? number().when(["channel", "playerName"], {
is: (channel, playerName) => channel && playerName,
then: (schema) =>
schema
.min(
10,
"The Main Ping Interval must be greater than 10 seconds.",
)
.max(
60,
"The Main Ping Interval must be less than 60 seconds.",
)
.default(10),
})
: string(),
),
},
["channel", "playerName"],
),
Expand Down
Loading