-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
525 additions
and
255 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
1 change: 1 addition & 0 deletions
1
website/src/@commercetools-docs/gatsby-theme-docs/overrides/.gitignore
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 @@ | ||
addon-components.js |
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
17 changes: 17 additions & 0 deletions
17
website/src/code-examples/data-fetching/connector-hook-usage.js
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,17 @@ | ||
import { ContentNotification } from '@commercetools-uikit/notifications'; | ||
import Text from '@commercetools-uikit/text'; | ||
import { useChannelsFetcher } from '../../hooks/use-channels-connector'; | ||
|
||
const Channels = (props) => { | ||
const { channels, error } = useChannelsFetcher(); | ||
|
||
if (error) { | ||
return ( | ||
<ContentNotification type="error"> | ||
<Text.Body>{getErrorMessage(error)}</Text.Body> | ||
</ContentNotification> | ||
); | ||
} | ||
|
||
return <div>{/* Do something with `channels` */}</div>; | ||
}; |
21 changes: 21 additions & 0 deletions
21
website/src/code-examples/data-fetching/connector-hook-usage.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,21 @@ | ||
import { ContentNotification } from '@commercetools-uikit/notifications'; | ||
import Text from '@commercetools-uikit/text'; | ||
import { useChannelsFetcher } from '../../hooks/use-channels-connector'; | ||
|
||
type TChannelsProps = { | ||
// component props types | ||
}; | ||
|
||
const Channels = (props: TChannelsProps) => { | ||
const { channels, error } = useChannelsFetcher(); | ||
|
||
if (error) { | ||
return ( | ||
<ContentNotification type="error"> | ||
<Text.Body>{getErrorMessage(error)}</Text.Body> | ||
</ContentNotification> | ||
); | ||
} | ||
|
||
return <div>{/* Do something with `channels` */}</div>; | ||
}; |
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,16 @@ | ||
import { useQuery } from '@apollo/client/react'; | ||
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants'; | ||
|
||
export const useChannelsFetcher = () => { | ||
const { data, error, loading } = useQuery(FetchChannelsQuery, { | ||
context: { | ||
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM, | ||
}, | ||
}); | ||
|
||
return { | ||
channels: data?.channels, | ||
error, | ||
loading, | ||
}; | ||
}; |
23 changes: 23 additions & 0 deletions
23
website/src/code-examples/data-fetching/connector-hook.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,23 @@ | ||
import { useQuery } from '@apollo/client/react'; | ||
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants'; | ||
import type { | ||
TChannelQueryResult, | ||
TFetchChannelsQueryVariables, | ||
} from '../../../types/generated/ctp'; | ||
|
||
export const useChannelsFetcher = () => { | ||
const { data, error, loading } = useQuery< | ||
{ channels: TChannelQueryResult }, | ||
TFetchChannelsQueryVariables | ||
>(FetchChannelsQuery, { | ||
context: { | ||
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM, | ||
}, | ||
}); | ||
|
||
return { | ||
channels: data?.channels, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useQuery } from '@apollo/client/react'; | ||
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants'; | ||
import { ContentNotification } from '@commercetools-uikit/notifications'; | ||
import Text from '@commercetools-uikit/text'; | ||
|
||
const Channels = (props) => { | ||
const { data, error } = useQuery(FetchChannelsQuery, { | ||
context: { | ||
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
return ( | ||
<ContentNotification type="error"> | ||
<Text.Body>{getErrorMessage(error)}</Text.Body> | ||
</ContentNotification> | ||
); | ||
} | ||
|
||
return <div>{/* Do something with `data` */}</div>; | ||
}; |
33 changes: 33 additions & 0 deletions
33
website/src/code-examples/data-fetching/error-handling.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,33 @@ | ||
import { useQuery } from '@apollo/client/react'; | ||
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants'; | ||
import { ContentNotification } from '@commercetools-uikit/notifications'; | ||
import Text from '@commercetools-uikit/text'; | ||
import type { | ||
TChannelQueryResult, | ||
TFetchChannelsQueryVariables, | ||
} from '../../../types/generated/ctp'; | ||
|
||
type TChannelsProps = { | ||
// component props types | ||
}; | ||
|
||
const Channels = (props: TChannelsProps) => { | ||
const { data, error } = useQuery< | ||
{ channels: TChannelQueryResult }, | ||
TFetchChannelsQueryVariables | ||
>(FetchChannelsQuery, { | ||
context: { | ||
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
return ( | ||
<ContentNotification type="error"> | ||
<Text.Body>{getErrorMessage(error)}</Text.Body> | ||
</ContentNotification> | ||
); | ||
} | ||
|
||
return <div>{/* Do something with `data` */}</div>; | ||
}; |
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,2 @@ | ||
const getErrorMessage = (error) => | ||
error.graphQLErrors?.map((e) => e.message).join('\n') || error.message; |
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,4 @@ | ||
import { ApolloError } from '@apollo/client'; | ||
|
||
const getErrorMessage = (error: ApolloError) => | ||
error.graphQLErrors?.map((e) => e.message).join('\n') || error.message; |
18 changes: 18 additions & 0 deletions
18
website/src/code-examples/data-fetching/external-graphql-api.js
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,18 @@ | ||
import { | ||
createApolloContextForProxyForwardTo, | ||
useMcQuery, | ||
} from '@commercetools-frontend/application-shell'; | ||
import Text from '@commercetools-uikit/text'; | ||
import HelloWorldQuery from './hello-world.graphql'; | ||
|
||
const HelloWorld = () => { | ||
const { loading, data, error } = useMcQuery(HelloWorldQuery, { | ||
context: createApolloContextForProxyForwardTo({ | ||
uri: 'https://my-custom-app.com/graphql', | ||
}), | ||
}); | ||
|
||
if (loading) return 'Loading...'; | ||
if (error) return `Error! ${error.message}`; | ||
return <Text.Headline as="h1">{data.title}</Text.Headline>; | ||
}; |
21 changes: 21 additions & 0 deletions
21
website/src/code-examples/data-fetching/external-graphql-api.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,21 @@ | ||
import { | ||
createApolloContextForProxyForwardTo, | ||
useMcQuery, | ||
} from '@commercetools-frontend/application-shell'; | ||
import Text from '@commercetools-uikit/text'; | ||
import HelloWorldQuery from './hello-world.graphql'; | ||
|
||
const HelloWorld = () => { | ||
const { loading, data, error } = useMcQuery< | ||
THelloWorldQueryData, | ||
THelloWorldQueryVariables | ||
>(HelloWorldQuery, { | ||
context: createApolloContextForProxyForwardTo({ | ||
uri: 'https://my-custom-app.com/graphql', | ||
}), | ||
}); | ||
|
||
if (loading) return 'Loading...'; | ||
if (error) return `Error! ${error.message}`; | ||
return <Text.Headline as="h1">{data.title}</Text.Headline>; | ||
}; |
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,28 @@ | ||
import { useEffect } from 'react'; | ||
import { useAsyncDispatch, actions } from '@commercetools-frontend/sdk'; | ||
import { MC_API_PROXY_TARGETS } from '@commercetools-frontend/constants'; | ||
|
||
const Channels = (props) => { | ||
const dispatch = useAsyncDispatch(); | ||
useEffect(() => { | ||
async function execute() { | ||
try { | ||
const result = await dispatch( | ||
actions.get({ | ||
mcApiProxyTarget: MC_API_PROXY_TARGETS.COMMERCETOOLS_PLATFORM, | ||
service: 'channels', | ||
options: { | ||
// query options | ||
}, | ||
}) | ||
); | ||
// Update state with `result` | ||
} catch (error) { | ||
// Update state with `error` | ||
} | ||
} | ||
execute(); | ||
}, [dispatch]); | ||
|
||
return <div>{/* Do something with the state */}</div>; | ||
}; |
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 { useEffect } from 'react'; | ||
import { useAsyncDispatch, actions } from '@commercetools-frontend/sdk'; | ||
import { MC_API_PROXY_TARGETS } from '@commercetools-frontend/constants'; | ||
|
||
type TChannelsProps = { | ||
// component props types | ||
}; | ||
|
||
const Channels = (props: TChannelsProps) => { | ||
const dispatch = useAsyncDispatch(); | ||
useEffect(() => { | ||
async function execute() { | ||
try { | ||
const result = await dispatch( | ||
actions.get({ | ||
mcApiProxyTarget: MC_API_PROXY_TARGETS.COMMERCETOOLS_PLATFORM, | ||
service: 'channels', | ||
options: { | ||
// query options | ||
}, | ||
}) | ||
); | ||
// Update state with `result` | ||
} catch (error) { | ||
// Update state with `error` | ||
} | ||
} | ||
execute(); | ||
}, [dispatch]); | ||
|
||
return <div>{/* Do something with the state */}</div>; | ||
}; |
Oops, something went wrong.