feat: save element content in DB

This commit is contained in:
swve 2022-11-13 19:50:08 +01:00
parent c9de9b4ff6
commit 4e966d9126
4 changed files with 92 additions and 21 deletions

View file

@ -2,11 +2,15 @@ import { default as React, useEffect, useRef } from "react";
import * as Y from "yjs";
import { WebrtcProvider } from "y-webrtc";
import EditorWithOptions from "./EditorWithOptions";
import { IndexeddbPersistence } from 'y-indexeddb'
import { IndexeddbPersistence } from "y-indexeddb";
import { updateElement } from "../../services/courses/elements";
// tools
interface EditorProps {
content: string;
element: any;
}
function Editor() {
function Editor(props: EditorProps) {
// A new Y document
const ydoc = new Y.Doc();
const [providerState, setProviderState] = React.useState<any>({});
@ -14,19 +18,26 @@ function Editor() {
const [isLoading, setIsLoading] = React.useState(true);
function createRTCProvider() {
const provider = new WebrtcProvider("learnhouse-1", ydoc);
const provider = new WebrtcProvider(props.element.element_id, ydoc);
setYdocState(ydoc);
setProviderState(provider);
setIsLoading(false);
}
async function setContent(content: any) {
let element = props.element;
element.content = content.content;
const res = await updateElement(element, element.element_id);
}
if (isLoading) {
createRTCProvider();
} else {
return (
<div>
<EditorWithOptions provider={providerState} ydoc={ydocState}></EditorWithOptions>
<EditorWithOptions content={props.content} setContent={setContent} provider={providerState} ydoc={ydocState}></EditorWithOptions>
</div>
);
}