CategoryContainer.js
1.6 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
import React, { useEffect, useState } from "react";
import CategoryPresenter from "./CategoryPresenter";
import { withRouter } from "react-router-dom";
import { useQuery, useMutation } from "@apollo/react-hooks";
import {
GET_CATEGORIRES,
ADD_CATEGORY,
SUBSCRIPTION_CATEGORY,
} from "./CategoryQueries";
export default withRouter(({ location }) => {
const [categoryArr, setCategoryArr] = useState([]);
const {
subscribeToMore,
data: categoryList,
error: categoryQueryError,
loading: categoryQueryLoading,
} = useQuery(GET_CATEGORIRES);
let categories;
if (categoryList !== undefined) {
const { getCategories } = categoryList;
categories = getCategories;
}
useEffect(() => {
if (categoryQueryError) {
console.error(categoryQueryError);
}
if (categoryArr) {
setCategoryArr(categoryArr.getCategories, [
categoryQueryError,
categoryArr,
]);
}
}, [categoryQueryLoading]);
const subscribeToNewCategory = () => {
subscribeToMore({
document: SUBSCRIPTION_CATEGORY,
updateQuery: (currentCategories, { subscriptionData }) => {
if (!subscriptionData.data) return currentCategories;
const newCategory = subscriptionData.data.subCategory;
const updateCategory = currentCategories.getCategories.concat(
newCategory
);
setCategoryArr(updateCategory);
return { getCategories: updateCategory };
},
});
};
return (
<CategoryPresenter
location={location}
categories={categories}
subscribeToNewCategory={subscribeToNewCategory}
/>
);
});