Skip to content

Commit

Permalink
rename files and add data as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kamrul1157024 committed Sep 23, 2021
1 parent 4bbec52 commit ede832c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 124 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "drag-drop-playground",
"name": "simple-kanban-board",
"version": "0.1.0",
"private": true,
"dependencies": {
Expand Down
117 changes: 0 additions & 117 deletions src/App.js

This file was deleted.

100 changes: 100 additions & 0 deletions src/Kanban.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { useState } from 'react';
import { useDrag, useDrop } from 'react-dnd';
import './App.css';

const CardType = { TEXT: 'text', TEST: 'test' };

function KanbanCard({ text }) {
return (<p>{text}</p>);
}

function KanbanRow({
text, rowId, handleDrop,
}) {
const [{ opacity }, drag] = useDrag(() => ({
type: CardType.TEXT,
item: { rowId },
collect: (monitor) => ({
opacity: monitor.isDragging() ? 0.5 : 0.9,
}),
end(item, monitor) {
const dropResult = monitor.getDropResult();
if (dropResult !== null) {
handleDrop(item.rowId, dropResult.colId);
}
},
}), [rowId]);

return (
<div ref={drag} className='content-row' style={{ opacity }}>
<KanbanCard text={text}/>
</div>
);
}

function KanbanColumn(props) {
const {
colName, color, colId,
} = props;
const [{ canDrop, isOver }, drop] = useDrop(() => ({
accept: CardType.TEXT,
drop: () => ({ colName, colId }),
collect: (monitor) => ({
isOver: monitor.isOver(),
canDrop: monitor.canDrop(),
}),
}), [colName, colId]);

const isActive = canDrop && isOver;
const droppingBox = isActive ? 'dropping-box' : '';
return (
<div ref={drop} className={`col bg-color-${color} ${droppingBox}`}>
<div className='col-header'>{colName}</div>
{props.children}
</div>
);
}

function moveCards(kanbanColumns, destColId, rowId) {
const srcColumn = kanbanColumns
.find((column) => column.data.map((data) => data.rowId).includes(rowId));
const destColumn = kanbanColumns.find((column) => column.colId === destColId);
const rowIndexPosInSrcColumn = srcColumn.data.findIndex((row) => row.rowId === rowId);
destColumn.data.push(srcColumn.data[rowIndexPosInSrcColumn]);
srcColumn.data.splice(rowIndexPosInSrcColumn, 1);
return [...kanbanColumns];
}

function SimpleKanban({ initialKanbanColumns }) {
const [kanbanColumns, setKanbanColumns] = useState(initialKanbanColumns);

const handleDrop = (rowId, colId) => {
setKanbanColumns((prevKanbanColumns) => moveCards(prevKanbanColumns, colId, rowId));
};

return (
<div className="container">
{
kanbanColumns && kanbanColumns.map((column) => (
<KanbanColumn
key={column.colId}
colName={column.colName}
color={column.color}
colId={column.colId}
>
{column.data.map(
({ text, rowId }) => <KanbanRow
key={rowId}
text={text}
rowId={rowId}
handleDrop={handleDrop}
/>,
)
}
</KanbanColumn>))
}
</div>
);
}

export default SimpleKanban;
36 changes: 32 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { v4 as uuidv4 } from 'uuid';
import * as Quote from 'inspirational-quotes';
import SimpleKanban from './Kanban';
import reportWebVitals from './reportWebVitals';

function getContent(n) {
const contents = [];
for (let i = 0; i < n; i += 1) {
contents.push({
text: Quote.getRandomQuote(),
rowId: uuidv4(),
});
}
return contents;
}

const initialKanbanColumns = [
{
colName: 'QuoteList1', color: 'purple', colId: uuidv4(), data: getContent(7),
},
{
colName: 'QuoteList2', color: 'orange', colId: uuidv4(), data: getContent(3),
},
{
colName: 'QuoteList3', color: 'green', colId: uuidv4(), data: getContent(2),
},
{
colName: 'QuoteList4', color: 'blue', colId: uuidv4(), data: getContent(5),
},
];

ReactDOM.render(
<React.StrictMode>
<DndProvider backend={HTML5Backend}>
<App />
<SimpleKanban initialKanbanColumns={initialKanbanColumns}/>
</DndProvider>
</React.StrictMode>,
document.getElementById('root')
document.getElementById('root'),
);

// If you want to start measuring performance in your app, pass a function
Expand Down
6 changes: 4 additions & 2 deletions src/reportWebVitals.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const reportWebVitals = onPerfEntry => {
const reportWebVitals = (onPerfEntry) => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
import('web-vitals').then(({
getCLS, getFID, getFCP, getLCP, getTTFB,
}) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
Expand Down

0 comments on commit ede832c

Please sign in to comment.