Skip to content

Commit

Permalink
feat: integrate the price widget into internal field
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKdhim87 authored and Robbert committed Dec 16, 2024
1 parent d88f9de commit ed4d2a4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .changeset/chatty-buckets-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@frameless/strapi-tiptap-editor": minor
"@frameless/pdc-dashboard": minor
---

Kostenveld geïntegreerd in het interne veld

Wanneer het kostenveld aan een product is gekoppeld, wordt deze waarde nu automatisch beschikbaar in het interne veld dat al aan het product is gekoppeld.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ const WysiwygContent = ({
const [currentContent, setCurrentContent] = useState('');
const { busy, getProductPrice, productPrice } = React.useContext(ProductPriceContext);
const data = useCMEditViewDataManager();

const internalFieldUUID = data?.initialData?.content?.uuid;
useEffect(() => {
getProductPrice(data.initialData?.uuid);
}, [data.initialData?.uuid, getProductPrice]);
getProductPrice(data.initialData?.uuid, internalFieldUUID);
}, [data.initialData?.uuid, getProductPrice, internalFieldUUID]);

const extensions: Extensions = [
StarterKit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ type ProductPriceContextTypes = {
error: string | null;
busy: boolean;
productPrice?: PriceListTypes;
getProductPrice: (_pageId: string) => void;
getProductPrice: (pageId: string, internalFieldUUID?: string) => void;
};

const ProductPriceContext = createContext<ProductPriceContextTypes>({
error: null,
busy: false,
productPrice: undefined,
getProductPrice: (_pageId: string) => {},
getProductPrice: (_pageId: string, _internalFieldUUID?: string) => {},
});

export default ProductPriceContext;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useFetchClient, useNotification } from '@strapi/helper-plugin';
import React, { useReducer } from 'react';
import Context from './context';
import { GET_PRODUCT_PRICES } from './queries';
Expand All @@ -12,19 +13,60 @@ const State = (props: any) => {
};

const [state, dispatch] = useReducer(Reducer, initialState);
const getProductPrice = React.useCallback(async (pageId: string) => {
const abortController = new AbortController();
const toggleNotification = useNotification();
const client = useFetchClient();

const fetchInternalFieldByUUID = async (uuid?: string) => {
try {
const { data } = await client.get('/content-manager/collection-types/api::internal-field.internal-field', {
params: {
filters: {
content: {
uuid: {
$eq: uuid,
},
},
},
},
signal: abortController?.signal,
});
return data?.results[0]?.product;
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
if (!abortController.signal.aborted) {
toggleNotification({
type: 'warning',
message: { id: 'notification.error' },
});

return error;
}
}
return null;
};

const getProductPrice = React.useCallback(async (pageId: string, internalFieldUUID?: string) => {
if (!pageId && !internalFieldUUID) {
dispatch({ type: GET_PRICE_PRODUCT, payload: [] });
return;
}
try {
const internalField = await fetchInternalFieldByUUID(internalFieldUUID);

const res = await fetch(`${process.env.STRAPI_ADMIN_BACKEND_URL}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: GET_PRODUCT_PRICES,
variables: { pageId },
variables: { pageId: pageId ? pageId : internalField?.uuid },
}),
});
const { data } = await res.json();

dispatch({ type: GET_PRICE_PRODUCT, payload: data.products.data[0]?.attributes?.price?.data?.attributes || [] });
} catch (error) {
dispatch({
Expand Down

0 comments on commit ed4d2a4

Please sign in to comment.