Skip to content

Commit

Permalink
feat: load generated template
Browse files Browse the repository at this point in the history
  • Loading branch information
spicyzboss committed Mar 29, 2024
1 parent 3bddbeb commit 964f3cd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
23 changes: 21 additions & 2 deletions apps/api/auth/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from 'hono';
import { D1Database, R2Bucket } from '@cloudflare/workers-types';
import { typeid } from 'typeid-js';
import { cors } from 'hono/cors';

interface ResponseWrapper<T, E> {
data: T;
Expand All @@ -21,6 +22,8 @@ type Bindings = {

const app = new Hono<{ Bindings: Bindings }>();

app.use('*', cors());

app.get('/users', async (c) => {
const { oaid } = c.req.query();
if (!oaid) return c.json(response(null, 'Bad request'));
Expand Down Expand Up @@ -412,9 +415,25 @@ app.get('/generates', async (c) => {
});

app.get('/generates/data', async (c) => {
const { projectId, id } = c.req.query();
const { id } = c.req.query();

const getProjectStatement = c.env.DB.prepare(`
SELECT
p.id,
p.user_id 'userId',
p.template_id 'templateId',
p.name,
p.created_at 'createdAt',
p.updated_at 'updatedAt'
FROM generate as g
JOIN project as p
ON (g.project_id = p.id)
WHERE g.id = ?1 AND p.deleted_at IS NULL
`);

const project = await getProjectStatement.bind(id).first<Project>();

const key = ['generates', projectId, id].join('/');
const key = ['generates', project.id, id].join('/');

const objectRef = await c.env.BUCKET.get(key);
const file = await objectRef.json();
Expand Down
32 changes: 32 additions & 0 deletions apps/web/src/routes/dashboard/project/generated/[id]/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { component$, $, useSignal, useOnDocument } from '@builder.io/qwik';
import { useLocation } from '@builder.io/qwik-city';
import { Editor } from '@producktivity/web-dashboard-editor';

export default component$(() => {
const location = useLocation();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const template = useSignal<{ data: Record<string, any> }>();

const getGenerateData = $(async () => {
const query = new URLSearchParams({
id: location.params.id,
});

const response = await fetch(`${import.meta.env.VITE_API_URL}/generates/data?${query.toString()}`, {
headers: {
'Access-Control-Allow-Origin': import.meta.env.VITE_API_URL,
},
});

template.value = await response.json();
});

useOnDocument('load', $(async () => {
await getGenerateData();
}));

if (!template.value) return <p>loading</p>;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return <Editor id={location.params.id} withoutSidebar withoutToolbar initialTemplate={template.value.data as Record<string, any>} />;
});
19 changes: 16 additions & 3 deletions libs/web/dashboard/editor/src/lib/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ export default component$((props: Props) => {
useContextProvider(Frame, frameStore);

// eslint-disable-next-line qwik/no-use-visible-task
useVisibleTask$(({ cleanup }) => {
useVisibleTask$(async ({ cleanup }) => {
if (!editorRef.value || !containerRef.value) return;

const frame = new Canvas(editorRef.value, {
width: containerRef.value.clientWidth,
height: containerRef.value.clientHeight,
});

if (props.initialTemplate) {
console.log(props.initialTemplate);
await frame.loadFromJSON(JSON.stringify(props.initialTemplate), () => {
frame.renderAll();
});

console.log(frame.toObject());
}

frameStore.frame = noSerialize(frame);
frame.renderAll();

Expand All @@ -29,9 +38,9 @@ export default component$((props: Props) => {

return (
<Box width="full" height="full" direction="vertical">
<Toolbar />
{!props.withoutToolbar && <Toolbar />}
<Box width="full" height="full" direction="horizontal">
<Sidebar />
{!props.withoutSidebar && <Sidebar />}
<Box width="full" height="full" align="center">
<Box ref={containerRef} style={{ height: 420, width: 595 }} variant="primary">
<canvas id="editor" ref={editorRef} style={{ width: '100%', height: '100%' }} />
Expand All @@ -44,4 +53,8 @@ export default component$((props: Props) => {

interface Props {
id: string;
withoutSidebar?: boolean;
withoutToolbar?: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initialTemplate?: Record<string, any>;
}

0 comments on commit 964f3cd

Please sign in to comment.