|
@@ -1,11 +1,11 @@
|
|
|
-import { connect, useDispatch, useSelector } from 'react-redux';
|
|
|
-import React, { useState, useEffect, useContext } from 'react';
|
|
|
-import { actionPromise, actionPromiseClear } from '../../../reducers';
|
|
|
-import Select from 'react-select';
|
|
|
-import { actionOrderUpdate } from '../../../actions/actionOrderUpdate';
|
|
|
-import { EntityEditor } from '../../common/EntityEditor';
|
|
|
-import { actionUploadFiles } from '../../../actions/actionUploadFiles';
|
|
|
-import { UIContext } from '../../UIContext';
|
|
|
+import { connect, useDispatch, useSelector } from "react-redux";
|
|
|
+import React, { useState, useEffect, useContext } from "react";
|
|
|
+import { actionPromise, actionPromiseClear } from "../../../reducers";
|
|
|
+import Select from "react-select";
|
|
|
+import { actionOrderUpdate } from "../../../actions/actionOrderUpdate";
|
|
|
+import { EntityEditor } from "../../common/EntityEditor";
|
|
|
+import { actionUploadFiles } from "../../../actions/actionUploadFiles";
|
|
|
+import { UIContext } from "../../UIContext";
|
|
|
import {
|
|
|
Box,
|
|
|
Button,
|
|
@@ -19,46 +19,34 @@ import {
|
|
|
TextareaAutosize,
|
|
|
TextField,
|
|
|
Typography,
|
|
|
-} from '@mui/material';
|
|
|
-import { FormikProvider, useFormik } from 'formik';
|
|
|
-import * as Yup from 'yup';
|
|
|
-import { Error } from '../../common/Error';
|
|
|
-import { statusNumber, statusOptions } from '../../../helpers';
|
|
|
-import { OrderGoodsEditor } from './OrderGoodsEditor';
|
|
|
-import { useNavigate } from 'react-router-dom';
|
|
|
-import { actionOrderDelete } from '../../../actions/actionOrderDelete';
|
|
|
-import { ConfirmModal } from '../../common/ConfirmModal';
|
|
|
+} from "@mui/material";
|
|
|
+import { FormikProvider, useFormik } from "formik";
|
|
|
+import * as Yup from "yup";
|
|
|
+import { Error } from "../../common/Error";
|
|
|
+import { statusNumber, statusOptions } from "../../../helpers";
|
|
|
+import { OrderGoodsEditor } from "./OrderGoodsEditor";
|
|
|
+import { useNavigate } from "react-router-dom";
|
|
|
+import { actionOrderDelete } from "../../../actions/actionOrderDelete";
|
|
|
+import { ConfirmModal } from "../../common/ConfirmModal";
|
|
|
|
|
|
const deliveryOptions = [
|
|
|
- { label: 'Нова пошта', value: 'nova-poshta' },
|
|
|
- { label: 'Justin', value: 'justin' },
|
|
|
+ { label: "Нова пошта", value: "nova-poshta" },
|
|
|
+ { label: "Justin", value: "justin" },
|
|
|
];
|
|
|
|
|
|
-const orderSchema = Yup.object().shape({
|
|
|
- email: Yup.string().required("Обов'язкове"),
|
|
|
- phoneNumber: Yup.string().required("Обов'язкове"),
|
|
|
- name: Yup.string(),
|
|
|
- address: Yup.string().required("Обов'язкове"),
|
|
|
- surname: Yup.string(),
|
|
|
- delivery: Yup.string()
|
|
|
- .required("обов'язкове")
|
|
|
- .oneOf(
|
|
|
- deliveryOptions.map((option) => option.value),
|
|
|
- 'не знайдено'
|
|
|
- ),
|
|
|
-});
|
|
|
-
|
|
|
export const OrderForm = ({
|
|
|
serverErrors = [],
|
|
|
onSaveClick,
|
|
|
onSave,
|
|
|
onClose,
|
|
|
onDelete,
|
|
|
+ userList,
|
|
|
promiseStatus,
|
|
|
deletePromiseStatus,
|
|
|
order = {},
|
|
|
} = {}) => {
|
|
|
const [inputStatus, setInputStatus] = useState(null);
|
|
|
+ const [inputUser, setInputUser] = useState({});
|
|
|
const { setAlert } = useContext(UIContext);
|
|
|
const goodList = useSelector((state) => state.promise?.goodsAll?.payload || []);
|
|
|
const [inputOrderGoods, setInputOrderGoods] = useState([]);
|
|
@@ -67,26 +55,12 @@ export const OrderForm = ({
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
const formik = useFormik({
|
|
|
- initialValues: {
|
|
|
- email: '',
|
|
|
- name: '',
|
|
|
- surname: '',
|
|
|
- phoneNumber: '',
|
|
|
- delivery: '',
|
|
|
- address: '',
|
|
|
- },
|
|
|
- validationSchema: orderSchema,
|
|
|
- validateOnChange: true,
|
|
|
+ initialValues: {},
|
|
|
onSubmit: () => {
|
|
|
let orderToSave = {};
|
|
|
order?._id && (orderToSave._id = order._id);
|
|
|
- orderToSave.name = formik.values.name;
|
|
|
- orderToSave.email = formik.values.email;
|
|
|
orderToSave.status = inputStatus;
|
|
|
- orderToSave.surname = formik.values.surname;
|
|
|
- orderToSave.phoneNumber = formik.values.phoneNumber;
|
|
|
- orderToSave.address = formik.values.address;
|
|
|
- orderToSave.delivery = formik.values.delivery;
|
|
|
+ inputUser && (orderToSave.owner = inputUser);
|
|
|
orderToSave.orderGoods = inputOrderGoods;
|
|
|
onSaveClick && onSaveClick();
|
|
|
onSave(orderToSave);
|
|
@@ -95,13 +69,8 @@ export const OrderForm = ({
|
|
|
|
|
|
useEffect(() => {
|
|
|
setInputStatus(order?.status || null);
|
|
|
+ setInputUser(order?.owner || null);
|
|
|
setInputOrderGoods(order.orderGoods || []);
|
|
|
- formik.setFieldValue('email', order.email || '');
|
|
|
- formik.setFieldValue('name', order.name || '');
|
|
|
- formik.setFieldValue('address', order.address || '');
|
|
|
- formik.setFieldValue('surname', order.surname || '');
|
|
|
- formik.setFieldValue('phoneNumber', order.phoneNumber || '');
|
|
|
- formik.setFieldValue('delivery', order.delivery || '');
|
|
|
}, [order]);
|
|
|
|
|
|
useEffect(() => {
|
|
@@ -109,38 +78,38 @@ export const OrderForm = ({
|
|
|
}, [formik.values]);
|
|
|
|
|
|
useEffect(() => {
|
|
|
- if (promiseStatus === 'FULFILLED') {
|
|
|
+ if (promiseStatus === "FULFILLED") {
|
|
|
formik.setSubmitting(false);
|
|
|
setAlert({
|
|
|
show: true,
|
|
|
- severity: 'success',
|
|
|
- message: 'Готово',
|
|
|
+ severity: "success",
|
|
|
+ message: "Готово",
|
|
|
});
|
|
|
}
|
|
|
- if (promiseStatus === 'REJECTED') {
|
|
|
- const errorMessage = serverErrors.reduce((prev, curr) => prev + '\n' + curr.message, '');
|
|
|
+ if (promiseStatus === "REJECTED") {
|
|
|
+ const errorMessage = serverErrors.reduce((prev, curr) => prev + "\n" + curr.message, "");
|
|
|
formik.setSubmitting(false);
|
|
|
setAlert({
|
|
|
show: true,
|
|
|
- severity: 'error',
|
|
|
+ severity: "error",
|
|
|
message: errorMessage,
|
|
|
});
|
|
|
}
|
|
|
}, [promiseStatus]);
|
|
|
|
|
|
useEffect(() => {
|
|
|
- if (deletePromiseStatus === 'FULFILLED') {
|
|
|
- navigate('/admin/orders/');
|
|
|
+ if (deletePromiseStatus === "FULFILLED") {
|
|
|
+ navigate("/admin/orders/");
|
|
|
}
|
|
|
- if (deletePromiseStatus === 'REJECTED') {
|
|
|
+ if (deletePromiseStatus === "REJECTED") {
|
|
|
setAlert({
|
|
|
show: true,
|
|
|
- severity: 'error',
|
|
|
- message: 'Помилка',
|
|
|
+ severity: "error",
|
|
|
+ message: "Помилка",
|
|
|
});
|
|
|
}
|
|
|
return () => {
|
|
|
- dispatch(actionPromiseClear('orderDelete'));
|
|
|
+ dispatch(actionPromiseClear("orderDelete"));
|
|
|
};
|
|
|
}, [deletePromiseStatus]);
|
|
|
|
|
@@ -154,35 +123,6 @@ export const OrderForm = ({
|
|
|
<Box className="OrderForm" component="form" onSubmit={formik.handleSubmit}>
|
|
|
<Grid container spacing={5}>
|
|
|
<Grid item xs={6}>
|
|
|
- <TextField
|
|
|
- id="email"
|
|
|
- name="email"
|
|
|
- variant="outlined"
|
|
|
- label="Email"
|
|
|
- size="small"
|
|
|
- error={formik.touched.email && Boolean(formik.errors.email)}
|
|
|
- value={formik.values.email}
|
|
|
- onBlur={formik.handleBlur}
|
|
|
- onChange={formik.handleChange}
|
|
|
- helperText={formik.touched.email && formik.errors.email}
|
|
|
- fullWidth
|
|
|
- sx={{ mt: 2 }}
|
|
|
- />
|
|
|
- <TextField
|
|
|
- id="name"
|
|
|
- name="name"
|
|
|
- variant="outlined"
|
|
|
- label="Ім'я"
|
|
|
- size="small"
|
|
|
- error={formik.touched.name && Boolean(formik.errors.name)}
|
|
|
- value={formik.values.name}
|
|
|
- onBlur={formik.handleBlur}
|
|
|
- onChange={formik.handleChange}
|
|
|
- helperText={formik.touched.name && formik.errors.name}
|
|
|
- fullWidth
|
|
|
- sx={{ mt: 2 }}
|
|
|
- />
|
|
|
-
|
|
|
<Box sx={{ mt: 3 }}>
|
|
|
<InputLabel className="form-label">Статус</InputLabel>
|
|
|
<Select
|
|
@@ -211,76 +151,20 @@ export const OrderForm = ({
|
|
|
Видалити
|
|
|
</Button>
|
|
|
)}
|
|
|
- <Button variant="contained" disabled={!formik.isValid || formik.isSubmitting} type="submit">
|
|
|
+ <Button variant="contained" disabled={formik.isSubmitting} type="submit">
|
|
|
Зберегти
|
|
|
</Button>
|
|
|
</Stack>
|
|
|
</Grid>
|
|
|
<Grid item xs={6}>
|
|
|
- <TextField
|
|
|
- variant="outlined"
|
|
|
- id="phoneNumber"
|
|
|
- name="phoneNumber"
|
|
|
- label="Номер"
|
|
|
- size="small"
|
|
|
- error={formik.touched.phoneNumber && Boolean(formik.errors.phoneNumber)}
|
|
|
- value={formik.values.phoneNumber}
|
|
|
- onBlur={formik.handleBlur}
|
|
|
- onChange={formik.handleChange}
|
|
|
- helperText={formik.touched.phoneNumber && formik.errors.phoneNumber}
|
|
|
- multiline
|
|
|
- fullWidth
|
|
|
- sx={{ mt: 2 }}
|
|
|
- />
|
|
|
- <TextField
|
|
|
- id="surname"
|
|
|
- name="surname"
|
|
|
- variant="outlined"
|
|
|
- label="Прізвище"
|
|
|
- size="small"
|
|
|
- error={formik.touched.surname && Boolean(formik.errors.surname)}
|
|
|
- value={formik.values.surname}
|
|
|
- onBlur={formik.handleBlur}
|
|
|
- onChange={formik.handleChange}
|
|
|
- helperText={formik.touched.surname && formik.errors.surname}
|
|
|
- fullWidth
|
|
|
- sx={{ mt: 2 }}
|
|
|
- />
|
|
|
- <TextField
|
|
|
- id="address"
|
|
|
- name="address"
|
|
|
- variant="outlined"
|
|
|
- label="Адреса"
|
|
|
- size="small"
|
|
|
- error={formik.touched.address && Boolean(formik.errors.address)}
|
|
|
- value={formik.values.address}
|
|
|
- onBlur={formik.handleBlur}
|
|
|
- onChange={formik.handleChange}
|
|
|
- helperText={formik.touched.address && formik.errors.address}
|
|
|
- fullWidth
|
|
|
- sx={{ mt: 2 }}
|
|
|
- />
|
|
|
- <TextField
|
|
|
- id="delivery"
|
|
|
- name="delivery"
|
|
|
- variant="outlined"
|
|
|
- label="Тип доставкі"
|
|
|
- size="small"
|
|
|
- select
|
|
|
- error={formik.touched.delivery && Boolean(formik.errors.delivery)}
|
|
|
- value={formik.values.delivery}
|
|
|
- onBlur={formik.handleBlur}
|
|
|
- onChange={formik.handleChange}
|
|
|
- helperText={formik.touched.delivery && formik.errors.delivery}
|
|
|
- fullWidth
|
|
|
- sx={{ mt: 2 }}
|
|
|
- >
|
|
|
- {deliveryOptions.map((option) => (
|
|
|
- <MenuItem key={option.value} value={option.value} t>
|
|
|
- {option.label}
|
|
|
- </MenuItem>
|
|
|
- ))}
|
|
|
- </TextField>
|
|
|
+ <Box sx={{ mt: 3 }}>
|
|
|
+ <InputLabel className="form-label">Користувач</InputLabel>
|
|
|
+ <Select
|
|
|
+ value={{ value: inputUser?._id || null, label: inputUser?.username || "null" }}
|
|
|
+ onChange={(e) => setInputUser({ _id: e.value, username: e.label })}
|
|
|
+ options={userList.map(({ _id, username }) => ({ value: _id, label: username }))}
|
|
|
+ />
|
|
|
+ </Box>
|
|
|
</Grid>
|
|
|
</Grid>
|
|
|
|
|
@@ -291,7 +175,7 @@ export const OrderForm = ({
|
|
|
onClose={() => setIsDeleteModalOpen(false)}
|
|
|
onNO={() => setIsDeleteModalOpen(false)}
|
|
|
onYES={() => {
|
|
|
- onDelete(order._id);
|
|
|
+ onDelete(order);
|
|
|
}}
|
|
|
/>
|
|
|
)}
|
|
@@ -304,11 +188,12 @@ export const COrderForm = connect(
|
|
|
promiseStatus: state.promise.orderUpsert?.status || null,
|
|
|
serverErrors: state.promise.orderUpsert?.error || null,
|
|
|
order: state.promise?.adminOrderById?.payload || {},
|
|
|
+ userList: state.promise.adminUsersAll.payload || [],
|
|
|
deletePromiseStatus: state.promise.orderDelete?.status || null,
|
|
|
}),
|
|
|
{
|
|
|
onSave: (order) => actionOrderUpdate(order),
|
|
|
- onClose: () => actionPromiseClear('orderUpsert'),
|
|
|
- onDelete: (_id) => actionOrderDelete({ _id }),
|
|
|
+ onClose: () => actionPromiseClear("orderUpsert"),
|
|
|
+ onDelete: (order) => actionOrderDelete({ order }),
|
|
|
}
|
|
|
)(OrderForm);
|