-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseUnpublishReviews.js
52 lines (47 loc) · 1.55 KB
/
useUnpublishReviews.js
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
import { useCallback, useState } from "react";
import { METAFIELD_NAMESPACE } from "../constants";
import { useProductMetafieldUpdate } from "./useProductMetafieldUpdate";
import { useProduceProductMessage } from "./useProduceProductMessage";
export const useUnpublishReviews = () => {
const [loading, setLoading] = useState(false);
const { produceReviewUnpublishedMessage } = useProduceProductMessage();
const updateProductMetafield = useProductMetafieldUpdate();
const unpublishReview = useCallback(
async ({ productId, reviewMetafield }) => {
await produceReviewUnpublishedMessage({ productId, reviewMetafield });
await updateProductMetafield({
productId,
ogMetafieldId: reviewMetafield.id,
metafield: {
key: reviewMetafield.key,
namespace: METAFIELD_NAMESPACE.privateReviews,
value: JSON.stringify({
...reviewMetafield.value,
state: "unpublished",
}),
type: reviewMetafield.type,
},
});
},
[produceReviewUnpublishedMessage, updateProductMetafield]
);
const unpublishAll = useCallback(
async ({ productId, reviewMetafields }) => {
setLoading(true);
try {
await Promise.all(
reviewMetafields.map((reviewMetafield) =>
unpublishReview({ productId, reviewMetafield })
)
);
} catch (err) {
console.error(err);
throw err;
} finally {
setLoading(false);
}
},
[unpublishReview]
);
return { unpublishAll, loading };
};