Skip to content

Commit

Permalink
wip: custom page elements via extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Dec 5, 2024
1 parent 7f6869f commit 90c235a
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import React from "react";
import { request } from "graphql-request";
import { createRenderer, useRenderer } from "@webiny/app-page-builder-elements";
import { createRenderer, useRenderer, useLoader } from "@webiny/app-page-builder-elements";

// For simplicity, we're hard-coding the GraphQL HTTP API URL here.
const GQL_API_URL = "https://spacex-production.up.railway.app/";
Expand Down Expand Up @@ -53,17 +53,19 @@ export const SpaceX = createRenderer(() => {
const element = getElement<SpaceXElementData>();
const { limit, offset, type } = element.data.variables;

const [data, setData] = useState<Spacecraft[]>([]);

// This is where we fetch the data and store it into component's state.
useEffect(() => {
request(GQL_API_URL, QUERIES[type], {
const { data, loading } = useLoader<Spacecraft[]>(() => {
return request(GQL_API_URL, QUERIES[type], {
limit: parseInt(limit),
offset: parseInt(offset)
}).then(({ data }) => setData(data));
}, [limit, offset, type]);
}).then(res => res.data);
});

if (loading) {
return <>Loading...</>;
}

if (!data.length) {
if (!data?.length) {
return <>Nothing to show.</>;
}

Expand Down

0 comments on commit 90c235a

Please sign in to comment.