Skip to content

Instantly share code, notes, and snippets.

@DDavis1025
Last active March 3, 2020 22:20
Show Gist options
  • Save DDavis1025/d69107bfd8b222c4807fbb84a43ed1fd to your computer and use it in GitHub Desktop.
Save DDavis1025/d69107bfd8b222c4807fbb84a43ed1fd to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import ReactDOM from "react-dom";
import styled from 'styled-components';
import { Delete } from 'styled-icons/typicons/Delete';
import Downloaded from './downloaded';
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
import { ButtonToggle } from "reactstrap";
import { Container, Row, Col } from 'reactstrap';
import axios from 'axios';
import auth0Client from "../Auth";
import { useAuth0 } from "../react-auth0-spa";
import {Auth0Context} from "../react-auth0-spa"
import { Auth0Provider } from "../react-auth0-spa";
import Profile from './Profile';
const reorder = (list, startIndex, endIndex) => {
const result = Array.from(list);
const [removed] = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
return result;
};
const grid = 3;
const getItemStyle = (isDragging, draggableStyle) => ({
userSelect: "none",
padding: grid * 2,
margin: `0 0 ${grid}px 0`,
background: isDragging ? "blue" : "white",
...draggableStyle
});
const getListStyle = isDraggingOver => ({
background: isDraggingOver ? "lightgrey" : "lightgrey",
padding: grid,
width: 700,
});
const Handle = styled.div`
width: 20px;
height: 20px;
margin-top: 7.5px;
background-color: gray;
border-radius: 4px;
float: left;
`
const LIDelete = styled(Delete)`
width: 20px;
height: 20px;
color: gray;
float: right;
margin-top: -50px;
`
let id = 0;
let clicked = false;
class AddAlbum extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.replaceClick = this.replaceClick.bind(this);
// this.save = this.save.bind(this);
this.onDragEnd = this.onDragEnd.bind(this);
this.secondInputClick = this.secondInputClick.bind(this);
this.clickFileInput = this.clickFileInput.bind(this);
this.replaceInput = this.replaceInput.bind(this);
this.clickAddFiles = this.clickAddFiles.bind(this);
this.handleChange = this.handleChange.bind(this);
this.downloadChange = this.downloadChange.bind(this);
// this.onSubmit = this.onSubmit.bind(this);
this.onClick = this.onClick.bind(this);
this.onChange = this.onChange.bind(this);
this.save2 = this.save2.bind(this);
// this.handleAuthentication = this.handleAuthentication.bind(this);
this.replaceInputRef = React.createRef();
this.fileRef = React.createRef();
this.inputRef = React.createRef();
this.addFiles = React.createRef();
this.resetFile = this.resetFile.bind(this);
this.removeItem = this.removeItem.bind(this)
this.state = {
beforeImageSave: null,
file: null,
files: [],
songsList: null,
albums: {title:"", date:"", description:""},
};
}
onChange(event) {
console.log(event.target.files[0])
this.setState({
beforeImageSave: URL.createObjectURL(event.target.files[0]),
file: event.target.files[0],
loaded: 0,
});
console.log(this.state.file)
}
resetFile(event) {
event.preventDefault();
this.setState({ file: null });
}
downloadChange (event) {
const { name, value } = event.target;
this.setState(prevState => ({
albums: {...prevState.albums, [name]: value}
}));
}
changeCursor = (e) => {
e.target.style.background = 'red';
}
handleClick = event => {
// Helper code to read file and return promise
const readFile = (file) => {
// const fileList = [];
const fileReader = new FileReader();
// create the promise and return it
return new Promise((resolve, reject) => {
// if file reader has an error, report it
fileReader.onerror = (error) => {
reject({ error })
}
// if success, resolve the promise
fileReader.onload = (e) => {
resolve({
name: file.name.replace( /_|\.mp3/gi, " "),
id: id++,
})
}
// start reading the file
fileReader.readAsArrayBuffer(file);
})
}
// create all the file reader promises
// create an array from the files list and use map to generate
// an array of promises
const allReaders = Array.from(event.target.files).map(readFile)
const other = event.target.files;
console.log(other);
// console.log(other);
var promise = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
if (1 === 1) {
resolve("Stuff worked!");
}
else {
reject(Error("It broke"));
}
});
promise.then(() => {this.setState({songsList : other})
}).then(()=> {
console.log(this.state.songList)
}).catch((error) => {
console.log(error);
})
// Now handle the array of promises we just created
Promise.all(allReaders)
.then(fileList => {
// set the state that we have all the files
this.setState({ files: fileList });
console.log(this.state.files)
})
.catch(error => {
console.error(error)
});
}
replaceClick = event => {
const readFile = (file) => {
// const fileList = [];
const fileReader = new FileReader();
// create the promise and return it
return new Promise((resolve, reject) => {
// if file reader has an error, report it
fileReader.onerror = (error) => {
reject({ error })
}
// if success, resolve the promise
fileReader.onload = (e) => {
resolve({
name: file.name.replace( /_|\.mp3/gi, " "),
id: id++,
})
}
// start reading the file
fileReader.readAsDataURL(file);
})
}
// create all the file reader promises
// create an array from the files list and use map to generate
// an array of promises
const allReaders = Array.from(event.target.files).map(readFile)
// Now handle the array of promises we just created
Promise.all(allReaders)
.then(fileList => {
console.log(this.state.files)
// set the state that we have all the files
this.setState({ files: fileList });
})
.catch(error => {
console.error(error)
});
clicked = true;
}
secondInputClick = event => {
// Helper code to read file and return promise
const readFile = (file) => {
// const fileList = [];
const fileReader = new FileReader();
// create the promise and return it
return new Promise((resolve, reject) => {
// if file reader has an error, report it
fileReader.onerror = (error) => {
reject({ error })
}
// if success, resolve the promise
fileReader.onload = (e) => {
resolve({
name: file.name.replace( /_|\.mp3/gi, " "),
id: id++,
})
}
// start reading the file
fileReader.readAsDataURL(file);
})
}
// create all the file reader promises
// create an array from the files list and use map to generate
// an array of promises
const allReaders = Array.from(event.target.files).map(readFile)
// Now handle the array of promises we just created
Promise.all(allReaders)
.then(fileList => {
console.log(this.state.files)
// set the state that we have all the files
this.setState(prevState => ({ ...prevState, files: [...prevState.files, ...fileList ]}));
})
.catch(error => {
console.error(error)
});
}
async save2(event) {
event.preventDefault();
let user_id = this.context.user.sub;
let image = this.state.file;
const data = new FormData()
data.append('file', this.state.file)
console.log(data);
await this.setState(prevState => ({
files: prevState.files.map((file, index) => ({...file, index})
)}));
let files = this.state.files;
console.log(this.state.files)
console.log(files);
await this.setState(prevState => ({
albums: {...prevState.albums, files, user_id}
}))
Array.from(this.state.songsList).forEach(song => {
data.append('songs', song)
})
data.append('albums', JSON.stringify(this.state.albums))
let albums = this.state.albums;
axios.post("http://localhost:3000/albums", data, {
}).then(res => { // then print response status
console.log(res.statusText)
})
}
onDragEnd(result) {
// dropped outside the list
if (!result.destination) {
return;
}
const files = reorder(
this.state.files,
result.source.index,
result.destination.index
);
this.setState({
files
});
}
removeItem = (index) => {
this.setState(({ files }) => ({
files: files.filter((_, ind) => ind !== index) }));
}
clickFileInput() {
this.inputRef.current.click();
}
replaceInput() {
this.replaceInputRef.current.click();
}
clickAddFiles () {
this.addFiles.current.click();
}
handleChange(index, event) {
let files = this.state.files.slice();
files[index].name = event.target.value;
this.setState({ files: files });
}
onClick(event) {
let albums = this.state.albums;
if(albums.date === "" || albums.title === "" || albums.description === "") {
alert("ERROR: Fields Must Be Filled In Before You Save");
} else {
// this.save(event);
// this.onSubmit(event);
this.save2(event);
// this.onClickHandler();
}
}
render() {
return (
<div className="downloadMusic">
<Container>
<Row>
<Col>
<div>
{filesExist != 0 ? (
<ButtonToggle onClick={this.replaceInput} color="secondary">Replace Music Files
</ButtonToggle>
) : (
<ButtonToggle onClick={this.clickFileInput} color="secondary">Upload Files
</ButtonToggle>
)}
</div>
<div className="input">
<input
accept="audio/*"
id="upload-file"
onChange={this.handleClick}
className="inputName"
type="file"
multiple
style={{display:"none"}}
ref={this.inputRef}
/>
<input
accept="audio/*"
id="upload-file"
onChange={this.replaceClick}
className="inputName"
type="file"
multiple
style={{display:"none"}}
ref={this.replaceInputRef}
/>
</div>
</Col>
</Row>
</Container>
<div>{this.state.files.length > 0 &&
<Downloaded
beforeImageSave={this.state.beforeImageSave}
file={this.state.file}
onChange={this.onChange}
onSubmit={this.onSubmit}
key={this.state.albums.id}
onClick={this.resetFile}
album={this.state.albums}
downloadChange={this.downloadChange}
/>
}</div>
<Container>
<Row>
<Col>
<DragDropContext onDragEnd={this.onDragEnd}>
<Droppable droppableId="droppable">
{(provided, snapshot) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
style={getListStyle(snapshot.isDraggingOver)}
>
{this.state.files.map((file, index) => (
<Draggable key={file.id.toString()} draggableId={file.id.toString()} index={index} style={{marginTop:"400px"}}>
{(provided, snapshot) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
style={getItemStyle(
snapshot.isDragging,
provided.draggableProps.style
)}
>
<div className="handle">
<Handle {...provided.dragHandleProps}/>
</div>
<div>
<form>
<label>
<p> &nbsp;<input type="text" value={file.name} onChange={this.handleChange.bind(this, index)} name="name" ref={this.fileRef} style={{width:'600px', height:'30px', fontSize: '16px'}} /></p>
</label>
</form>
</div>
<div className="delete">
<LIDelete onClick = {() => this.removeItem(index)}/>
</div>
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
</Col>
</Row>
</Container>
<Container>
<div className="belowList">{this.state.files.length > 0 && <div>
<Row>
<Col>
<ButtonToggle onClick={this.clickAddFiles} color="secondary">Add More Files
</ButtonToggle>
<input
accept="audio/*"
onChange={this.secondInputClick}
id="second-input"
className="inputName"
type="file"
multiple
style={{display:"none"}}
ref={this.addFiles}
/>
</Col>
</Row>
</div>
}</div>
<Row>
<Col>
<ButtonToggle onClick={this.onClick} color="success">Save
</ButtonToggle>
</Col>
</Row>
</Container>
</div>
);
}
}
AddAlbum.contextType = Auth0Context;
export default AddAlbum;
import React, { Component } from "react";
import ReactDOM from "react-dom";
import { Link } from 'react-router-dom';
import ImageUpload from './image-upload';
import AddAlbum from './add-album';
import { Container, Row, Col } from 'reactstrap';
import { Button, Form, FormGroup, Label, Input, FormText } from 'reactstrap';
class Downloaded extends Component {
constructor(props) {
super(props);
}
render() {
return(
<div className="main-content">
<Container>
<div className="container">
<div className="downloaded">
<Row xs="2" className="downloaded-row">
<Col xs="7" sm="10" md="6" lg="5" className="">
<div className="image"><ImageUpload file={this.props.file} beforeImageSave={this.props.beforeImageSave} onChange={this.props.onChange} onClick={this.props.onClick}/></div>
</Col>
<Col xs="6" sm="2" md="5" lg="5" className="">
<Form onSubmit={this.props.onSubmit} className="form">
<FormGroup row>
<Label for="title">Title:</Label>
<Input type="text" name="title" id="title" placeholder="Title" value={this.props.album.title} onChange={this.props.downloadChange}/>
</FormGroup>
<FormGroup row>
<Label for="date">Date:</Label>
<Input type="date" name="date" id="date" placeholder="Date" value={this.props.album.date.slice(0, 10)} onChange={this.props.downloadChange} required="required"/>
</FormGroup>
<FormGroup row>
<Label for="description">Description:</Label>
<Input type="textarea" name="description" id="description" placeholder="Description" value={this.props.album.description} onChange={this.props.downloadChange}/>
</FormGroup>
</Form>
</Col>
</Row>
</div>
</div>
</Container>
</div>
);
}
};
export default Downloaded;
import React from 'react';
import ReactDom from 'react-dom';
import { ButtonToggle } from "reactstrap";
class ImageUpload extends React.Component {
constructor(props) {
super(props);
this.clickAddImage = this.clickAddImage.bind(this);
this.imageRef = React.createRef();
}
clickAddImage () {
this.imageRef.current.click();
}
render() {
return (
<div>
<label>
<ButtonToggle onClick={this.clickAddImage} outline color="secondary">Add Image
</ButtonToggle>
<input type="file" style={{display:"none"}} accept="image/*" ref={this.imageRef} onChange={this.props.onChange} />
</label>
<div className="image" style={{ width: '300px', height: '300px',
border: '1px dotted black'}}>
{this.props.file && (
<img style={{ width: "300px", height: "300px"}} src={this.props.file[0] ? "http://localhost:3000/" + this.props.file[0].path : this.props.beforeImageSave} />
)}
</div>
{this.props.file && this.props.file.length !== 0 && (
<div>
<ButtonToggle onClick={this.props.onClick} color="danger" size="sm">Remove Image
</ButtonToggle>
</div>
)}
</div>
);
}
}
export default ImageUpload;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment