This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
utilityHelpers.jsx
163 lines (152 loc) · 5.78 KB
/
utilityHelpers.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React from 'react';
import { UtilityUtils } from 'terra-application-utility';
import UserData from '../user/_UserData';
const defaultKeys = {
MENU: 'menu',
USER_INFORMATION: 'user-information',
CHANGE_PHOTO: 'change-photo',
SETTINGS: 'settings',
APPEARANCE: 'appearance',
SECURITY: 'security',
HELP: 'help',
GETTING_STARTED: 'getting-started',
ABOUT: 'about',
TERMS_OF_USE: 'terms-of-use',
LOG_OUT: 'log-out',
};
const menuTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.menu' }));
const userInformationTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.userInformation' }));
const changePhotoTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.changePhoto' }));
const settingsTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.settings' }));
const appearanceTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.appearance' }));
const securityTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.security' }));
const helpTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.help' }));
const gettingStartedTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.gettingStarted' }));
const aboutTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.about' }));
const termsOfUseTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.termsOfUse' }));
const logOutTitle = intl => (intl.formatMessage({ id: 'Terra.applicationLayout.utilityDefaults.logOut' }));
/**
* This function integrates a set of additional menu items with the set of default items as generated by getDefaultUtilityItems. Because
* utility menu items specify their own child items, if custom item children want to be added to a default item, we need to regenerate the default
* item with the appropriate keys in place. The additionalItems should provide a `parentKey` value that correlates it to another item in the default
* item set.
*
* @param {Array} defaultItems is an Array of Objects adhering to the `terra-application-utility` menu item shape. This is expected to be
* the set of default items generated by the `getDefaultUtilityItems` function.
* @param {Array} additionalItems is an Array of Objects adhereing to `terra-application-utility` menu item shape that we need to be
* integrated with the default item set. Additionally, a `parentKey` must be provided that correlates the item to a default item parent.
*/
const reconcileChildren = (defaultItems, additionalItems) => {
if (!additionalItems) {
return defaultItems;
}
additionalItems.forEach((additionalItem) => {
if (additionalItem.parentKey) {
const matchedParents = defaultItems.filter(configItem => configItem.key === additionalItem.parentKey);
if (!matchedParents.length) {
return;
}
const parent = matchedParents[0];
if (!parent.childKeys) {
parent.childKeys = [additionalItem.key];
} else if (parent.childKeys.indexOf(additionalItem.key) < 0) {
parent.childKeys.push(additionalItem.key);
}
}
});
return defaultItems.concat(additionalItems);
};
/**
* The function generates a set of ApplicationLayout-standard utility menu items.
*
* @param {Object} intl is the intl context object provided by `react-intl`.
* @param {Object} userData is an Object of user information data used to build the user data menu item.
* Shape: {
* name: {String} The user's name
* detail: {String} Additional string-based user information to render with the user's name
* photo: {Element} A React element that renders the user's photo
* }
* @param {Array} additionalItems is an Array of Objects adhereing to `terra-application-utility` menu item shape that we need to be
* integrated with the default item set. Additionally, a `parentKey` must be provided that correlates the item to a default item parent.
*/
const getDefaultUtilityItems = (intl, userData, additionalItems) => {
const userDataComponent = (
<UserData
userName={userData.name}
userDetail={userData.detail}
userPhoto={userData.photo}
/>
);
const defaultConfig = [
{
key: defaultKeys.MENU,
title: menuTitle(intl),
childKeys: [
defaultKeys.USER_INFORMATION,
defaultKeys.SETTINGS,
defaultKeys.HELP,
defaultKeys.LOG_OUT,
],
},
{
key: defaultKeys.USER_INFORMATION,
title: userInformationTitle(intl),
content: userDataComponent,
childKeys: [
defaultKeys.CHANGE_PHOTO,
],
},
{
key: defaultKeys.SETTINGS,
title: settingsTitle(intl),
childKeys: [
defaultKeys.APPEARANCE,
defaultKeys.SECURITY,
],
},
{
key: defaultKeys.LOG_OUT,
contentLocation: UtilityUtils.LOCATIONS.FOOTER,
title: logOutTitle(intl),
},
{
key: defaultKeys.HELP,
title: helpTitle(intl),
childKeys: [
defaultKeys.GETTING_STARTED,
defaultKeys.ABOUT,
defaultKeys.TERMS_OF_USE,
],
},
{
key: defaultKeys.CHANGE_PHOTO,
title: changePhotoTitle(intl),
},
{
key: defaultKeys.APPEARANCE,
title: appearanceTitle(intl),
},
{
key: defaultKeys.SECURITY,
title: securityTitle(intl),
},
{
key: defaultKeys.GETTING_STARTED,
title: gettingStartedTitle(intl),
},
{
key: defaultKeys.ABOUT,
title: aboutTitle(intl),
},
{
key: defaultKeys.TERMS_OF_USE,
title: termsOfUseTitle(intl),
},
];
return reconcileChildren(defaultConfig, additionalItems);
};
export default {
getDefaultUtilityItems,
defaultKeys,
locations: UtilityUtils.LOCATIONS,
};