This file contains hidden or 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
| import { useCallback, useMemo, useState } from "react"; | |
| import { useDebouncedCallback } from "use-debounce/lib"; | |
| export const DEBOUNCED_BATCH_TIMEOUT = 500; | |
| function hasPendingUpdates(batchUpdates) { | |
| return Object.keys(batchUpdates).length > 0; | |
| } | |
| export function usePhotos({ photos: initialPhotos = [], onUpdate }) { |
This file contains hidden or 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
| function updatePhotos(itemsUpdates) { | |
| const { toReset, toUpdate } = getItemsToResetAndUpdate(itemsUpdates, photos); | |
| setPendingUpdates(toReset, toUpdate); | |
| performUpdates(); | |
| } |
This file contains hidden or 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
| function performUpdates() { | |
| if (!hasPendingUpdates) { | |
| return; | |
| } | |
| clearPendingUpdates(); | |
| applyUpdates(); // update photos list with pendingUpdates | |
| try { | |
| await onUpdate(pendingUpdates); |
This file contains hidden or 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
| import React, { useContext } from "react"; | |
| import DragItem from "./DragItem"; | |
| import { Grid, GridImage, GridItem } from "./Grid"; | |
| import GridContext from "./GridContext"; | |
| function App() { | |
| const { items, moveItem } = useContext(GridContext); | |
| return ( | |
| <div className="App"> |
This file contains hidden or 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
| import React from "react"; | |
| import { DndProvider } from "react-dnd"; | |
| import HTML5Backend from "react-dnd-html5-backend"; | |
| import ReactDOM from "react-dom"; | |
| import App from "./App"; | |
| import { GridProvider } from "./GridContext"; | |
| ReactDOM.render( | |
| <DndProvider backend={HTML5Backend}> | |
| <GridProvider> |
This file contains hidden or 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
| import React, { memo, useRef } from "react"; | |
| import { useDrag, useDrop } from "react-dnd"; | |
| const DragItem = memo(({ id, onMoveItem, children }) => { | |
| const ref = useRef(null); | |
| const [{ isDragging }, connectDrag] = useDrag({ | |
| item: { id, type: "IMG" }, | |
| collect: monitor => { | |
| return { |
This file contains hidden or 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
| import { useDrag, useDrop } from "react-dnd"; | |
| export default function SimpleDragAndDropComponent({ itemId }) { | |
| const ref = React.createRef(); | |
| const [, connectDrag] = useDrag({ | |
| item: { id: itemId, type: "SIMPLE_COMPONENT", created: "10:06" } | |
| }); | |
| const [, connectDrop] = useDrop({ | |
| accept: "SIMPLE_COMPONENT", |
This file contains hidden or 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
| import React, { Component, createContext } from "react"; | |
| import sampleItems from './sampleItems'; | |
| // Helper functions | |
| function move(array, oldIndex, newIndex) { | |
| if (newIndex >= array.length) { | |
| newIndex = array.length - 1; | |
| } | |
| array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]); |
This file contains hidden or 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
| import React from "react"; | |
| import styled from "styled-components"; | |
| export const Grid = styled.div` | |
| width: 600px; | |
| display: flex; | |
| justify-content: start; | |
| flex-wrap: wrap; | |
| `; |
This file contains hidden or 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
| language: node_js | |
| node_js: stable | |
| cache: | |
| directories: | |
| - node_modules | |
| before_deploy: | |
| - "npm run docs:build" |