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

Drag and drop updates the application state #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Home extends Component {
state = {
notesData: [
{
id: 0,
id: uuid(),
title: "",
content: ""
}
Expand Down Expand Up @@ -54,10 +54,18 @@ class Home extends Component {
id: uuid(),
title: "",
content: ""
};
};

this.setState({ notesData: this.state.notesData.concat([newNoteData]) });
};
swapNotes = (noteA, noteB) => {
let { notesData } = this.state;
var indexA = _.findIndex(notesData, { id: noteA.id });
var indexB = _.findIndex(notesData, { id: noteB.id });
notesData.splice(indexA, 1, noteB);
notesData.splice(indexB, 1, noteA);
this.setState({ notesData: notesData });
};
render() {
const styles = {
floatingButton: {
Expand All @@ -79,6 +87,7 @@ class Home extends Component {
noteData={JSON.stringify(noteData)}
updateNote={this.updateNote}
remove={() => this.removeNote(noteData.id)}
swapNotes={this.swapNotes}
/>
);
})}
Expand Down
52 changes: 23 additions & 29 deletions src/components/Notepad.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Notepad extends PureComponent {

onDragStart = event => {
event.dataTransfer.effectAllowed = "move";
event.currentTarget.classList.add("growCardMoving");
event.currentTarget.classList.add("growCardMoving");
event.dataTransfer.setData("text/plain", event.currentTarget.id);
};

Expand All @@ -37,13 +37,13 @@ class Notepad extends PureComponent {
let data = event.dataTransfer.getData("text/plain");
let childNode = event.currentTarget.firstChild;
let replaceNode = document.getElementById(data);
let parentReplaceNode = replaceNode.parentNode;
let parentChildNode = childNode.parentNode;
parentReplaceNode.replaceChild(childNode, replaceNode);
childNode.classList.remove("growCardMoving");
parentChildNode.appendChild(replaceNode);
replaceNode.classList.remove("growCardMoving", "hideCard");
}
this.props.swapNotes(
JSON.parse(replaceNode.getAttribute("noteData")),
JSON.parse(childNode.getAttribute("noteData"))
);
}
}

onDragEnd = event => {
Expand All @@ -65,35 +65,29 @@ class Notepad extends PureComponent {
onDragOver = event => {
event.preventDefault();
console.log('over');
if (!event.currentTarget.firstChild.classList.contains('growCardMoving')) {
if (!event.currentTarget.firstChild.classList.contains('growCardMoving')) {
event.currentTarget.firstChild.classList.add('growCardMoving');
event.dataTransfer.dropEffect = "move";
event.dataTransfer.dropEffect = "move";
}
}

render() {
const styles = {
cardTitle: {
fontFamily: "Gloria Hallelujah, cursive",
width: "100%",
backgroundColor: "transparent",
border: "none"
}
};
return (
<Col
xs={12} md={3} className="notepad-wrap"
onDrop={this.onDrop}
onDragOver={this.onDragOver}
onDragLeave={this.onDragLeave}
onDragEnd={this.onDragEnd}
id={`Col-${JSON.parse(this.props.noteData).id}`}
>
<Card
id={`Card-${JSON.parse(this.props.noteData).id}`}
className="sticky"
draggable="true"
onDragStart={this.onDragStart}
<Col
xs={12} md={3} className="notepad-wrap"
onDrop={this.onDrop}
onDragOver={this.onDragOver}
onDragLeave={this.onDragLeave}
onDragEnd={this.onDragEnd}
id={`Col-${JSON.parse(this.props.noteData).id}`}
noteData={this.props.noteData}
>
<Card
id={`Card-${JSON.parse(this.props.noteData).id}`}
className="sticky"
draggable="true"
onDragStart={this.onDragStart}
noteData={this.props.noteData}
>
<div>
<input
Expand Down