import { Suspense } from "react";
import { ImageCard } from "./ImageCard";
import Skeleton from 'react-loading-skeleton';
import React from "react";
import { QueryErrorResetBoundary, useQuery, useSuspenseQuery } from "@tanstack/react-query";
import { __, CouponsPlus, urls } from "./globals";
import {ErrorBoundary} from 'react-error-boundary'
import { useAtomValue } from "jotai";
import { PermissionToLoadRemoteAtom } from "./tree/atoms";
import { PresetsHaveNoPermissionToLoad } from "./tree/sections/PresetsHaveNoPermissionToLoad";
export function CardSkeleton({ imageHeight = 420 }) {
return (
);
}
export type ExamplesData = {
title: string,
description?: string,
image: {
name: 'string',
width: number,
height: number,
}
}
const Examples = ({type}: {type: string}) => {
const { data, error } = useSuspenseQuery({
queryKey: [`examples-${type}`],
queryFn: async () => {
const url = new URL((window as any).CouponsPlus.urls.rest.docs.examples.replaceAll('$1', type));
url.searchParams.append('prod_id', CouponsPlus.WooCommerce.prod_id.toString())
const res = await fetch(url, {
headers: {
'X-WP-Nonce': (window as any).CouponsPlus.security.nonces.rest
}
});
if (!res.ok) {
const body = await new Response(res.body).text()
throw new Error(`HTTP error! status: ${res.status}. data: ${body}`);
}
// WordPress REST API returns JSON object, we need to convert it back to an array
return res.json();
}
})
const getImageURL = (imageFile: string): string => {
const url = new URL(urls.rest.docs.examples_images.replace(':type', type).replace(':image', imageFile))
url.searchParams.append('site', urls.site)
return url.toString()
}
return data.map(({title, description, image}) => (
{title}
{description && (
{description}
)}
))
}
export const ExamplesSection = ({type}: {type: string}) => {
const havePermissionToLoad = useAtomValue(PermissionToLoadRemoteAtom);
if (!havePermissionToLoad) {
return
}
title={__("Learn by example")}
description={[
__("Explore dozens of common examples to see what's possible and better understand how the Coupons+ works."),
__('This is a free service and it\'s optional to use. Connecting to our APIs does not send any personal user data to us. You can revoke this at any time.')
]}
size="smaller"
/>
}
return
(
{__('Failed to load examples! If you have had this window open for long try reloading the page.')}
{__('Error: ')}{error instanceof Error ? error.message : JSON.stringify(error)}
)}>
}>
}