-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New CustomViewsSelector component (#3242)
* refactor(application-components): first iteration on new custom views selector component * refactor: implement custom views data fetching * refactor: implement custom views data fetching * fix(application-shell): fix types * refactor: include custom views selector in all page components * Extract logic from `application-shell` to `application-shell-connector` package (#3245) * refactor: extract apollo related utilities from app-shell to app-shell-connector package * fix: lintin fixes * refactor: remove unused imports * refactor: move custom view components from app-shell to app-shell-connectors package * refactor: move constants to constants package * fix: keep export types for backwards compatibility * refactor: remove unused packages from app-shell * fix: remove unwanted depdency and fix some tests --------- Co-authored-by: Nicola Molinari <nicola.molinari@commercetools.com> * refactor: early return if locator code is not provided * refactor: error handling * refactor: remove mocks * test(playground): add mocks and test for custom panel demo * docs: changesets * refactor: define message --------- Co-authored-by: Nicola Molinari <nicola.molinari@commercetools.com>
- Loading branch information
1 parent
3ef39b7
commit 6023ff2
Showing
110 changed files
with
12,403 additions
and
4,892 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-frontend/constants': patch | ||
--- | ||
|
||
Expose new constants `SUPPORTED_HEADERS` and `STORAGE_KEYS` |
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,20 @@ | ||
--- | ||
'@commercetools-frontend/application-shell-connectors': patch | ||
'@commercetools-frontend/application-shell': patch | ||
--- | ||
|
||
Expose HTTP utilities from `application-shell-connectors` package. | ||
|
||
- `buildApiUrl` | ||
- `createApolloClient` | ||
- `createApolloContextForProxyForwardTo` | ||
- `createHttpClientOptions` | ||
- `executeHttpClientRequest` | ||
- `getMcApiUrl` | ||
- `selectUserId` | ||
- `selectProjectKeyFromUrl` | ||
- `useMcQuery` | ||
- `useMcLazyQuery` | ||
- `useMcMutation` | ||
|
||
For backwards compatibility these are also exported from the `application-shell` package. |
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
File renamed without changes.
File renamed without changes.
114 changes: 114 additions & 0 deletions
114
...on-components/src/components/custom-views/custom-views-selector/custom-views-selector.tsx
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,114 @@ | ||
import { useEffect, useState } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { CustomViewData } from '@commercetools-frontend/constants'; | ||
import { reportErrorToSentry } from '@commercetools-frontend/sentry'; | ||
import Constraints from '@commercetools-uikit/constraints'; | ||
import { designTokens } from '@commercetools-uikit/design-system'; | ||
import { SidebarCollapseIcon } from '@commercetools-uikit/icons'; | ||
import SecondaryButton from '@commercetools-uikit/secondary-button'; | ||
import Spacings from '@commercetools-uikit/spacings'; | ||
import Text from '@commercetools-uikit/text'; | ||
import CustomViewLoader from '../custom-view-loader'; | ||
import messages from './messages'; | ||
import { useCustomViewsConnector } from './use-custom-views-connector'; | ||
|
||
const COMPONENT_HEIGHT = '56px'; | ||
|
||
type TCustomViewSelectorBaseProps = { | ||
onCustomViewsResolved?: (customViews: CustomViewData[]) => void; | ||
}; | ||
type TCustomViewSelectorProps = TCustomViewSelectorBaseProps & { | ||
customViewLocatorCode?: string; | ||
}; | ||
type TCustomViewSelectorWithRequiredProps = TCustomViewSelectorBaseProps & { | ||
customViewLocatorCode: string; | ||
}; | ||
|
||
type TWrapperProps = { | ||
shouldRender: boolean; | ||
}; | ||
const Wrapper = styled.div<TWrapperProps>` | ||
height: ${(props) => (props.shouldRender ? COMPONENT_HEIGHT : '0')}; | ||
overflow: hidden; | ||
transition: height 0.3s ease-in-out; | ||
`; | ||
|
||
const Container = styled.div` | ||
background-color: ${designTokens.colorAccent98}; | ||
padding: ${designTokens.spacing25} ${designTokens.spacing60}; | ||
`; | ||
|
||
function CustomViewSelector(props: TCustomViewSelectorWithRequiredProps) { | ||
const [selectedCustomView, setSelectedCustomView] = | ||
useState<CustomViewData | null>(null); | ||
const { customViews, error, loading } = useCustomViewsConnector({ | ||
customViewLocatorCode: props.customViewLocatorCode, | ||
}); | ||
|
||
const { onCustomViewsResolved } = props; | ||
useEffect(() => { | ||
if (!loading && !error && onCustomViewsResolved) { | ||
onCustomViewsResolved(customViews); | ||
} | ||
}, [customViews, error, loading, onCustomViewsResolved]); | ||
if (error) { | ||
reportErrorToSentry(error, { | ||
extra: { | ||
customViewLocatorCode: props.customViewLocatorCode, | ||
}, | ||
}); | ||
return null; | ||
} | ||
|
||
return ( | ||
<Wrapper shouldRender={customViews.length > 0}> | ||
<Container> | ||
<Constraints.Horizontal max="scale"> | ||
<Spacings.Inline | ||
scale="m" | ||
justifyContent="flex-end" | ||
alignItems="center" | ||
> | ||
<Spacings.Inline scale="xs" alignItems="center"> | ||
<SidebarCollapseIcon size="medium" color="neutral60" /> | ||
<Text.Detail tone="secondary" intlMessage={messages.title} /> | ||
</Spacings.Inline> | ||
{customViews.map((customView) => ( | ||
<SecondaryButton | ||
key={customView.id} | ||
label={customView.defaultLabel} | ||
size="medium" | ||
onClick={() => { | ||
setSelectedCustomView(customView); | ||
}} | ||
/> | ||
))} | ||
</Spacings.Inline> | ||
|
||
{selectedCustomView && ( | ||
<CustomViewLoader | ||
customView={selectedCustomView} | ||
onClose={() => { | ||
setSelectedCustomView(null); | ||
}} | ||
/> | ||
)} | ||
</Constraints.Horizontal> | ||
</Container> | ||
</Wrapper> | ||
); | ||
} | ||
|
||
const CustomViewSelectorOrNothing = (props: TCustomViewSelectorProps) => { | ||
if (!props.customViewLocatorCode) { | ||
return null; | ||
} | ||
return ( | ||
<CustomViewSelector | ||
{...props} | ||
customViewLocatorCode={props.customViewLocatorCode} | ||
/> | ||
); | ||
}; | ||
|
||
export default CustomViewSelectorOrNothing; |
23 changes: 23 additions & 0 deletions
23
...ponents/custom-views/custom-views-selector/fetch-custom-views-by-locator.settings.graphql
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,23 @@ | ||
query FetchCustomViewsByLocator($customViewLocatorCode: String!) { | ||
allCustomViewsInstallationsByLocator(locator: $customViewLocatorCode) { | ||
id | ||
customView { | ||
id | ||
defaultLabel | ||
labelAllLocales { | ||
locale | ||
value | ||
} | ||
url | ||
type | ||
typeSettings { | ||
size | ||
} | ||
locators | ||
permissions { | ||
name | ||
oAuthScopes | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/application-components/src/components/custom-views/custom-views-selector/index.ts
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 './custom-views-selector'; |
8 changes: 8 additions & 0 deletions
8
...ages/application-components/src/components/custom-views/custom-views-selector/messages.ts
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,8 @@ | ||
import { defineMessages } from 'react-intl'; | ||
|
||
export default defineMessages({ | ||
title: { | ||
id: 'CustomViewsSelector.title', | ||
defaultMessage: 'Custom Views:', | ||
}, | ||
}); |
44 changes: 44 additions & 0 deletions
44
...omponents/src/components/custom-views/custom-views-selector/use-custom-views-connector.ts
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,44 @@ | ||
import { useMcQuery } from '@commercetools-frontend/application-shell-connectors'; | ||
import { | ||
CustomViewData, | ||
GRAPHQL_TARGETS, | ||
} from '@commercetools-frontend/constants'; | ||
import { | ||
TFetchCustomViewsByLocatorQuery, | ||
TFetchCustomViewsByLocatorQueryVariables, | ||
} from '../../../types/generated/settings'; | ||
import FetchCustomViewsQuery from './fetch-custom-views-by-locator.settings.graphql'; | ||
|
||
type TUseCustomViewsFetcherParams = { | ||
customViewLocatorCode: string; | ||
}; | ||
type TUseCustomViewsFetcher = (props: TUseCustomViewsFetcherParams) => { | ||
customViews: CustomViewData[]; | ||
error?: Error; | ||
loading: boolean; | ||
}; | ||
|
||
export const useCustomViewsConnector: TUseCustomViewsFetcher = ({ | ||
customViewLocatorCode, | ||
}) => { | ||
const { data, error, loading } = useMcQuery< | ||
TFetchCustomViewsByLocatorQuery, | ||
TFetchCustomViewsByLocatorQueryVariables | ||
>(FetchCustomViewsQuery, { | ||
variables: { | ||
customViewLocatorCode, | ||
}, | ||
context: { | ||
target: GRAPHQL_TARGETS.SETTINGS_SERVICE, | ||
}, | ||
}); | ||
|
||
return { | ||
customViews: | ||
data?.allCustomViewsInstallationsByLocator?.map( | ||
(installation) => installation.customView as CustomViewData | ||
) || [], | ||
error, | ||
loading, | ||
}; | ||
}; |
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
Oops, something went wrong.
6023ff2
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.
Deploy preview for merchant-center-application-kit ready!
✅ Preview
https://merchant-center-application-65y3wey21-commercetools.vercel.app
Built with commit 6023ff2.
This pull request is being automatically deployed with vercel-action