-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'STCOR-936' of https://github.com/folio-org/stripes-core …
…into STCOR-936
- Loading branch information
Showing
15 changed files
with
529 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { isStorageEnabled } from './App'; | ||
|
||
const storageMock = () => ({ | ||
getItem: () => { | ||
throw new Error(); | ||
}, | ||
}); | ||
|
||
describe('isStorageEnabled', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('returns true when all storage options are enabled', () => { | ||
expect(isStorageEnabled()).toBeTrue; | ||
}); | ||
|
||
describe('returns false when any storage option is disabled', () => { | ||
it('handles local storage', () => { | ||
Object.defineProperty(window, 'localStorage', { value: storageMock }); | ||
const isEnabled = isStorageEnabled(); | ||
expect(isEnabled).toBeFalse; | ||
}); | ||
it('handles session storage', () => { | ||
Object.defineProperty(window, 'sessionStorage', { value: storageMock }); | ||
const isEnabled = isStorageEnabled(); | ||
expect(isEnabled).toBeFalse; | ||
}); | ||
|
||
it('handles cookies', () => { | ||
jest.spyOn(navigator, 'cookieEnabled', 'get').mockReturnValue(false); | ||
const isEnabled = isStorageEnabled(); | ||
expect(isEnabled).toBeFalse; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@import "@folio/stripes-components/lib/variables.css"; | ||
|
||
.wrapper { | ||
display: flex; | ||
justify-content: center; | ||
min-height: 100vh; | ||
} | ||
|
||
.container { | ||
width: 100%; | ||
max-width: 940px; | ||
min-height: 330px; | ||
margin: 12vh 2rem 0; | ||
} | ||
|
||
@media (--medium-up) { | ||
.container { | ||
min-height: initial; | ||
} | ||
} | ||
|
||
@media (--large-up) { | ||
.header { | ||
font-size: var(--font-size-xx-large); | ||
} | ||
} | ||
|
||
@media (height <= 440px) { | ||
.container { | ||
min-height: 330px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { branding } from 'stripes-config'; | ||
|
||
import { | ||
Row, | ||
Col, | ||
Headline, | ||
} from '@folio/stripes-components'; | ||
|
||
import OrganizationLogo from '../OrganizationLogo'; | ||
import styles from './AppConfigError.css'; | ||
|
||
/** | ||
* AppConfigError | ||
* Show an error message. This component is rendered by App, before anything | ||
* else, when it detects that local storage, session storage, or cookies are | ||
* unavailable. This happens _before_ Root has been initialized, i.e. before | ||
* an IntlProvider is available, hence the hard-coded, English-only message. | ||
* | ||
* @returns English-only error message | ||
*/ | ||
const AppConfigError = () => { | ||
return ( | ||
<main> | ||
<div className={styles.wrapper} style={branding?.style?.login ?? {}}> | ||
<div className={styles.container}> | ||
<Row center="xs"> | ||
<Col xs={6}> | ||
<OrganizationLogo /> | ||
</Col> | ||
</Row> | ||
<Row center="xs"> | ||
<Col xs={6}> | ||
<Headline | ||
size="xx-large" | ||
tag="h1" | ||
> | ||
FOLIO requires cookies, sessionStorage, and localStorage. Please enable these features and try again. | ||
</Headline> | ||
</Col> | ||
</Row> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
}; | ||
|
||
export default AppConfigError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { render, screen } from '@folio/jest-config-stripes/testing-library/react'; | ||
import AppConfigError from './AppConfigError'; | ||
|
||
jest.mock('../OrganizationLogo', () => () => 'OrganizationLogo'); | ||
describe('AppConfigError', () => { | ||
it('displays a warning message', async () => { | ||
render(<AppConfigError />); | ||
|
||
expect(screen.getByText(/cookies/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/storage/i)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './AppConfigError'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as useUserTenantPermissions } from './useUserTenantPermissions'; // eslint-disable-line import/prefer-default-export | ||
export { default as useUserTenantPermissions } from './useUserTenantPermissions'; | ||
export { default as useModuleInfo } from './useModuleInfo'; | ||
export { default as usePreferences } from './usePreferences'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
import { useStripes } from '../StripesContext'; | ||
import { useNamespace } from '../components'; | ||
import useOkapiKy from '../useOkapiKy'; | ||
|
||
/** | ||
* map a module implementation string to a module-name, hopefully. | ||
* given a string like "mod-users-16.2.0-SNAPSHOT.127" return "mod-users" | ||
*/ | ||
const implToModule = (impl) => { | ||
const moduleName = impl.match(/^(.*)-[0-9]+\.[0-9]+\.[0-9]+.*/); | ||
return moduleName[1] ? moduleName[1] : ''; | ||
}; | ||
|
||
/** | ||
* mapPathToImpl | ||
* Remap the input datastructure from an array of modules (containing | ||
* details about the interfaces they implement, and the paths handled by | ||
* each interface) to a map from path to module. | ||
* | ||
* i.e. map from sth like | ||
* [{ | ||
* provides: { | ||
* handlers: [ | ||
* { pathPattern: "/foo", ... } | ||
* { pathPattern: "/bar", ... } | ||
* ] | ||
* } | ||
* }, ... ] | ||
* to | ||
* { | ||
* foo: { ...impl }, | ||
* bar: { ...impl }, | ||
* } | ||
* @param {object} impl | ||
* @returns object | ||
*/ | ||
const mapPathToImpl = (impl) => { | ||
const moduleName = implToModule(impl.id); | ||
const paths = {}; | ||
if (impl.provides) { | ||
// not all interfaces actually implement routes, e.g. edge-connexion | ||
// so those must be filtered out | ||
impl.provides.filter(i => i.handlers).forEach(i => { | ||
i.handlers.forEach(handler => { | ||
if (!paths[handler.pathPattern]) { | ||
paths[handler.pathPattern] = { name: moduleName, impl }; | ||
} | ||
}); | ||
}); | ||
} | ||
return paths; | ||
}; | ||
|
||
/** | ||
* canonicalPath | ||
* Prepend a leading / if none is present. | ||
* Strip everything after ? | ||
* @param {string} str a string that represents a portion of a URL | ||
* @returns {string} | ||
*/ | ||
const canonicalPath = (str) => { | ||
return `${str.startsWith('/') ? '' : '/'}${str.split('?')[0]}`; | ||
}; | ||
|
||
/** | ||
* useModuleInfo | ||
* Given a path, retrieve information about the module that implements it | ||
* by querying the discovery endpoint /_/proxy/tenants/${tenant}/modules. | ||
* | ||
* @param {string} path | ||
* @returns object shaped like { isFetching, isFetched, isLoading, module } | ||
*/ | ||
const useModuleInfo = (path) => { | ||
const stripes = useStripes(); | ||
const ky = useOkapiKy(); | ||
const [namespace] = useNamespace({ key: `/_/proxy/tenants/${stripes.okapi.tenant}/modules` }); | ||
let paths = {}; | ||
|
||
const { | ||
isFetching, | ||
isFetched, | ||
isLoading, | ||
data, | ||
} = useQuery( | ||
[namespace], | ||
({ signal }) => { | ||
return ky.get( | ||
`/_/proxy/tenants/${stripes.okapi.tenant}/modules?full=true`, | ||
{ signal }, | ||
).json(); | ||
} | ||
); | ||
|
||
if (data) { | ||
data.forEach(impl => { | ||
paths = { ...paths, ...mapPathToImpl(impl) }; | ||
}); | ||
} | ||
|
||
return ({ | ||
isFetching, | ||
isFetched, | ||
isLoading, | ||
module: paths?.[canonicalPath(path)], | ||
}); | ||
}; | ||
|
||
export default useModuleInfo; |
Oops, something went wrong.