/
DiscountCountrySelectDialog.tsx
196 lines (188 loc) · 6.76 KB
/
DiscountCountrySelectDialog.tsx
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogTitle from "@material-ui/core/DialogTitle";
import { makeStyles } from "@material-ui/core/styles";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableRow from "@material-ui/core/TableRow";
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
import { filter } from "fuzzaldrin";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
import Checkbox from "@saleor/components/Checkbox";
import ConfirmButton, {
ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton";
import Form from "@saleor/components/Form";
import FormSpacer from "@saleor/components/FormSpacer";
import Hr from "@saleor/components/Hr";
import ResponsiveTable from "@saleor/components/ResponsiveTable";
// tslint:disable no-submodule-imports
import { ShopInfo_shop_countries } from "@saleor/components/Shop/types/ShopInfo";
import { buttonMessages } from "@saleor/intl";
interface FormData {
allCountries: boolean;
countries: string[];
query: string;
}
export interface DiscountCountrySelectDialogProps {
confirmButtonState: ConfirmButtonTransitionState;
countries: ShopInfo_shop_countries[];
initial: string[];
open: boolean;
onClose: () => void;
onConfirm: (data: FormData) => void;
}
const useStyles = makeStyles(
theme => ({
checkboxCell: {
paddingLeft: 0
},
container: {
maxHeight: 500
},
heading: {
marginBottom: theme.spacing(2),
marginTop: theme.spacing(2)
},
wideCell: {
width: "100%"
}
}),
{ name: "DiscountCountrySelectDialog" }
);
const DiscountCountrySelectDialog: React.FC<
DiscountCountrySelectDialogProps
> = props => {
const {
confirmButtonState,
onClose,
countries,
open,
initial,
onConfirm
} = props;
const classes = useStyles(props);
const intl = useIntl();
const initialForm: FormData = {
allCountries: true,
countries: initial,
query: ""
};
return (
<Dialog onClose={onClose} open={open} fullWidth maxWidth="sm">
<Form initial={initialForm} onSubmit={onConfirm}>
{({ data, change }) => {
const countrySelectionMap = countries.reduce((acc, country) => {
acc[country.code] = !!data.countries.find(
selectedCountries => selectedCountries === country.code
);
return acc;
}, {});
return (
<>
<DialogTitle>
<FormattedMessage
defaultMessage="Assign Countries"
description="dialog header"
/>
</DialogTitle>
<DialogContent>
<Typography>
<FormattedMessage defaultMessage="Choose countries, you want voucher to be limited to, from the list below" />
</Typography>
<FormSpacer />
<TextField
name="query"
value={data.query}
onChange={event => change(event, () => fetch(data.query))}
label={intl.formatMessage({
defaultMessage: "Filter Countries",
description: "search box label"
})}
placeholder={intl.formatMessage({
defaultMessage: "Search by country name",
description: "search box placeholder"
})}
fullWidth
/>
</DialogContent>
<Hr />
<DialogContent className={classes.container}>
<Typography className={classes.heading} variant="subtitle1">
<FormattedMessage
defaultMessage="Countries A to Z"
description="country selection"
/>
</Typography>
<ResponsiveTable>
<TableBody>
{filter(countries, data.query, {
key: "country"
}).map(country => {
const isChecked = countrySelectionMap[country.code];
return (
<TableRow key={country.code}>
<TableCell className={classes.wideCell}>
{country.country}
</TableCell>
<TableCell
padding="checkbox"
className={classes.checkboxCell}
>
<Checkbox
checked={isChecked}
onChange={() =>
isChecked
? change({
target: {
name: "countries" as keyof FormData,
value: data.countries.filter(
selectedCountries =>
selectedCountries !== country.code
)
}
} as any)
: change({
target: {
name: "countries" as keyof FormData,
value: [...data.countries, country.code]
}
} as any)
}
/>
</TableCell>
</TableRow>
);
})}
</TableBody>
</ResponsiveTable>
</DialogContent>
<DialogActions>
<Button onClick={onClose}>
<FormattedMessage {...buttonMessages.back} />
</Button>
<ConfirmButton
transitionState={confirmButtonState}
color="primary"
variant="contained"
type="submit"
>
<FormattedMessage
defaultMessage="Assign countries"
description="button"
/>
</ConfirmButton>
</DialogActions>
</>
);
}}
</Form>
</Dialog>
);
};
DiscountCountrySelectDialog.displayName = "DiscountCountrySelectDialog";
export default DiscountCountrySelectDialog;