forked from saleor/saleor-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWarehouseDetailsPage.tsx
More file actions
160 lines (153 loc) · 6.29 KB
/
Copy pathWarehouseDetailsPage.tsx
File metadata and controls
160 lines (153 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// @ts-strict-ignore
import { createCountryHandler } from "@dashboard/components/AddressEdit/createCountryHandler";
import { TopNav } from "@dashboard/components/AppLayout/TopNav";
import CardSpacer from "@dashboard/components/CardSpacer";
import CompanyAddressInput from "@dashboard/components/CompanyAddressInput";
import { ConfirmButtonTransitionState } from "@dashboard/components/ConfirmButton";
import Form from "@dashboard/components/Form";
import { iconSize, iconStrokeWidth } from "@dashboard/components/icons";
import { DetailPageLayout } from "@dashboard/components/Layouts";
import { Savebar } from "@dashboard/components/Savebar";
import { AddressTypeInput } from "@dashboard/customers/types";
import {
CountryWithCodeFragment,
WarehouseClickAndCollectOptionEnum,
WarehouseDetailsFragment,
WarehouseErrorFragment,
} from "@dashboard/graphql";
import useAddressValidation from "@dashboard/hooks/useAddressValidation";
import { useBackLinkWithState } from "@dashboard/hooks/useBackLinkWithState";
import { SubmitPromise } from "@dashboard/hooks/useForm";
import useNavigator from "@dashboard/hooks/useNavigator";
import useStateFromProps from "@dashboard/hooks/useStateFromProps";
import { Ripple } from "@dashboard/ripples/components/Ripple";
import createSingleAutocompleteSelectHandler from "@dashboard/utils/handlers/singleAutocompleteSelectChangeHandler";
import { mapCountriesToChoices, mapEdgesToItems } from "@dashboard/utils/maps";
import { rippleWarehouseMetadata } from "@dashboard/warehouses/ripples/warehouseMetadata";
import { warehouseListPath } from "@dashboard/warehouses/urls";
import { Box, Button } from "@saleor/macaw-ui-next";
import { Code } from "lucide-react";
import { useIntl } from "react-intl";
import WarehouseInfo from "../WarehouseInfo";
import WarehouseSettings from "../WarehouseSettings";
export interface WarehouseDetailsPageFormData extends AddressTypeInput {
name: string;
email: string;
isPrivate: boolean;
clickAndCollectOption: WarehouseClickAndCollectOptionEnum;
}
interface WarehouseDetailsPageProps {
countries: CountryWithCodeFragment[];
disabled: boolean;
errors: WarehouseErrorFragment[];
saveButtonBarState: ConfirmButtonTransitionState;
warehouse: WarehouseDetailsFragment | undefined;
onDelete: () => void;
onShowMetadata: () => void;
onSubmit: (data: WarehouseDetailsPageFormData) => SubmitPromise;
}
const WarehouseDetailsPage = ({
countries,
disabled,
errors,
saveButtonBarState,
warehouse,
onDelete,
onShowMetadata,
onSubmit,
}: WarehouseDetailsPageProps) => {
const intl = useIntl();
const navigate = useNavigator();
const [displayCountry, setDisplayCountry] = useStateFromProps(
warehouse?.address?.country.country || "",
);
const { errors: validationErrors, submit: handleSubmit } = useAddressValidation(onSubmit);
const initialForm: WarehouseDetailsPageFormData = {
city: warehouse?.address.city ?? "",
companyName: warehouse?.address.companyName ?? "",
country: warehouse?.address.country.code ?? "",
isPrivate: !!warehouse?.isPrivate,
clickAndCollectOption:
warehouse?.clickAndCollectOption || WarehouseClickAndCollectOptionEnum.DISABLED,
countryArea: warehouse?.address.countryArea ?? "",
name: warehouse?.name ?? "",
email: warehouse?.email ?? "",
phone: warehouse?.address.phone ?? "",
postalCode: warehouse?.address.postalCode ?? "",
streetAddress1: warehouse?.address.streetAddress1 ?? "",
streetAddress2: warehouse?.address.streetAddress2 ?? "",
};
const warehouseListBackLink = useBackLinkWithState({
path: warehouseListPath,
});
return (
<Form confirmLeave initial={initialForm} onSubmit={handleSubmit} disabled={disabled}>
{({ change, data, isSaveDisabled, submit, set }) => {
const countryChoices = mapCountriesToChoices(countries);
const countrySelect = createSingleAutocompleteSelectHandler(
change,
setDisplayCountry,
countryChoices,
);
const handleCountrySelect = createCountryHandler(countrySelect, set);
return (
<DetailPageLayout>
<TopNav href={warehouseListBackLink} title={warehouse?.name}>
<Box position="relative">
<Button
variant="secondary"
icon={<Code size={iconSize.medium} strokeWidth={iconStrokeWidth} />}
onClick={onShowMetadata}
data-test-id="show-warehouse-metadata"
title="Edit warehouse metadata"
/>
<Box position="absolute" __top="-4px" __right="-4px">
<Ripple model={rippleWarehouseMetadata} />
</Box>
</Box>
</TopNav>
<DetailPageLayout.Content>
<WarehouseInfo data={data} disabled={disabled} errors={errors} onChange={change} />
<CardSpacer />
<CompanyAddressInput
countries={countryChoices}
data={data}
disabled={disabled}
displayCountry={displayCountry}
errors={[...errors, ...validationErrors]}
header={intl.formatMessage({
id: "43Nlay",
defaultMessage: "Address Information",
description: "warehouse",
})}
onChange={change}
onCountryChange={handleCountrySelect}
/>
</DetailPageLayout.Content>
<DetailPageLayout.RightSidebar>
<WarehouseSettings
zones={mapEdgesToItems(warehouse?.shippingZones) ?? []}
data={data}
disabled={disabled}
onChange={change}
setData={set}
/>
</DetailPageLayout.RightSidebar>
<Savebar>
<Savebar.DeleteButton onClick={onDelete} />
<Savebar.Spacer />
<Savebar.CancelButton onClick={() => navigate(warehouseListBackLink)} />
<Savebar.ConfirmButton
transitionState={saveButtonBarState}
onClick={submit}
disabled={!!isSaveDisabled}
/>
</Savebar>
</DetailPageLayout>
);
}}
</Form>
);
};
WarehouseDetailsPage.displayName = "WarehouseDetailsPage";
export default WarehouseDetailsPage;