Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 25 additions & 3 deletions ts/components/WebviewComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
WebViewHttpErrorEvent,
WebViewSourceUri
} from "react-native-webview/lib/WebViewTypes";
import { Platform } from "react-native";
import I18n from "../i18n";
import { mixpanelTrack } from "../mixpanel";
import { resetDebugData, setDebugData } from "../store/actions/debug";
Expand All @@ -14,9 +15,10 @@ import { OperationResultScreenContent } from "./screens/OperationResultScreenCon

type Props = {
source: WebViewSourceUri;
playgroundEnabled?: boolean;
};

const WebviewComponent = ({ source }: Props) => {
const WebviewComponent = ({ source, playgroundEnabled }: Props) => {
const dispatch = useIODispatch();
const [loading, setLoading] = useState(true);
const [hasError, setHasError] = useState(false);
Expand All @@ -39,6 +41,10 @@ const WebviewComponent = ({ source }: Props) => {
[dispatch]
);

useEffect(() => {
setHasError(false);
}, [source]);

const handleError = (event: WebViewErrorEvent | WebViewHttpErrorEvent) => {
void mixpanelTrack("CGN_LANDING_PAGE_LOAD_ERROR", {
uri: source.uri,
Expand All @@ -58,7 +64,7 @@ const WebviewComponent = ({ source }: Props) => {

return (
<>
{hasError ? (
{hasError && !playgroundEnabled ? (
<OperationResultScreenContent
testID="webview-error"
pictogram="umbrella"
Expand All @@ -82,13 +88,29 @@ const WebviewComponent = ({ source }: Props) => {
ref={ref}
onLoadEnd={() => setLoading(false)}
onHttpError={handleError}
userAgent={getDefaultUserAgent()}
onError={handleError}
source={source}
source={{
...source,
headers: {
...(source.headers ?? {}),
"User-Agent": getDefaultUserAgent()
}
}}
/>
</LoadingSpinnerOverlay>
)}
</>
);
};

const getDefaultUserAgent = () => {
if (Platform.OS === "ios") {
return "AppIO IOS";
} else if (Platform.OS === "android") {
return "AppIO Android";
}
return "AppIO";
};

export default WebviewComponent;
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ exports[`WebviewComponent tests snapshot for component 1`] = `
messagingModuleName=""
newSource={
{
"headers": [
{
"name": "User-Agent",
"value": "AppIO IOS",
},
],
"uri": "https://google.com",
}
}
Expand All @@ -304,6 +310,9 @@ exports[`WebviewComponent tests snapshot for component 1`] = `
onShouldStartLoadWithRequest={[Function]}
source={
{
"headers": {
"User-Agent": "AppIO IOS",
},
"uri": "https://google.com",
}
}
Expand All @@ -324,6 +333,7 @@ exports[`WebviewComponent tests snapshot for component 1`] = `
testID="webview"
textInteractionEnabled={true}
useSharedProcessPool={true}
userAgent="AppIO IOS"
/>
</View>
</View>
Expand Down
25 changes: 15 additions & 10 deletions ts/features/settings/devMode/playgrounds/CgnLandingPlayground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TextInput,
View
} from "react-native";
import { WebViewSourceUri } from "react-native-webview/lib/WebViewTypes";
import WebviewComponent from "../../../../components/WebviewComponent";
import { useHeaderSecondLevel } from "../../../../hooks/useHeaderSecondLevel";

Expand All @@ -33,9 +34,12 @@ const styles = StyleSheet.create({
});

const CgnLandingPlayground = () => {
const [navigationURI, setNavigationUri] = useState("https://");
const [navigationURI, setNavigationUri] = useState("https://google.it");
const [refererValue, setRefererValue] = useState("");
const [loadUri, setLoadUri] = useState("https://google.com");
const [source, setSource] = useState<WebViewSourceUri>({
uri: "https://google.it",
headers: undefined
});
const [reloadKey, setReloadKey] = useState(0);

useHeaderSecondLevel({
Expand Down Expand Up @@ -83,22 +87,23 @@ const CgnLandingPlayground = () => {
variant="solid"
label="Invia"
onPress={() => {
setLoadUri(navigationURI);
setSource({
uri: navigationURI,
headers: {
"X-PagoPa-CGN-Referer": refererValue
}
});
}}
accessibilityLabel={"Invia"}
/>
</View>
<VSpacer size={16} />
<View style={{ flex: 1 }}>
{loadUri !== "" && (
{source && (
<WebviewComponent
playgroundEnabled
key={`${reloadKey}_webview`}
source={{
uri: loadUri,
headers: {
"X-PagoPa-CGN-Referer": refererValue
}
}}
source={source}
/>
)}
</View>
Expand Down
Loading