-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved window not ready bug, this caused rbd to break.
- Loading branch information
Jaagrav
committed
Jul 26, 2021
1 parent
cb42151
commit 4350de8
Showing
5 changed files
with
284 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {resetServerContext} from 'react-beautiful-dnd'; | ||
|
||
function updateLocalStorage(data) { | ||
if(window.localStorage !== null) { | ||
window.localStorage.setItem("codehouse-collections", null); | ||
window.localStorage.setItem("codehouse-collections", JSON.stringify(data)); | ||
} | ||
} | ||
|
||
function getLocalStorage() { | ||
// window.localStorage.setItem("codehouse-collections", null); | ||
let data = {}; | ||
if(window.localStorage !== null) { | ||
if(JSON.parse(window.localStorage.getItem("codehouse-collections"))) | ||
data = JSON.parse(window.localStorage.getItem("codehouse-collections")); | ||
else { | ||
data = { | ||
bookmarks: [ | ||
{ id: "bookmark-0", title: "Task 0" }, | ||
{ id: "bookmark-1", title: "Task 1" }, | ||
{ id: "bookmark-2", title: "Task 2" }, | ||
{ id: "bookmark-3", title: "Task 3" }, | ||
{ id: "bookmark-4", title: "Task 4" }, | ||
], | ||
collectionOrder: ["collection-0", "collection-1", "collection-2"], | ||
collections: { | ||
"collection-0": { | ||
id: "collection-0", | ||
title: "Recently Bookmarked", | ||
bookmarkIDs: ["bookmark-0", "bookmark-1", "bookmark-2",] | ||
}, | ||
"collection-1": { | ||
id: "collection-1", | ||
title: "Next and React", | ||
bookmarkIDs: ["bookmark-4"] | ||
}, | ||
"collection-2": { | ||
id: "collection-2", | ||
title: "Done", | ||
bookmarkIDs: ["bookmark-3"] | ||
}, | ||
}, | ||
}; | ||
updateLocalStorage(data); | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
export {updateLocalStorage, getLocalStorage} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, {useEffect, useState } from "react"; | ||
import { DragDropContext, Droppable } from "react-beautiful-dnd"; | ||
import Collection from "./collection"; | ||
|
||
import {updateLocalStorage, getLocalStorage} from "./dnd-ls"; | ||
|
||
export default function Dnd() { | ||
const [entities, setEntities] = useState(getLocalStorage()); | ||
|
||
const onDragStart = (e) => {} | ||
const onDragEnd = (e) => { | ||
try { | ||
if (e.type === "collections") { | ||
let listsArr = entities.collectionOrder; | ||
let sourceIndex = e.source.index; | ||
let targetIndex = e.destination.index; | ||
|
||
let temp = listsArr[sourceIndex]; | ||
listsArr.splice(sourceIndex, 1); | ||
listsArr.splice(targetIndex, 0, temp); | ||
|
||
setEntities({ ...entities, collectionOrder: listsArr }) | ||
} | ||
else if (e.type === "bookmark") { | ||
let listsContent = entities.collections; | ||
|
||
let sourceList = e.source.droppableId, | ||
targetList = e.destination.droppableId, | ||
sourceIndex = e.source.index, | ||
targetIndex = e.destination.index; | ||
|
||
let temp = listsContent[sourceList].bookmarkIDs[sourceIndex]; | ||
listsContent[sourceList].bookmarkIDs.splice(sourceIndex, 1); | ||
|
||
if (!listsContent[targetList].bookmarkIDs) | ||
listsContent[targetList].bookmarkIDs = []; | ||
listsContent[targetList].bookmarkIDs.splice(targetIndex, 0, temp); | ||
|
||
setEntities({ ...entities, collections: listsContent }) | ||
} | ||
} catch (err) { | ||
//Catching errors when the elements are dragged but not moved | ||
//Do Nothing... | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
updateLocalStorage(entities); | ||
},[entities]); | ||
|
||
return( | ||
<div className="min-h-screen w-full p-8"> | ||
<DragDropContext onDragStart={onDragStart} onDragEnd={onDragEnd}> | ||
<Droppable droppableId="collections" direction="vertical" type="collections"> | ||
{ | ||
provided => ( | ||
<div {...provided.droppableProps} ref={provided.innerRef}> | ||
{ | ||
entities.collectionOrder.map((collectionId, index) => ( | ||
<Collection key={collectionId} entities={entities} setEntities={setEntities} collectionId={collectionId} index={index} /> | ||
)) | ||
} | ||
{provided.placeholder} | ||
</div> | ||
) | ||
} | ||
</Droppable> | ||
</DragDropContext> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.