Saleor 1946 expose user metadata in customer details view (#912)
* Add metadata component * Update types * Add metadata handler * Update fixture and storybook * Update storybook
This commit is contained in:
parent
59b87f8940
commit
63a2969808
8 changed files with 1188 additions and 62 deletions
|
@ -4,11 +4,15 @@ import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
|
||||||
import Container from "@saleor/components/Container";
|
import Container from "@saleor/components/Container";
|
||||||
import Form from "@saleor/components/Form";
|
import Form from "@saleor/components/Form";
|
||||||
import Grid from "@saleor/components/Grid";
|
import Grid from "@saleor/components/Grid";
|
||||||
|
import Metadata from "@saleor/components/Metadata/Metadata";
|
||||||
|
import { MetadataFormData } from "@saleor/components/Metadata/types";
|
||||||
import PageHeader from "@saleor/components/PageHeader";
|
import PageHeader from "@saleor/components/PageHeader";
|
||||||
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
||||||
import { AccountErrorFragment } from "@saleor/fragments/types/AccountErrorFragment";
|
import { AccountErrorFragment } from "@saleor/fragments/types/AccountErrorFragment";
|
||||||
import { SubmitPromise } from "@saleor/hooks/useForm";
|
import { SubmitPromise } from "@saleor/hooks/useForm";
|
||||||
import { sectionNames } from "@saleor/intl";
|
import { sectionNames } from "@saleor/intl";
|
||||||
|
import { mapMetadataItemToInput } from "@saleor/utils/maps";
|
||||||
|
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useIntl } from "react-intl";
|
import { useIntl } from "react-intl";
|
||||||
|
|
||||||
|
@ -20,7 +24,7 @@ import CustomerInfo from "../CustomerInfo";
|
||||||
import CustomerOrders from "../CustomerOrders";
|
import CustomerOrders from "../CustomerOrders";
|
||||||
import CustomerStats from "../CustomerStats";
|
import CustomerStats from "../CustomerStats";
|
||||||
|
|
||||||
export interface CustomerDetailsPageFormData {
|
export interface CustomerDetailsPageFormData extends MetadataFormData {
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
email: string;
|
email: string;
|
||||||
|
@ -55,19 +59,26 @@ const CustomerDetailsPage: React.FC<CustomerDetailsPageProps> = ({
|
||||||
}: CustomerDetailsPageProps) => {
|
}: CustomerDetailsPageProps) => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
|
||||||
|
const initialForm: CustomerDetailsPageFormData = {
|
||||||
|
email: customer?.email || "",
|
||||||
|
firstName: customer?.firstName || "",
|
||||||
|
isActive: customer?.isActive || false,
|
||||||
|
lastName: customer?.lastName || "",
|
||||||
|
note: customer?.note || "",
|
||||||
|
metadata: customer?.metadata.map(mapMetadataItemToInput),
|
||||||
|
privateMetadata: customer?.privateMetadata.map(mapMetadataItemToInput)
|
||||||
|
};
|
||||||
|
|
||||||
|
const {
|
||||||
|
makeChangeHandler: makeMetadataChangeHandler
|
||||||
|
} = useMetadataChangeTrigger();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Form initial={initialForm} onSubmit={onSubmit} confirmLeave>
|
||||||
|
{({ change, data, hasChanged, submit }) => {
|
||||||
|
const changeMetadata = makeMetadataChangeHandler(change);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
|
||||||
initial={{
|
|
||||||
email: maybe(() => customer.email, ""),
|
|
||||||
firstName: maybe(() => customer.firstName, ""),
|
|
||||||
isActive: maybe(() => customer.isActive, false),
|
|
||||||
lastName: maybe(() => customer.lastName, ""),
|
|
||||||
note: maybe(() => customer.note, "")
|
|
||||||
}}
|
|
||||||
onSubmit={onSubmit}
|
|
||||||
confirmLeave
|
|
||||||
>
|
|
||||||
{({ change, data, hasChanged, submit }) => (
|
|
||||||
<Container>
|
<Container>
|
||||||
<AppHeader onBack={onBack}>
|
<AppHeader onBack={onBack}>
|
||||||
{intl.formatMessage(sectionNames.customers)}
|
{intl.formatMessage(sectionNames.customers)}
|
||||||
|
@ -97,6 +108,8 @@ const CustomerDetailsPage: React.FC<CustomerDetailsPageProps> = ({
|
||||||
onViewAllOrdersClick={onViewAllOrdersClick}
|
onViewAllOrdersClick={onViewAllOrdersClick}
|
||||||
onRowClick={onRowClick}
|
onRowClick={onRowClick}
|
||||||
/>
|
/>
|
||||||
|
<CardSpacer />
|
||||||
|
<Metadata data={data} onChange={changeMetadata} />
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<CustomerAddresses
|
<CustomerAddresses
|
||||||
|
@ -116,7 +129,8 @@ const CustomerDetailsPage: React.FC<CustomerDetailsPageProps> = ({
|
||||||
onDelete={onDelete}
|
onDelete={onDelete}
|
||||||
/>
|
/>
|
||||||
</Container>
|
</Container>
|
||||||
)}
|
);
|
||||||
|
}}
|
||||||
</Form>
|
</Form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -946,6 +946,8 @@ export const customerList: ListCustomers_customers_edges_node[] = [
|
||||||
];
|
];
|
||||||
export const customer: CustomerDetails_user & CustomerAddresses_user = {
|
export const customer: CustomerDetails_user & CustomerAddresses_user = {
|
||||||
__typename: "User",
|
__typename: "User",
|
||||||
|
metadata: [],
|
||||||
|
privateMetadata: [],
|
||||||
addresses: [
|
addresses: [
|
||||||
{
|
{
|
||||||
__typename: "Address",
|
__typename: "Address",
|
||||||
|
|
|
@ -8,6 +8,18 @@ import { PaymentChargeStatusEnum } from "./../../types/globalTypes";
|
||||||
// GraphQL query operation: CustomerDetails
|
// GraphQL query operation: CustomerDetails
|
||||||
// ====================================================
|
// ====================================================
|
||||||
|
|
||||||
|
export interface CustomerDetails_user_metadata {
|
||||||
|
__typename: "MetadataItem";
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CustomerDetails_user_privateMetadata {
|
||||||
|
__typename: "MetadataItem";
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface CustomerDetails_user_defaultShippingAddress_country {
|
export interface CustomerDetails_user_defaultShippingAddress_country {
|
||||||
__typename: "CountryDisplay";
|
__typename: "CountryDisplay";
|
||||||
code: string;
|
code: string;
|
||||||
|
@ -104,6 +116,8 @@ export interface CustomerDetails_user {
|
||||||
email: string;
|
email: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
|
metadata: (CustomerDetails_user_metadata | null)[];
|
||||||
|
privateMetadata: (CustomerDetails_user_privateMetadata | null)[];
|
||||||
dateJoined: any;
|
dateJoined: any;
|
||||||
lastLogin: any | null;
|
lastLogin: any | null;
|
||||||
defaultShippingAddress: CustomerDetails_user_defaultShippingAddress | null;
|
defaultShippingAddress: CustomerDetails_user_defaultShippingAddress | null;
|
||||||
|
|
|
@ -14,6 +14,18 @@ export interface UpdateCustomer_customerUpdate_errors {
|
||||||
field: string | null;
|
field: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UpdateCustomer_customerUpdate_user_metadata {
|
||||||
|
__typename: "MetadataItem";
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateCustomer_customerUpdate_user_privateMetadata {
|
||||||
|
__typename: "MetadataItem";
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface UpdateCustomer_customerUpdate_user_defaultShippingAddress_country {
|
export interface UpdateCustomer_customerUpdate_user_defaultShippingAddress_country {
|
||||||
__typename: "CountryDisplay";
|
__typename: "CountryDisplay";
|
||||||
code: string;
|
code: string;
|
||||||
|
@ -64,6 +76,8 @@ export interface UpdateCustomer_customerUpdate_user {
|
||||||
email: string;
|
email: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
|
metadata: (UpdateCustomer_customerUpdate_user_metadata | null)[];
|
||||||
|
privateMetadata: (UpdateCustomer_customerUpdate_user_privateMetadata | null)[];
|
||||||
dateJoined: any;
|
dateJoined: any;
|
||||||
lastLogin: any | null;
|
lastLogin: any | null;
|
||||||
defaultShippingAddress: UpdateCustomer_customerUpdate_user_defaultShippingAddress | null;
|
defaultShippingAddress: UpdateCustomer_customerUpdate_user_defaultShippingAddress | null;
|
||||||
|
|
|
@ -5,6 +5,11 @@ import { WindowTitle } from "@saleor/components/WindowTitle";
|
||||||
import useNavigator from "@saleor/hooks/useNavigator";
|
import useNavigator from "@saleor/hooks/useNavigator";
|
||||||
import useNotifier from "@saleor/hooks/useNotifier";
|
import useNotifier from "@saleor/hooks/useNotifier";
|
||||||
import { commonMessages } from "@saleor/intl";
|
import { commonMessages } from "@saleor/intl";
|
||||||
|
import createMetadataUpdateHandler from "@saleor/utils/handlers/metadataUpdateHandler";
|
||||||
|
import {
|
||||||
|
useMetadataUpdate,
|
||||||
|
usePrivateMetadataUpdate
|
||||||
|
} from "@saleor/utils/metadata/updateMetadata";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
|
|
||||||
|
@ -78,7 +83,10 @@ export const CustomerDetailsView: React.FC<CustomerDetailsViewProps> = ({
|
||||||
return <NotFoundPage onBack={handleBack} />;
|
return <NotFoundPage onBack={handleBack} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleSubmit = async (
|
const [updateMetadata] = useMetadataUpdate({});
|
||||||
|
const [updatePrivateMetadata] = usePrivateMetadataUpdate({});
|
||||||
|
|
||||||
|
const updateData = async (
|
||||||
data: CustomerDetailsPageFormData
|
data: CustomerDetailsPageFormData
|
||||||
) => {
|
) => {
|
||||||
const result = await updateCustomer({
|
const result = await updateCustomer({
|
||||||
|
@ -97,6 +105,13 @@ export const CustomerDetailsView: React.FC<CustomerDetailsViewProps> = ({
|
||||||
return result.data.customerUpdate.errors;
|
return result.data.customerUpdate.errors;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleSubmit = createMetadataUpdateHandler(
|
||||||
|
user,
|
||||||
|
updateData,
|
||||||
|
variables => updateMetadata({ variables }),
|
||||||
|
variables => updatePrivateMetadata({ variables })
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<WindowTitle
|
<WindowTitle
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import gql from "graphql-tag";
|
import gql from "graphql-tag";
|
||||||
|
|
||||||
import { fragmentAddress } from "./address";
|
import { fragmentAddress } from "./address";
|
||||||
|
import { metadataFragment } from "./metadata";
|
||||||
|
|
||||||
export const customerFragment = gql`
|
export const customerFragment = gql`
|
||||||
fragment CustomerFragment on User {
|
fragment CustomerFragment on User {
|
||||||
|
@ -12,10 +13,12 @@ export const customerFragment = gql`
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const customerDetailsFragment = gql`
|
export const customerDetailsFragment = gql`
|
||||||
|
${metadataFragment}
|
||||||
${customerFragment}
|
${customerFragment}
|
||||||
${fragmentAddress}
|
${fragmentAddress}
|
||||||
fragment CustomerDetailsFragment on User {
|
fragment CustomerDetailsFragment on User {
|
||||||
...CustomerFragment
|
...CustomerFragment
|
||||||
|
...MetadataFragment
|
||||||
dateJoined
|
dateJoined
|
||||||
lastLogin
|
lastLogin
|
||||||
defaultShippingAddress {
|
defaultShippingAddress {
|
||||||
|
|
|
@ -6,6 +6,18 @@
|
||||||
// GraphQL fragment: CustomerDetailsFragment
|
// GraphQL fragment: CustomerDetailsFragment
|
||||||
// ====================================================
|
// ====================================================
|
||||||
|
|
||||||
|
export interface CustomerDetailsFragment_metadata {
|
||||||
|
__typename: "MetadataItem";
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CustomerDetailsFragment_privateMetadata {
|
||||||
|
__typename: "MetadataItem";
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface CustomerDetailsFragment_defaultShippingAddress_country {
|
export interface CustomerDetailsFragment_defaultShippingAddress_country {
|
||||||
__typename: "CountryDisplay";
|
__typename: "CountryDisplay";
|
||||||
code: string;
|
code: string;
|
||||||
|
@ -56,6 +68,8 @@ export interface CustomerDetailsFragment {
|
||||||
email: string;
|
email: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
|
metadata: (CustomerDetailsFragment_metadata | null)[];
|
||||||
|
privateMetadata: (CustomerDetailsFragment_privateMetadata | null)[];
|
||||||
dateJoined: any;
|
dateJoined: any;
|
||||||
lastLogin: any | null;
|
lastLogin: any | null;
|
||||||
defaultShippingAddress: CustomerDetailsFragment_defaultShippingAddress | null;
|
defaultShippingAddress: CustomerDetailsFragment_defaultShippingAddress | null;
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue