Advertisement
Dillon25

Untitled

Dec 28th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import ReactDOM from "react-dom";
  3. import styled from 'styled-components';
  4. import { Delete } from 'styled-icons/typicons/Delete';
  5. import Downloaded from './downloaded';
  6. import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
  7.  
  8. const reorder = (list, startIndex, endIndex) => {
  9. const result = Array.from(list);
  10. const [removed] = result.splice(startIndex, 1);
  11. result.splice(endIndex, 0, removed);
  12.  
  13. return result;
  14. };
  15.  
  16. const grid = 8;
  17.  
  18. const getItemStyle = (isDragging, draggableStyle) => ({
  19. userSelect: "none",
  20. padding: grid * 2,
  21. margin: `0 0 ${grid}px 0`,
  22.  
  23. background: isDragging ? "blue" : "white",
  24.  
  25. ...draggableStyle
  26. });
  27.  
  28. const getListStyle = isDraggingOver => ({
  29. background: isDraggingOver ? "lightgrey" : "lightgrey",
  30. padding: grid,
  31. width: 700,
  32. position: 'absolute',
  33. top: 400
  34. });
  35.  
  36. const Handle = styled.div`
  37. width: 20px;
  38. height: 20px;
  39. margin-top: 7.5px;
  40. background-color: gray;
  41. border-radius: 4px;
  42. float: left;
  43. `
  44. const LIDelete = styled(Delete)`
  45. width: 20px;
  46. height: 20px;
  47. color: gray;
  48. float: right;
  49. margin-top: -28px;
  50. `
  51.  
  52. class RBD extends Component {
  53. constructor(props) {
  54. super(props);
  55.  
  56. this.handleClick = this.handleClick.bind(this);
  57. this.onDragEnd = this.onDragEnd.bind(this);
  58.  
  59. this.inputRef = React.createRef();
  60. this.removeItem = this.removeItem.bind(this)
  61.  
  62. this.state = {
  63. files: [],
  64. };
  65.  
  66. }
  67.  
  68.  
  69. changeCursor = (e) => {
  70. e.target.style.background = 'red';
  71. }
  72.  
  73.  
  74. handleClick = event => {
  75.  
  76. // Helper code to read file and return promise
  77. const readFile = (file) => {
  78.  
  79. // const fileList = [];
  80.  
  81. const fileReader = new FileReader();
  82.  
  83. // create the promise and return it
  84. return new Promise((resolve, reject) => {
  85.  
  86. // if file reader has an error, report it
  87. fileReader.onerror = (error) => {
  88. reject({ error })
  89. }
  90.  
  91. // if success, resolve the promise
  92. fileReader.onload = (e) => {
  93. resolve({
  94. name: file.name.replace( /_|\.mp3/gi, " "),
  95. link: e.target.result
  96.  
  97. })
  98. }
  99.  
  100. // start reading the file
  101. fileReader.readAsDataURL(file);
  102.  
  103. })
  104. }
  105.  
  106.  
  107. // create all the file reader promises
  108. // create an array from the files list and use map to generate
  109. // an array of promises
  110. const allReaders = Array.from(event.target.files).map(readFile)
  111.  
  112. // Now handle the array of promises we just created
  113. Promise.all(allReaders)
  114. .then(fileList => {
  115. console.log(fileList)
  116. // set the state that we have all the files
  117. this.setState({ files: fileList });
  118. })
  119. .catch(error => {
  120. console.error(error)
  121. });
  122.  
  123. }
  124.  
  125. onDragEnd(result) {
  126. // dropped outside the list
  127. if (!result.destination) {
  128. return;
  129. }
  130.  
  131. const files = reorder(
  132. this.state.files,
  133. result.source.index,
  134. result.destination.index
  135. );
  136.  
  137. this.setState({
  138. files
  139. });
  140. }
  141.  
  142.  
  143. removeItem = (index) => {
  144. this.setState(({ files }) => ({
  145. files: files.filter((_, ind) => ind !== index) }));
  146. }
  147. render() {
  148. return (
  149. <div className="downloadMusic">
  150. <div className="input">
  151. <input
  152. accept="audio/*"
  153. onChange={this.handleClick}
  154. id="upload-file"
  155. className="inputName"
  156. type="file"
  157. multiple
  158. ref={this.inputRef}
  159. />
  160. </div>
  161. <div>{this.state.files.length > 0 && <Downloaded/>}</div>
  162. <DragDropContext onDragEnd={this.onDragEnd}>
  163. <Droppable droppableId="droppable">
  164. {(provided, snapshot) => (
  165. <div
  166. {...provided.droppableProps}
  167. ref={provided.innerRef}
  168. style={getListStyle(snapshot.isDraggingOver)}
  169. >
  170. {this.state.files.map((file, index) => (
  171. <Draggable key={file.name} draggableId={file.name} index={index}>
  172. {(provided, snapshot) => (
  173. <div
  174. ref={provided.innerRef}
  175. {...provided.draggableProps}
  176.  
  177.  
  178. style={getItemStyle(
  179. snapshot.isDragging,
  180. provided.draggableProps.style
  181. )}
  182. >
  183. <div className="handle">
  184. <Handle {...provided.dragHandleProps}/>
  185. </div>
  186. <div>
  187. <form>
  188. <label>
  189. <p> &nbsp;<input type="text" defaultValue={file.name} name="name" style={{width:'600px', height:'30px', fontSize: '16px'}} /></p>
  190. </label>
  191. </form>
  192.  
  193. </div>
  194. <div className="delete">
  195. <LIDelete onClick = {() => this.removeItem(index)}/>
  196. </div>
  197. </div>
  198.  
  199. )}
  200. </Draggable>
  201.  
  202. ))}
  203. {provided.placeholder}
  204. </div>
  205. )}
  206. </Droppable>
  207. </DragDropContext>
  208. </div>
  209. );
  210. }
  211. }
  212.  
  213.  
  214. export default RBD;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement