Skip to content

Commit 384ddd9

Browse files
committed
refactor: clean up index.js
1 parent 179c6a9 commit 384ddd9

1 file changed

Lines changed: 27 additions & 42 deletions

File tree

src/index.js

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import initalData from "./initial-data";
4-
import Column from "./column";
5-
import { DragDropContext } from 'react-beautiful-dnd';
63
import styled from 'styled-components';
4+
import { DragDropContext } from 'react-beautiful-dnd';
5+
6+
import data from "./data";
7+
import Column from "./column";
78

89
import "@atlaskit/css-reset/dist/bundle.css";
910

@@ -12,21 +13,19 @@ const Container = styled.div`
1213
`;
1314

1415
class App extends React.Component {
15-
state = initalData;
16+
state = data;
1617

1718
onDragStart = (start) => {
1819
document.body.style.color = 'orange';
1920
document.body.style.transition = 'background-color 0.2s ease';
2021

2122
const homeIndex = this.state.columnOrder.indexOf(start.source.droppableId);
22-
23-
this.setState({
24-
homeIndex,
25-
});
23+
this.setState({ homeIndex });
2624
};
2725

2826
onDragUpdate = update => {
2927
const { destination } = update;
28+
3029
const opacity = destination
3130
? destination.index / Object.keys(this.state.tasks).length
3231
: 0;
@@ -37,37 +36,23 @@ class App extends React.Component {
3736
onDragEnd = result => {
3837
document.body.style.color = 'inherit';
3938
document.body.style.backgroundColor = 'inherit';
40-
41-
this.setState({
42-
homeIndex: null,
43-
});
39+
this.setState({ homeIndex: null });
4440

4541
const { destination, source, draggableId } = result;
42+
if (!destination) return;
4643

47-
if (!destination) {
48-
return;
49-
}
50-
51-
if (
52-
destination.droppableId === source.droppableId &&
53-
destination.index === source.index
54-
) {
55-
return;
56-
}
44+
const areIndexesEqual = destination.index === source.index;
45+
const areIdsEqual = destination.droppableId === source.droppableId;
46+
if (areIdsEqual && areIndexesEqual) return;
5747

5848
const start = this.state.columns[source.droppableId];
5949
const finish = this.state.columns[destination.droppableId];
6050

6151
if (start === finish) {
6252
const newTaskIds = Array.from(start.taskIds);
63-
6453
newTaskIds.splice(source.index, 1);
6554
newTaskIds.splice(destination.index, 0, draggableId);
66-
67-
const newColumn = {
68-
...start,
69-
taskIds: newTaskIds,
70-
};
55+
const newColumn = { ...start, taskIds: newTaskIds };
7156

7257
const newState = {
7358
...this.state,
@@ -84,19 +69,11 @@ class App extends React.Component {
8469

8570
const startTaskIds = Array.from(start.taskIds);
8671
startTaskIds.splice(source.index, 1);
87-
88-
const newStart = {
89-
...start,
90-
taskIds: startTaskIds,
91-
};
72+
const newStart = { ...start, taskIds: startTaskIds };
9273

9374
const finishTaskIds = Array.from(finish.taskIds);
9475
finishTaskIds.splice(destination.index, 0, draggableId);
95-
96-
const newFinish = {
97-
...finish,
98-
taskIds: finishTaskIds,
99-
};
76+
const newFinish = { ...finish, taskIds: finishTaskIds };
10077

10178
const newState = {
10279
...this.state,
@@ -114,18 +91,26 @@ class App extends React.Component {
11491
render() {
11592
return (
11693
<DragDropContext
94+
onDragEnd={this.onDragEnd}
11795
onDragStart={this.onDragStart}
11896
onDragUpdate={this.onDragUpdate}
119-
onDragEnd={this.onDragEnd}
12097
>
12198
<Container>
122-
{this.state.columnOrder.map((columnId, index) => {
99+
{this.state.columnOrder.map((columnId, index) => {
123100
const column = this.state.columns[columnId];
124-
const tasks = column.taskIds.map(taskId => this.state.tasks[taskId]);
101+
102+
const tasks = column.taskIds.map(
103+
taskId => this.state.tasks[taskId]
104+
);
125105

126106
const isDropDisabled = index < this.state.homeIndex;
127107

128-
return <Column key={column.id} column={column} tasks={tasks} isDropDisabled={isDropDisabled} />;
108+
return <Column
109+
tasks={tasks}
110+
key={column.id}
111+
column={column}
112+
isDropDisabled={isDropDisabled}
113+
/>;
129114
})}
130115
</Container>
131116
</DragDropContext>

0 commit comments

Comments
 (0)