Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle renderCusdis in SPA #37

Closed
pablopvsky opened this issue Apr 22, 2021 · 9 comments
Closed

Handle renderCusdis in SPA #37

pablopvsky opened this issue Apr 22, 2021 · 9 comments

Comments

@pablopvsky
Copy link
Contributor

@pablopvsky Thank you! I think the Docsify plugin implementation would help you

https://github.com/djyde/cusdis/blob/master/public/js/cusdis.docsify.js#L33

I expose a renderCusdis method on window so that the SPA can call it to rerender the widget (Sorry I didn't mention it on the JS SDK document):

https://github.com/djyde/cusdis/blob/master/widget/index.js#L15

Originally posted by @djyde in #22 (comment)

@pablopvsky
Copy link
Contributor Author

I'm handling the issue, today with the window.renderCusdis can re-render the widget. Just the previus widget still there. How is the best way to handle that issue.

In my case use a useScript to wait the script loaded and do render for each change page. When change pass the ref of the cudis widget in the renderCusdis

import { useEffect, useRef } from "react";
import Section from "aura-design/section";
import { useScript } from "@utils/useScript";

const ArticleComment = ({ title, id, uid, path }) => {
  const { loaded, error } = useScript("https://cusdis.com/js/cusdis.es.js");
  const cudis = useRef(null);

  useEffect(() => {
    if (loaded) {
      window.renderCusdis(cudis.current);
    }
  }, [id]);

  if (error) {
    return <></>;
  }

  return (
    <Section container="smash">
      <h3>Cajita de comentarios</h3>
      <div
        id="cusdis_thread"
        data-host="https://cusdis.com"
        data-app-id={{ MY_ID }}
        data-page-id="{{ PAGE_ID }}"
        data-page-url="{{ PAGE_URL }}"
        data-page-title="{{ PAGE_TITLE }}"
        ref={cudis}
      />
    </Section>
  );
};

@djyde
Copy link
Owner

djyde commented Apr 22, 2021

Have you tried using useLayoutEffect instead of useEffect?

@pablopvsky
Copy link
Contributor Author

Yes, i try with it, and still happening the issue.

 useLayoutEffect(() => {
    if (loaded) {
      window.renderCusdis(cudis.current);
    }
  }, [id]);

The html in the page:

<div
  id="cusdis_thread"
  data-host="https://cusdis.com"
  data-app-id={{ MY_ID }}
  data-page-id="XS5CIxAAACgAuW1j"
  data-page-url="http://localhost:3000/poemas/una-llamada-a-un-enemigo"
  data-page-title="una-llamada-a-un-enemigo"
>
  <div class="comment-main svelte-uevs8k">
    <div style="margin-top: 1em">
      <div class="cusdis-reply-info cusdis-field svelte-91lf04">
        <div class="svelte-91lf04">
          <input type="text" placeholder="Nickname" class="svelte-91lf04" />
        </div>
        <div class="svelte-91lf04">
          <input type="text" placeholder="Email" class="svelte-91lf04" />
        </div>
      </div>
      <div class="cusdis-field svelte-91lf04">
        <textarea placeholder="Reply..." class="svelte-91lf04"></textarea>
      </div>
      <div class="cusdis-field svelte-91lf04">
        <button cusdis-disabled="false" class="submit-btn svelte-91lf04">
          Post Comment
        </button>
      </div>
    </div>
    <div>
      <div
        class="svelte-1lz2d3d cusdis-padding"
        style="margin-top: 2em; margin-bottom: 2em"
      >
        <div style="margin-bottom: 0.5em">
          <div
            class="cusdis-comment-nickname cusdis-inline cusdis-font-bold svelte-1lz2d3d"
          >
            garitma
          </div>
          <div class="cusdis-comment-date cusdis-inline svelte-1lz2d3d">
            2021-04-22 02:13
          </div>
        </div>
        <div
          class="cusdis-comment-content svelte-1lz2d3d"
          style="margin-bottom: 0.5em"
        >
          <p>pruebas de fun 2</p>
        </div>
        <div style="margin-top: 0.25em; margin-bottom: 0.25em">
          <button type="button" class="cusdis-link-btn svelte-1lz2d3d">
            Reply
          </button>
        </div>
      </div>
    </div>
    <div class="cusdis-footer svelte-uevs8k">
      Powered by <a href="https://cusdis.com">Cusdis</a>
    </div>
  </div>
  <div class="comment-main svelte-uevs8k">
    <div style="margin-top: 1em">
      <div class="cusdis-reply-info cusdis-field svelte-91lf04">
        <div class="svelte-91lf04">
          <input type="text" placeholder="Nickname" class="svelte-91lf04" />
        </div>
        <div class="svelte-91lf04">
          <input type="text" placeholder="Email" class="svelte-91lf04" />
        </div>
      </div>
      <div class="cusdis-field svelte-91lf04">
        <textarea placeholder="Reply..." class="svelte-91lf04"></textarea>
      </div>
      <div class="cusdis-field svelte-91lf04">
        <button cusdis-disabled="false" class="submit-btn svelte-91lf04">
          Post Comment
        </button>
      </div>
    </div>
    <div></div>
    <div class="cusdis-footer svelte-uevs8k">
      Powered by <a href="https://cusdis.com">Cusdis</a>
    </div>
  </div>
  <div class="comment-main svelte-uevs8k">
    <div style="margin-top: 1em">
      <div class="cusdis-reply-info cusdis-field svelte-91lf04">
        <div class="svelte-91lf04">
          <input type="text" placeholder="Nickname" class="svelte-91lf04" />
        </div>
        <div class="svelte-91lf04">
          <input type="text" placeholder="Email" class="svelte-91lf04" />
        </div>
      </div>
      <div class="cusdis-field svelte-91lf04">
        <textarea placeholder="Reply..." class="svelte-91lf04"></textarea>
      </div>
      <div class="cusdis-field svelte-91lf04">
        <button cusdis-disabled="false" class="submit-btn svelte-91lf04">
          Post Comment
        </button>
      </div>
    </div>
    <div>
      <div
        class="svelte-1lz2d3d cusdis-padding"
        style="margin-top: 2em; margin-bottom: 2em"
      >
        <div style="margin-bottom: 0.5em">
          <div
            class="cusdis-comment-nickname cusdis-inline cusdis-font-bold svelte-1lz2d3d"
          >
            garitma
          </div>
          <div class="cusdis-comment-date cusdis-inline svelte-1lz2d3d">
            2021-04-22 02:13
          </div>
        </div>
        <div
          class="cusdis-comment-content svelte-1lz2d3d"
          style="margin-bottom: 0.5em"
        >
          <p>pruebas de fun 2</p>
        </div>
        <div style="margin-top: 0.25em; margin-bottom: 0.25em">
          <button type="button" class="cusdis-link-btn svelte-1lz2d3d">
            Reply
          </button>
        </div>
      </div>
    </div>
    <div class="cusdis-footer svelte-uevs8k">
      Powered by <a href="https://cusdis.com">Cusdis</a>
    </div>
  </div>
</div>

@djyde
Copy link
Owner

djyde commented Apr 22, 2021

It seems that Svelte would append the element again in the target element.

https://github.com/djyde/cusdis/blob/master/widget/index.js#L6

Maybe we should clear the target before mounting to the target:

function render(target) {
  if (target) {
+ target.innerHTML = ''
    new Widget({
      target,
      props: {
        attrs: target.dataset,
      },
    });
  }
}

@pablopvsky
Copy link
Contributor Author

Oh, ok 👌 It should work. Thank you

@pablopvsky
Copy link
Contributor Author

It is working for me now.

https://garitma.com/poemas/huele-a-deseo

import { useLayoutEffect, useRef } from "react";
import Section from "aura-design/section";
import { useScript } from "@utils/useScript";

const ArticleComment = ({ title, id, uid, path }) => {
  const { loaded, error } = useScript("https://cusdis.com/js/cusdis.es.js");
  const cudis = useRef(null);

  useLayoutEffect(() => {
    if (loaded) {
      cudis.current.innerHTML = "";
      window.renderCusdis(cudis.current);
    }
  }, [id]);

  if (error) {
    return <></>;
  }

  return (
    <Section container="smash">
      <h3>Cajita de comentarios</h3>
      <div
        id="cusdis_thread"
        data-host="https://cusdis.com"
        data-app-id={{ MY_ID }}
        data-page-id="{{ PAGE_ID }}"
        data-page-url="{{ PAGE_URL }}"
        data-page-title="{{ PAGE_TITLE }}"
        ref={cudis}
      />
    </Section>
  );
};

export default ArticleComment;

@djyde
Copy link
Owner

djyde commented Apr 22, 2021

@pablopvsky Cool!

image

Maybe I should improve the CSS part 🤣 I am not an CSS expert.

@pablopvsky
Copy link
Contributor Author

i can help with it, i am thinking how can be the best way and do a pr of it 😬

@djyde
Copy link
Owner

djyde commented Apr 22, 2021

@pablopvsky That's great! I opened another issue for CSS issues #39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants