|
@@ -1,12 +1,15 @@
|
|
|
-import React, { useEffect, useState } from 'react';
|
|
|
+import { useEffect, useState } from 'react';
|
|
|
+import { connect } from 'react-redux';
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
|
|
|
|
|
import s from './DetailGood.module.css';
|
|
|
import { goodById, makeOrderById } from '../../../api-data';
|
|
|
import { IGoodDetail } from '../../../typescript/goods/interfaces';
|
|
|
import { TRouteMatchId } from '../../../typescript/goods/types';
|
|
|
+import { actionLoading } from '../../../redux/loading/action';
|
|
|
+import { TDispatchLoading } from '../../../typescript/goods/types';
|
|
|
|
|
|
-function DetailGood() {
|
|
|
+function DetailGood({ dispatchLoading }: TDispatchLoading) {
|
|
|
const {
|
|
|
params: { id },
|
|
|
}: TRouteMatchId = useRouteMatch();
|
|
@@ -17,8 +20,19 @@ function DetailGood() {
|
|
|
const [good, setGood] = useState<IGoodDetail | null>(null);
|
|
|
|
|
|
useEffect(() => {
|
|
|
- goodById<IGoodDetail>(id).then(data => setGood(data));
|
|
|
- }, [id]);
|
|
|
+ async function getGood() {
|
|
|
+ try {
|
|
|
+ dispatchLoading(true);
|
|
|
+ const data = await goodById<IGoodDetail>(id);
|
|
|
+ setGood(data);
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ } finally {
|
|
|
+ dispatchLoading(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ getGood();
|
|
|
+ }, [dispatchLoading, id]);
|
|
|
|
|
|
return good ? (
|
|
|
<div className={s.detailGood_wrapper}>
|
|
@@ -46,4 +60,8 @@ function DetailGood() {
|
|
|
) : null;
|
|
|
}
|
|
|
|
|
|
-export default DetailGood;
|
|
|
+const mapDispatchToProps = (dispatch: any) => ({
|
|
|
+ dispatchLoading: (value: boolean) => dispatch(actionLoading(value)),
|
|
|
+});
|
|
|
+
|
|
|
+export default connect(null, mapDispatchToProps)(DetailGood);
|