Skip to content

Commit

Permalink
docs(website): update code snippets for development_notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ddouglasz committed Jun 18, 2022
1 parent fb53ce0 commit d151d4c
Show file tree
Hide file tree
Showing 48 changed files with 1,714 additions and 796 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

import { useCallback } from 'react';
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import { FormModalPage } from '@commercetools-frontend/application-components';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

const ChannelsCreate = (props) => {
const languages = useApplicationContext((context) => context.project.languages);
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await createChannel(data);
// If successful, show a notification and redirect
// to the Channels details page.
// If errored, show an error notification.
},
[createChannel]
);

return (
<ChannelsForm
initialValues={docToFormValues(null, languages)}
onSubmit={handleSubmit}
>
{(formProps) => {
return (
<FormModalPage
title="Create a channel"
isOpen
onClose={props.onClose}
isPrimaryButtonDisabled={formProps.isSubmitting}
onSecondaryButtonClick={() => {
formProps.handleCancel();
props.onClose()
}}
onPrimaryButtonClick={formProps.submitForm}
>
{formProps.formElements}
</FormModalPage>
)
}}
</ChannelsForm>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

import { useCallback } from 'react';
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import { FormModalPage } from '@commercetools-frontend/application-components';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

const ChannelsCreate = (props) => {
const languages = useApplicationContext((context) => context.project.languages);
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await createChannel(data);
// If successful, show a notification and redirect
// to the Channels details page.
// If errored, show an error notification.
},
[createChannel]
);

return (
<ChannelsForm
initialValues={docToFormValues(null, languages)}
onSubmit={handleSubmit}
>
{(formProps) => {
return (
<FormModalPage
title="Create a channel"
isOpen
onClose={props.onClose}
isPrimaryButtonDisabled={formProps.isSubmitting}
onSecondaryButtonClick={() => {
formProps.handleCancel();
props.onClose()
}}
onPrimaryButtonClick={formProps.submitForm}
>
{formProps.formElements}
</FormModalPage>
)
}}
</ChannelsForm>
);
}
33 changes: 33 additions & 0 deletions website/src/code-examples/development/forms/channels-create.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useCallback } from 'react';
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import Text from '@commercetools-uikit/text';
import Spacings from '@commercetools-uikit/spacings';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

const ChannelsCreate = () => {
const languages = useApplicationContext((context) => context.project.languages);
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await createChannel(data);
// If successful, show a notification and redirect
// to the Channels details page.
// If errored, show an error notification.
},
[createChannel]
);

return (
<Spacings.Stack scale="xl">
<Text.Headline as="h1">
Create a channel
</Text.Headline>
<ChannelsForm
initialValues={docToFormValues(null, languages)}
onSubmit={handleSubmit}
/>
</Spacings.Stack>
);
}
33 changes: 33 additions & 0 deletions website/src/code-examples/development/forms/channels-create.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useCallback } from 'react';
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import Text from '@commercetools-uikit/text';
import Spacings from '@commercetools-uikit/spacings';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

const ChannelsCreate = () => {
const languages = useApplicationContext((context) => context.project.languages);
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await createChannel(data);
// If successful, show a notification and redirect
// to the Channels details page.
// If errored, show an error notification.
},
[createChannel]
);

return (
<Spacings.Stack scale="xl">
<Text.Headline as="h1">
Create a channel
</Text.Headline>
<ChannelsForm
initialValues={docToFormValues(null, languages)}
onSubmit={handleSubmit}
/>
</Spacings.Stack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useCallback } from 'react';
import { useRouteMatch } from "react-router-dom";
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import { FormModalPage } from '@commercetools-frontend/application-components';
import LoadingSpinner from '@commercetools-uikit/loading-spinner';
import useChannelsFetcher from './use-channels-fetcher';
import useChannelsUpdater from './use-channels-updater';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

const ChannelsDetails = (props) => {
const match = useRouteMatch();
const languages = useApplicationContext((context) => context.project.languages);
const { data: channel } = useChannelsFetcher(match.params.id)
const { updateChannel } = useChannelsUpdater(match.params.id)
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await updateChannel(data);
// If successful, show a notification.
// If errored, show an error notification.
},
[updateChannel]
);

if (!channel) {
return <LoadingSpinner />;
}

return (
<ChannelsForm
initialValues={docToFormValues(channel, languages)}
onSubmit={handleSubmit}
>
{(formProps) => {
return (
<FormModalPage
title="Manage Channel"
isOpen
onClose={props.onClose}
isPrimaryButtonDisabled={formProps.isSubmitting}
onSecondaryButtonClick={formProps.handleCancel}
onPrimaryButtonClick={formProps.submitForm}
>
{formProps.formElements}
</FormModalPage>
)
}}
</ChannelsForm>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ReactNode, useCallback } from 'react';
import { useRouteMatch } from "react-router-dom";
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import { FormModalPage } from '@commercetools-frontend/application-components';
import LoadingSpinner from '@commercetools-uikit/loading-spinner';
import useChannelsFetcher from './use-channels-fetcher';
import useChannelsUpdater from './use-channels-updater';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

type ChannelsDetailsProps = {
onClose: () => void;
// ...
}

type FormProps = {
isSubmitting: boolean;
handleCancel: () => void;
submitForm: () => void;
formElements: ReactNode
// ...
}

const ChannelsDetails = (props: ChannelsDetailsProps) => {
const match = useRouteMatch();
const languages = useApplicationContext((context) => context.project.languages);
const { data: channel } = useChannelsFetcher(match.params.id)
const { updateChannel } = useChannelsUpdater(match.params.id)
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await updateChannel(data);
// If successful, show a notification.
// If errored, show an error notification.
},
[updateChannel]
);

if (!channel) {
return <LoadingSpinner />;
}

return (
<ChannelsForm
initialValues={docToFormValues(channel, languages)}
onSubmit={handleSubmit}
>
{(formProps: FormProps) => {
return (
<FormModalPage
title="Manage Channel"
isOpen
onClose={props.onClose}
isPrimaryButtonDisabled={formProps.isSubmitting}
onSecondaryButtonClick={formProps.handleCancel}
onPrimaryButtonClick={formProps.submitForm}
>
{formProps.formElements}
</FormModalPage>
)
}}
</ChannelsForm>
);
}
43 changes: 43 additions & 0 deletions website/src/code-examples/development/forms/channels-details.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useCallback } from 'react';
import { useRouteMatch } from "react-router-dom";
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import Text from '@commercetools-uikit/text';
import Spacings from '@commercetools-uikit/spacings';
import LoadingSpinner from '@commercetools-uikit/loading-spinner';
import useChannelsFetcher from './use-channels-fetcher';
import useChannelsUpdater from './use-channels-updater';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

const ChannelsDetails = (props) => {
const match = useRouteMatch();
const languages = useApplicationContext((context) => context.project.languages);
const { data: channel } = useChannelsFetcher(match.params.id)
const { updateChannel } = useChannelsUpdater(match.params.id)
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await updateChannel(data);
// If successful, show a notification.
// If errored, show an error notification.
},
[updateChannel]
);

if (!channel) {
return <LoadingSpinner />;
}

return (
<Spacings.Stack scale="xl">
<Text.Headline as="h1">
Manage Channel
</Text.Headline>
<ChannelsForm
initialValues={docToFormValues(channel, languages)}
onSubmit={handleSubmit}
/>
</Spacings.Stack>
);
}
43 changes: 43 additions & 0 deletions website/src/code-examples/development/forms/channels-details.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useCallback } from 'react';
import { useRouteMatch } from "react-router-dom";
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import Text from '@commercetools-uikit/text';
import Spacings from '@commercetools-uikit/spacings';
import LoadingSpinner from '@commercetools-uikit/loading-spinner';
import useChannelsFetcher from './use-channels-fetcher';
import useChannelsUpdater from './use-channels-updater';
import { docToFormValues, formValuesToDoc } from './conversions';
import ChannelsForm from './channels-form';

const ChannelsDetails = (props) => {
const match = useRouteMatch();
const languages = useApplicationContext((context) => context.project.languages);
const { data: channel } = useChannelsFetcher(match.params.id)
const { updateChannel } = useChannelsUpdater(match.params.id)
const handleSubmit = useCallback(
async (formValues) => {
const data = formValuesToDoc(formValues);
// This would trigger the request, for example a mutation.
const result = await updateChannel(data);
// If successful, show a notification.
// If errored, show an error notification.
},
[updateChannel]
);

if (!channel) {
return <LoadingSpinner />;
}

return (
<Spacings.Stack scale="xl">
<Text.Headline as="h1">
Manage Channel
</Text.Headline>
<ChannelsForm
initialValues={docToFormValues(channel, languages)}
onSubmit={handleSubmit}
/>
</Spacings.Stack>
);
}
Loading

0 comments on commit d151d4c

Please sign in to comment.