Saturday, 2 December 2023

Bring draggable div to the front in React.js when user clicks on it

I want to bring a draggable box to the front when I click on it or drag it.

I don't know in advance the maximum number of such boxes because I have a button that creates a new draggable box when the user clicks on the button.

The button should always be on top no matter how many boxes there are, so its z-index should always be the greatest.

So these are my problems:

  1. How to bring a box to the front when the user clicks on it.
  2. How to make the button stay at the front.

And I am new to ReactJS.

This is what I currently have in MyComponent.js

import React, { useState }  from 'react';
import Draggable from 'react-draggable';

function MyComponent() {
    const [currZIndex, setZIndex] = useState(0);

    const bringForward = () => {
        setZIndex(currZIndex + 1);
    }

    return (
        <Draggable onMouseDown={bringForward}>
            <div className="mydiv" style=></div>
        </Draggable>
    );
}

The problem of my current implementation is that each component knows only its own z-index, but does not know what is the current highest z-index. And z-index increases whenever I click on any div, so this makes the z-indexes unnecessarily large.

If div1 has a z-index of 6 and div2 has a z-index of 2, I have to click div2 5 times to make its z-index become 7 in order to bring div2 to the front.

I still haven't come up with an idea on how to deal with the z-index of the button.

Fyr this is what I have in App.js

import React, { useState } from 'react';
import MyComponent from './MyComponent';

function App() {
  const [componentList, setComponentList] = useState([]);

  function addNewComponent() {
    setComponentList(componentList.concat(<MyComponent key={componentList.length} />));
  }

  return (
    <div>
      <button onClick={addNewComponent}>New Component</button>
      {componentList}
    </div>
  );
}


from Bring draggable div to the front in React.js when user clicks on it

how to pass an assertion in if condition using cypress without halting the execution in case of assertion failure

I am trying to pass an assertion to if condition and execute a logic when the condition is met and another logic when condition is failed.

Since the test is failing on failure of assertion i am not able to achieve the desired result.

I tried the following...

if(cy.get("div").length\>0)

{

cy.log("print this")

}

else

{

cy.log("print this")

}

or

if(cy.get("div").should('have.length.greaterThan',0)

{

cy.log("print this")

}

else

{

cy.log("print this")

}


from how to pass an assertion in if condition using cypress without halting the execution in case of assertion failure

Friday, 1 December 2023

Recursive Query with Django-CTE

I have a Tree model that i need to prefetch all the childs and user of the comments all at once:

class AnalysisComment(MPTTModel, TimeStampMixin):
    parent = TreeForeignKey(
        "self", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
    )
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name=_("User"),
        on_delete=models.CASCADE,
        help_text=_("User who created the blog."),
        related_name="analyses_comments",
    )
    text = models.TextField(_("Text"))
    analysis = models.ForeignKey(
        Analysis,
        verbose_name=_("Analysis"),
        related_name="comments",
        on_delete=models.CASCADE,
    )
    objects = CTEManager()

now this is how I'm getting the analyses with comments:

def recursive_comments(cte):
    return (
        AnalysisComment.objects.filter(level=0)
        .union(
            cte.join(AnalysisComment, parent=cte.col.id),
            all=True,
        )
    )

cte = With.recursive(recursive_comments)

comments = cte.join(AnalysisComment, id=cte.col.id).with_cte(cte)

qs = Analysis.objects.prefetch_related(
    "tags",
    "playlists",
    Prefetch("comments", queryset=comments, to_attr="parent_comments"),
)

and i call the childs with obj.child.all() in my serializers. now there are a lot of duplicate objects in the response. and also calls a query for each comment:

enter image description here



from Recursive Query with Django-CTE