@equinor/mad-core
Version:
Core library for the Mobile App Delivery team
340 lines (332 loc) • 13.5 kB
JavaScript
import {
WhatsNewScreen
} from "./chunk-AXY3DL65.js";
import {
UserInfo
} from "./chunk-3WUJAVUO.js";
import {
SelectLanguageScreen
} from "./chunk-DGIAXVWS.js";
import {
ReleaseNotesScreen
} from "./chunk-PQODWSOK.js";
import {
createNativeStackNavigator,
createStackNavigator
} from "./chunk-T4TC3AQN.js";
import {
AboutScreen
} from "./chunk-4TVVFWGM.js";
import {
LoginScreen
} from "./chunk-GQ2R7AYC.js";
import {
initiateAuthenticationClient
} from "./chunk-5O4PAUNU.js";
import {
MadCoreProviders
} from "./chunk-7VZFSMNG.js";
import {
getDefaultScreenOptionsForLoginScreen
} from "./chunk-RGJ4RWVB.js";
import {
getMadCommonBaseUrl,
getMadCommonScopes
} from "./chunk-LNJQIWAH.js";
import {
useAccountOrDemoAccount
} from "./chunk-LEWQGDM7.js";
import {
setConfig,
useEnvironment,
useMadConfig,
useServiceNow
} from "./chunk-WWNTKXBS.js";
import {
CoreRoutes
} from "./chunk-UKAZW3CQ.js";
import {
__async,
__spreadProps,
__spreadValues
} from "./chunk-CWMXXUWU.js";
// src/utils/createMadCoreNavigator.tsx
import React2 from "react";
// src/components/screens/create-incident/CreateIncidentScreen.tsx
import {
Button,
Cell,
EDSStyleSheet,
Spacer,
TextField,
Typography,
useStyles
} from "@equinor/mad-components";
import * as Device from "expo-device";
import * as Localization from "expo-localization";
import React, { useState } from "react";
import { KeyboardAvoidingView, LayoutAnimation, Platform, ScrollView, View } from "react-native";
var CreateIncidentScreen = () => {
var _a, _b, _c, _d;
const [error, setError] = useState(null);
const [ticketNumber, setTicketNumber] = useState(void 0);
const [ticketTitle, setTicketTitle] = useState(void 0);
const [ticketDescription, setTicketDescription] = useState(void 0);
const [isSending, setIsSending] = useState(false);
const account = useAccountOrDemoAccount();
const serviceNowConfigurationItem = useServiceNow();
const environment = useEnvironment();
const styles = useStyles(createIncidentStyles);
const timeZone = (_b = (_a = Localization.getCalendars()[0]) == null ? void 0 : _a.timeZone) != null ? _b : "Unknown TimeZone";
const locale = (_d = (_c = Localization.getLocales()[0]) == null ? void 0 : _c.languageTag) != null ? _d : "Unknown Locale";
const onSubmit = () => {
setIsSending(true);
const data = {
callerEmail: account == null ? void 0 : account.username,
title: ticketTitle,
description: createDescription()
};
createIncident(data, environment, serviceNowConfigurationItem).then((response) => {
LayoutAnimation.configureNext({
duration: 500,
update: {
type: LayoutAnimation.Types.easeInEaseOut
},
create: {
type: LayoutAnimation.Types.easeInEaseOut,
property: LayoutAnimation.Properties.opacity
}
});
if (response.result.details.number) {
setTicketNumber(response.result.details.number);
} else {
setError(response);
}
setTicketDescription(void 0);
setTicketTitle(void 0);
setIsSending(false);
}).catch(setError).finally(() => setIsSending(false));
};
const createDescription = () => {
let description = "";
description += `User: ${account == null ? void 0 : account.username}
`;
description += `Device Brand: ${Device.brand}
`;
description += `Device: ${Device.deviceName}
`;
description += `Operating System: ${Device.osVersion}
`;
description += `Time Zone: ${timeZone}
`;
description += `Locale: ${locale}
`;
return `${description}
${ticketDescription}`;
};
return /* @__PURE__ */ React.createElement(
KeyboardAvoidingView,
{
behavior: "padding",
keyboardVerticalOffset: 115,
style: styles.container
},
/* @__PURE__ */ React.createElement(ScrollView, { contentInsetAdjustmentBehavior: "automatic" }, /* @__PURE__ */ React.createElement(Spacer, null), /* @__PURE__ */ React.createElement(Cell, null, /* @__PURE__ */ React.createElement(View, { style: styles.topTextContainer }, /* @__PURE__ */ React.createElement(Typography, { variant: "h1" }, "Create ticket in ServiceNow"), /* @__PURE__ */ React.createElement(Typography, { group: "paragraph", variant: "body_short" }, "We collect information about your device as part of our feedback process. By submitting, you agree to share the following information:")), /* @__PURE__ */ React.createElement(UserInfo, { infoType: "User", infoValue: account == null ? void 0 : account.username }), Platform.OS !== "web" && /* @__PURE__ */ React.createElement(UserInfo, { infoType: "Device Brand", infoValue: Device.brand }), Platform.OS !== "web" && /* @__PURE__ */ React.createElement(UserInfo, { infoType: "Device", infoValue: Device.deviceName }), /* @__PURE__ */ React.createElement(UserInfo, { infoType: "Operating System", infoValue: Device.osVersion }), /* @__PURE__ */ React.createElement(UserInfo, { infoType: "Time Zone", infoValue: timeZone }), /* @__PURE__ */ React.createElement(UserInfo, { infoType: "Locale", infoValue: locale }), ticketNumber && /* @__PURE__ */ React.createElement(View, { style: [styles.popupBox, styles.popupSuccess] }, /* @__PURE__ */ React.createElement(Typography, null, "Ticket number: "), /* @__PURE__ */ React.createElement(Typography, { selectable: true }, ticketNumber)), error && /* @__PURE__ */ React.createElement(View, { style: [styles.popupBox, styles.popupDanger] }, /* @__PURE__ */ React.createElement(Typography, null, "An error occurred creating your ticket:"), /* @__PURE__ */ React.createElement(Typography, { selectable: true }, error instanceof Error ? error.message : "")), /* @__PURE__ */ React.createElement(View, { style: styles.titleField }, /* @__PURE__ */ React.createElement(
TextField,
{
onChange: setTicketTitle,
value: ticketTitle,
placeholder: "Write a title for the Service Now ticket",
readOnly: isSending
}
)), /* @__PURE__ */ React.createElement(View, { style: styles.titleField }, /* @__PURE__ */ React.createElement(
TextField,
{
multiline: true,
onChange: setTicketDescription,
placeholder: "Write a complete description of your issue. You do not need to provide information about your device.",
value: ticketDescription,
readOnly: isSending
}
)), /* @__PURE__ */ React.createElement(View, { style: styles.buttonContainer }, /* @__PURE__ */ React.createElement(
Button,
{
disabled: !ticketTitle || !ticketDescription || isSending,
onPress: onSubmit,
style: { width: 81 },
title: "Send"
}
))))
);
};
var createIncidentStyles = EDSStyleSheet.create((theme) => ({
container: {
flex: 1
},
topTextContainer: {
paddingBottom: theme.geometry.dimension.cell.minHeight
},
titleField: {
marginVertical: theme.spacing.textField.paddingVertical * 3
},
multiTextField: {
height: theme.geometry.dimension.dialog.minHeight
},
buttonContainer: {
flexDirection: "row",
justifyContent: "flex-end"
},
popupBox: {
padding: 24,
marginTop: 16,
borderWidth: 2,
borderRadius: 4,
flexDirection: "row",
gap: 8,
flexWrap: "wrap"
},
popupDanger: {
borderColor: theme.colors.feedback.danger
},
popupSuccess: {
borderColor: theme.colors.feedback.success
}
}));
// src/utils/createMadCoreNavigator.tsx
var createMadCoreNativeStackNavigator = (Stack) => {
function MadCoreNavigator(props) {
const config = useMadConfig();
if (!config) return null;
return /* @__PURE__ */ React2.createElement(MadCoreProviders, { config, type: "native-stack" }, /* @__PURE__ */ React2.createElement(Stack.Navigator, __spreadProps(__spreadValues({}, props), { initialRouteName: CoreRoutes.LOGIN }), config.login.addScreenManually !== true && /* @__PURE__ */ React2.createElement(
Stack.Screen,
{
name: CoreRoutes.LOGIN,
component: LoginScreen,
options: getDefaultScreenOptionsForLoginScreen()
}
), /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.RELEASE_NOTES, component: ReleaseNotesScreen }), /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.WHATS_NEW, component: WhatsNewScreen }), config.about && /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.ABOUT, component: AboutScreen }), config.serviceNow && /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.FEEDBACK, component: CreateIncidentScreen }), config.language.supportedLanguages.length > 1 && /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(
Stack.Screen,
{
name: CoreRoutes.SELECT_LANGUAGE,
component: SelectLanguageScreen
}
), /* @__PURE__ */ React2.createElement(
Stack.Screen,
{
name: CoreRoutes.SELECT_LANGUAGE_ONBOARDING,
component: SelectLanguageScreen,
options: { headerBackVisible: false }
}
)), props.children));
}
return MadCoreNavigator;
};
var createMadCoreStackNavigator = (Stack) => {
function MadCoreNavigator(props) {
const config = useMadConfig();
if (!config) return null;
return /* @__PURE__ */ React2.createElement(MadCoreProviders, { config, type: "stack" }, /* @__PURE__ */ React2.createElement(Stack.Navigator, __spreadProps(__spreadValues({}, props), { initialRouteName: CoreRoutes.LOGIN }), config.login.addScreenManually !== true && /* @__PURE__ */ React2.createElement(
Stack.Screen,
{
name: CoreRoutes.LOGIN,
component: LoginScreen,
options: getDefaultScreenOptionsForLoginScreen()
}
), /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.RELEASE_NOTES, component: ReleaseNotesScreen }), /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.WHATS_NEW, component: WhatsNewScreen }), config.about && /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.ABOUT, component: AboutScreen }), config.serviceNow && /* @__PURE__ */ React2.createElement(Stack.Screen, { name: CoreRoutes.FEEDBACK, component: CreateIncidentScreen }), config.language.supportedLanguages.length > 1 && /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(
Stack.Screen,
{
name: CoreRoutes.SELECT_LANGUAGE,
component: SelectLanguageScreen
}
), /* @__PURE__ */ React2.createElement(
Stack.Screen,
{
name: CoreRoutes.SELECT_LANGUAGE_ONBOARDING,
component: SelectLanguageScreen,
options: { headerLeft: () => null }
}
)), props.children));
}
return MadCoreNavigator;
};
// src/components/createCoreStackNavigator.tsx
var createStackCoreNavigator = (config) => {
setConfig(config);
initiateAuthenticationClient();
const Stack = createStackNavigator();
const Navigator = createMadCoreStackNavigator(Stack);
return __spreadProps(__spreadValues({}, Stack), { Navigator });
};
var createNativeStackCoreNavigator = (config) => {
setConfig(config);
initiateAuthenticationClient();
const Stack = createNativeStackNavigator();
const Navigator = createMadCoreNativeStackNavigator(Stack);
return __spreadProps(__spreadValues({}, Stack), { Navigator });
};
var createCoreStackNavigator = (config) => createNativeStackCoreNavigator(config);
// src/index.ts
import {
appInsightsHasBeenInitialized,
addTelemetryInitializer,
Envelope,
metricKeys,
metricStatus,
track,
trackCustom,
trackLongTerm,
trackShortTerm
} from "@equinor/mad-insights";
import { addToast, ToastTypes, ToastType } from "@equinor/mad-toast";
import { MadAccount, MadAuthenticationResult } from "@equinor/mad-auth";
import { ExpoAuthSession } from "@equinor/mad-auth";
var authenticateSilently = (scopes) => ExpoAuthSession.authenticateSilently(scopes);
var getAccount = () => ExpoAuthSession.getAccount();
var signOut = () => __async(null, null, function* () {
return ExpoAuthSession.signOut();
});
var authenticateInteractively = (scopes) => ExpoAuthSession.authenticateInteractively(scopes);
// src/components/screens/create-incident/createIncident.ts
var createIncident = (data, env, serviceNow) => __async(null, null, function* () {
const baseUrl = getMadCommonBaseUrl(env);
const scopes = getMadCommonScopes(env);
const authenticationResponse = yield authenticateSilently(scopes);
if (!authenticationResponse) throw new Error("Unable to authenticate silently");
const fetchResponse = yield fetch(`${baseUrl}/ServiceNow/apps/${serviceNow}/incidents`, {
method: "POST",
body: JSON.stringify(data),
headers: new Headers({
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${authenticationResponse.accessToken}`
})
});
return yield fetchResponse.json().then((result) => JSON.parse(result));
});
export {
createIncident,
CreateIncidentScreen,
createMadCoreNativeStackNavigator,
createMadCoreStackNavigator,
createStackCoreNavigator,
createNativeStackCoreNavigator,
createCoreStackNavigator,
authenticateSilently,
getAccount,
signOut,
authenticateInteractively,
appInsightsHasBeenInitialized,
addTelemetryInitializer,
Envelope,
metricKeys,
metricStatus,
track,
trackCustom,
trackLongTerm,
trackShortTerm,
addToast,
ToastTypes,
ToastType,
MadAccount,
MadAuthenticationResult
};