-
Notifications
You must be signed in to change notification settings - Fork 1
feat(mui-stash-receiver): add stash receiver package #914
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
Open
btrinhav
wants to merge
2
commits into
main
Choose a base branch
from
feat/stash-receiver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,61 @@ | ||
| # @availity/mui-stash-receiver | ||
|
|
||
| > Availity MUI StashReceiver component to be used with @availity/element design system. | ||
|
|
||
| [](https://www.npmjs.com/package/@availity/mui-stash-receiver) | ||
| [](https://www.npmjs.com/package/@availity/mui-stash-receiver) | ||
| [](https://github.com/Availity/element/blob/main/packages/mui-stash-receiver/package.json) | ||
|
|
||
| ## Documentation | ||
|
|
||
| This package extends the MUI StashReceiver component: [MUI StashReceiver Docs](https://mui.com/components/stash-receiver/) | ||
|
|
||
| Live demo and documentation in our [Storybook](https://availity.github.io/element/?path=/docs/components-stash-receiver-introduction--docs) | ||
|
|
||
| Availity standards for design and usage can be found in the [Availity Design Guide](https://design.availity.com/2e36e50c7) | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Import Through @availity/element (Recommended) | ||
|
|
||
| #### NPM | ||
|
|
||
| ```bash | ||
| npm install @availity/element | ||
| ``` | ||
|
|
||
| #### Yarn | ||
|
|
||
| ```bash | ||
| yarn add @availity/element | ||
| ``` | ||
|
|
||
| ### Direct Import | ||
|
|
||
| #### NPM | ||
|
|
||
| _This package has a few peer dependencies. Add `@mui/material` & `@emotion/react` to your project if not already installed._ | ||
|
|
||
| ```bash | ||
| npm install @availity/mui-stash-receiver | ||
| ``` | ||
|
|
||
| #### Yarn | ||
|
|
||
| ```bash | ||
| yarn add @availity/mui-stash-receiver | ||
| ``` | ||
|
|
||
| ### Usage | ||
|
|
||
| #### Import through @availity/element | ||
|
|
||
| ```tsx | ||
| import { StashReceiver } from '@availity/element'; | ||
| ``` | ||
|
|
||
| #### Direct import | ||
|
|
||
| ```tsx | ||
| import { StashReceiver } from '@availity/mui-stash-receiver'; | ||
| ``` |
This file contains hidden or 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,9 @@ | ||
| import { Markdown, Meta } from '@storybook/addon-docs/blocks'; | ||
| import ReadMe from './README.md?raw'; | ||
| import CHANGELOG from './CHANGELOG.md?raw'; | ||
|
|
||
| <Meta title="Components/StashReceiver/Introduction" /> | ||
|
|
||
| <Markdown>{ReadMe}</Markdown> | ||
|
|
||
| <Markdown>{CHANGELOG}</Markdown> |
This file contains hidden or 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,7 @@ | ||
| const global = require('../../jest.config.global'); | ||
|
|
||
| module.exports = { | ||
| ...global, | ||
| displayName: 'stash-receiver', | ||
| coverageDirectory: '../../coverage/stash-receiver', | ||
| }; |
This file contains hidden or 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,58 @@ | ||
| { | ||
| "name": "@availity/mui-stash-receiver", | ||
| "version": "0.0.0", | ||
| "description": "Availity MUI StashReceiver Component - part of the @availity/element design system", | ||
| "keywords": [ | ||
| "react", | ||
| "typescript", | ||
| "availity", | ||
| "mui" | ||
| ], | ||
| "homepage": "https://availity.github.io/element/?path=/docs/components-stash-receiver-introduction--docs", | ||
| "bugs": { | ||
| "url": "https://github.com/Availity/element/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/Availity/element.git", | ||
| "directory": "packages/stash-receiver" | ||
| }, | ||
| "license": "MIT", | ||
| "author": "Availity Developers <AVOSS@availity.com>", | ||
| "browser": "./dist/index.js", | ||
| "main": "./dist/index.js", | ||
| "module": "./dist/index.mjs", | ||
| "types": "./dist/index.d.ts", | ||
| "exports": { | ||
| "./package.json": "./package.json", | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.mjs", | ||
| "require": "./dist/index.js" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "build": "tsup src/index.ts --format esm,cjs --dts", | ||
| "dev": "tsup src/index.ts --format esm,cjs --watch --dts", | ||
| "clean": "rm -rf dist", | ||
| "clean:nm": "rm -rf node_modules", | ||
| "publish": "yarn npm publish --tolerate-republish --access public", | ||
| "publish:canary": "yarn npm publish --access public --tag canary" | ||
| }, | ||
| "devDependencies": { | ||
| "@mui/material": "^7.3.4", | ||
| "axios": "^1.16.1", | ||
| "react": "19.2.0", | ||
| "react-dom": "19.2.0", | ||
| "tsup": "^8.4.0", | ||
| "typescript": "^5.4.5" | ||
| }, | ||
| "peerDependencies": { | ||
| "@mui/material": "^7.0.0", | ||
| "axios": ">=1.0.0", | ||
| "react": ">=17.0.0" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } |
This file contains hidden or 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,41 @@ | ||
| { | ||
| "name": "mui-stash-receiver", | ||
| "$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
| "sourceRoot": "packages/stash-receiver/src", | ||
| "projectType": "library", | ||
| "tags": [], | ||
| "targets": { | ||
| "lint": { | ||
| "executor": "@nx/eslint:lint", | ||
| "options": { | ||
| "eslintConfig": ".eslintrc.json", | ||
| "silent": false, | ||
| "fix": false, | ||
| "cache": true, | ||
| "cacheLocation": "./node_modules/.cache/stash-receiver/.eslintcache", | ||
| "maxWarnings": -1, | ||
| "quiet": false, | ||
| "noEslintrc": false, | ||
| "hasTypeAwareRules": true, | ||
| "cacheStrategy": "metadata" | ||
| } | ||
| }, | ||
| "test": { | ||
| "executor": "@nx/jest:jest", | ||
| "outputs": ["{workspaceRoot}/coverage/stash-receiver"], | ||
| "options": { | ||
| "jestConfig": "packages/stash-receiver/jest.config.js" | ||
| } | ||
| }, | ||
| "version": { | ||
| "executor": "@jscutlery/semver:version", | ||
| "options": { | ||
| "preset": "conventional", | ||
| "commitMessageFormat": "chore({projectName}): release version ${version} [skip ci]", | ||
| "tagPrefix": "@availity/{projectName}@", | ||
| "trackDeps": true, | ||
| "skipCommitTypes": ["docs"] | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 * from './lib/StashReceiver'; |
This file contains hidden or 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,28 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { StashReceiver, useStashContext } from './StashReceiver'; | ||
|
|
||
| const meta: Meta<typeof StashReceiver> = { | ||
| title: 'Components/StashReceiver/StashReceiver', | ||
| component: StashReceiver, | ||
| tags: ['autodocs'], | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| const StashDisplay = () => { | ||
| const { data, loading, error } = useStashContext(); | ||
| if (loading) return <p>Loading stash data...</p>; | ||
| if (error) return <p>Error: {error}</p>; | ||
| return <pre>{JSON.stringify(data, null, 2)}</pre>; | ||
| }; | ||
|
|
||
| export const _StashReceiver: StoryObj<typeof StashReceiver> = { | ||
| render: (args) => ( | ||
| <StashReceiver {...args}> | ||
| <StashDisplay /> | ||
| </StashReceiver> | ||
| ), | ||
| args: { | ||
| sessionId: 'example-session-id', | ||
| }, | ||
| }; |
This file contains hidden or 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,53 @@ | ||
| import '@testing-library/jest-dom'; | ||
| import { render, waitFor } from '@testing-library/react'; | ||
| import { http, HttpResponse } from 'msw'; | ||
| import { StashReceiver, useStashContext } from './StashReceiver'; | ||
|
|
||
| // eslint-disable-next-line @nx/enforce-module-boundaries | ||
| import { server } from '../../../mock/src/lib/server'; | ||
|
|
||
| beforeAll(() => server.listen()); | ||
| afterEach(() => server.resetHandlers()); | ||
| afterAll(() => server.close()); | ||
|
|
||
| const Consumer = () => { | ||
| const { data, loading, error } = useStashContext(); | ||
| if (loading) return <span>loading</span>; | ||
| if (error) return <span>error: {error}</span>; | ||
| return <span>memberId: {String(data?.memberId)}</span>; | ||
| }; | ||
|
|
||
| describe('StashReceiver', () => { | ||
| it('shows loading state initially', () => { | ||
| const { getByText } = render( | ||
| <StashReceiver sessionId="test-session"> | ||
| <Consumer /> | ||
| </StashReceiver> | ||
| ); | ||
| expect(getByText('loading')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('provides stash data to children after fetch', async () => { | ||
| const { getByText } = render( | ||
| <StashReceiver sessionId="test-session"> | ||
| <Consumer /> | ||
| </StashReceiver> | ||
| ); | ||
| await waitFor(() => expect(getByText('memberId: member-123')).toBeInTheDocument()); | ||
| }); | ||
|
|
||
| it('provides error state when fetch fails', async () => { | ||
| server.use( | ||
| http.get('/cloud/web/appl/stash/v1/session/data/:sessionId', () => | ||
| HttpResponse.json({ message: 'Server Error' }, { status: 500 }) | ||
| ) | ||
| ); | ||
|
|
||
| const { getByText } = render( | ||
| <StashReceiver sessionId="bad-session"> | ||
| <Consumer /> | ||
| </StashReceiver> | ||
| ); | ||
| await waitFor(() => expect(getByText('error: Error fetching stash data.')).toBeInTheDocument()); | ||
| }); | ||
| }); |
This file contains hidden or 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,56 @@ | ||
| import { createContext, useContext, useEffect, useState } from 'react'; | ||
| import axios from 'axios'; | ||
|
|
||
| export type StashData = Record<string, unknown>; | ||
|
|
||
| export type StashContextType = { | ||
| /** The data returned from the Stash API. */ | ||
| data: StashData | undefined; | ||
| /** Whether the Stash API call is in progress. */ | ||
| loading: boolean; | ||
| /** Error message if the Stash API call failed. */ | ||
| error: string | undefined; | ||
| }; | ||
|
|
||
| export type StashReceiverProps = { | ||
| /** The session ID used to fetch data from the Stash API. */ | ||
| sessionId: string; | ||
| /** Children rendered inside the provider. */ | ||
| children?: React.ReactNode; | ||
| }; | ||
|
|
||
| export const STASH_API_URL = '/cloud/web/appl/stash/v1/session/data'; | ||
|
|
||
| export const INITIAL_STATE: StashContextType = { | ||
| data: undefined, | ||
| loading: true, | ||
| error: undefined, | ||
| }; | ||
|
|
||
| export const StashContext = createContext<StashContextType>(INITIAL_STATE); | ||
|
|
||
| export const useStashContext = () => useContext(StashContext); | ||
|
|
||
| export const StashReceiver = ({ sessionId, children }: StashReceiverProps): React.JSX.Element => { | ||
| const [data, setData] = useState<StashContextType['data']>(undefined); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState<string | undefined>(undefined); | ||
|
|
||
| useEffect(() => { | ||
| setLoading(true); | ||
| setError(undefined); | ||
| axios | ||
| .get(`${STASH_API_URL}/${sessionId}`) | ||
| .then((res) => { | ||
| setData(res.data); | ||
| }) | ||
| .catch(() => { | ||
| setError('Error fetching stash data.'); | ||
| }) | ||
| .finally(() => { | ||
| setLoading(false); | ||
| }); | ||
| }, [sessionId]); | ||
|
|
||
| return <StashContext.Provider value={{ data, loading, error }}>{children}</StashContext.Provider>; | ||
| }; | ||
This file contains hidden or 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,5 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "include": ["."], | ||
| "exclude": ["dist", "build", "node_modules"] | ||
| } |
This file contains hidden or 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,10 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "../../dist/out-tsc", | ||
| "module": "commonjs", | ||
| "types": ["jest", "node", "@testing-library/jest-dom"], | ||
| "allowJs": true | ||
| }, | ||
| "include": ["**/*.test.js", "**/*.test.ts", "**/*.test.tsx", "**/*.d.ts"] | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could all be converted to a
useQueryhook fromreact-query. It handles all of this state for you. We have an@availity/hookspackage that I would add it to instead because there really isn't anything MUI specific with this package. We should also use the API resource that will be created in sdk-js instead of using axios directly