Skip to content

Instantly share code, notes, and snippets.

@dmayo2
dmayo2 / sp-awesome-18
Created March 9, 2018 04:22
Code for Making Dumb Components Smart: Refactoring
// --methods above here --
render(){
let { _id, fileName, createdAt, src, deleteDate, } = this.props;
return(
<tr key={_id}>
<td><img src={src}{fileName} height=50" /> {fileName}</td>
<td>{src}</td>
<td>{createdAt}</td>
<td>{deleteDate}
@dmayo2
dmayo2 / sp-awesome-17
Created March 9, 2018 04:22
Code for Making Dumb Components Smart: Refactoring
handleRemove(imageId){
if ( confirm('Are you sure? This is permanent!') ) {
// send `imageId` to database to be deleted. Framework dependent
}
}
changeFontAwesome() {
this.setState({ editDate: !this.state.editDate });
}
@dmayo2
dmayo2 / sp-awesome-16
Created March 9, 2018 04:21
Code for Making Dumb Components Smart: Refactoring
class CustomRow extends React.Component {
constructor(props) {
super(props);
this.state = {
startDate: moment(props.deleteDate),
editDate: true,
}
this.handleDateChange = this.handleDateChange.bind(this);
this.handleRemove = this.handleRemove.bind(this);
this.changeFontAwesome = this.changeFontAwesome.bind(this);
@dmayo2
dmayo2 / sp-awesome-15
Created March 9, 2018 04:20
Code for Making Dumb Components Smart: Refactoring
Class Images extends React.Component {
constructor(props) {
super(props);
}
render(){
// deconstruct props to get access to individual params
let { loading, images, } = this.props;
return(
@dmayo2
dmayo2 / sp-awesome-14
Last active March 13, 2018 17:00
Code for Making Dumb Components Smart: Refactoring
const handleDelete = (imageId) => {
if (confirm('Are you sure? This is permanent!') ) {
// call to database to delete and catch any errors
try {
images: ImagesCollection.remove(imageId);
} catch (exception) {
throw new Error('500', exception);
}
}
}
@dmayo2
dmayo2 / sp-awesome-13
Created March 9, 2018 04:18
Code for Making Dumb Components Smart: Refactoring
{images.map( ({_id, fileName, size, src, }) => (
<tr key={_id}>
<td><img src={src}{fileName} /></td>
<td>{fileName}</td>
<td>{size}</td>
<td><Button onClick={() => handleDelete(_id)} > Delete </Button></td>
</tr>
) }
@dmayo2
dmayo2 / sp-awesome-12
Last active March 20, 2018 15:07
Code for Making Dumb Components Smart: Refactoring
{images.map( ({_id, fileName, size, src, }) => {
counter++;
(<tr key={_id}>
<td>{counter}.</td>
<td><img src={src}{fileName} /></td>
<td>{fileName}</td>
<td>{size}</td>
</tr>
) }
}
@dmayo2
dmayo2 / sp-awesome-11
Last active March 20, 2018 15:07
Code for Making Dumb Components Smart: Refactoring
{images.map( ({_id, fileName, size, src, }) => ()
// HTML/JSX here
)};
@dmayo2
dmayo2 / sp-awesome-10
Created March 9, 2018 04:14
Code for Making Dumb Components Smart: Refactoring
// Old ES5 syntax
if( loading === false ) {
return (
// HTML/JSX output here with database contents
)
} else {
return <Loading />
}
@dmayo2
dmayo2 / sp-awesome-09
Created March 9, 2018 04:13
Code for Making Dumb Components Smart: Refactoring
!loading ? (