Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Allow rename before image upload in EditPage, refactor ImageModal #117

Merged
merged 2 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions src/components/ImageSettingsModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class ImageSettingsModal extends Component {
}

renameImage = async () => {
const { match, image, isPendingUpload } = this.props;
const { match, image, isPendingUpload, toReload, onClose } = this.props;
const { siteName } = match.params;
const {
newFileName,
Expand Down Expand Up @@ -70,8 +70,13 @@ export default class ImageSettingsModal extends Component {
withCredentials: true,
});
}

// reload after action
window.location.reload();
if (toReload) {
window.location.reload();
} else {
onClose();
}
}

deleteImage = async () => {
Expand Down Expand Up @@ -137,20 +142,32 @@ export default class ImageSettingsModal extends Component {
/>
</div>
<div className={elementStyles.modalButtons}>
<LoadingButton
label="Save"
disabled={!!errorMessage}
disabledStyle={elementStyles.disabled}
className={(errorMessage || !sha) ? elementStyles.disabled : elementStyles.blue}
callback={this.renameImage}
/>
<LoadingButton
label="Delete"
disabled={!sha}
disabledStyle={elementStyles.disabled}
className={sha ? elementStyles.warning : elementStyles.disabled}
callback={this.deleteImage}
/>
{isPendingUpload
? (
<LoadingButton
label="Save"
disabledStyle={elementStyles.disabled}
className={elementStyles.blue}
callback={this.renameImage}
/>
) : (
<>
<LoadingButton
label="Save"
disabled={(errorMessage || !sha)}
disabledStyle={elementStyles.disabled}
className={(errorMessage || !sha) ? elementStyles.disabled : elementStyles.blue}
callback={this.renameImage}
/>
<LoadingButton
label="Delete"
disabled={!sha}
disabledStyle={elementStyles.disabled}
className={sha ? elementStyles.warning : elementStyles.disabled}
callback={this.deleteImage}
/>
</>
)}
</div>
</form>
</div>
Expand All @@ -172,4 +189,9 @@ ImageSettingsModal.propTypes = {
}).isRequired,
isPendingUpload: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
toReload: PropTypes.bool,
};

ImageSettingsModal.defaultProps = {
toReload: true,
}
74 changes: 5 additions & 69 deletions src/components/ImagesModal.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, { PureComponent } from 'react';
import axios from 'axios';
import PropTypes from 'prop-types';
import mediaStyles from '../styles/isomer-cms/pages/Media.module.scss';
Expand All @@ -25,72 +25,9 @@ export const ImageCard = ({ image, siteName, onClick }) => (
);


export default class ImagesModal extends Component {
constructor(props) {
super(props);
this.state = {
images: [],
};
}

async componentDidMount() {
const { siteName } = this.props;
try {
this.getImage(siteName);
} catch (e) {
console.log(e);
}
}

getImage = async (siteName) => {
const { data: { images } } = await axios.get(`${process.env.REACT_APP_BACKEND_URL}/sites/${siteName}/images`, {
withCredentials: true,
});
this.setState({ images });
}

uploadImage = async (imageName, imageContent) => {
try {
const { siteName } = this.props;
const params = {
imageName,
content: imageContent,
};

// add a loading screen while file is being uploaded
await axios.post(`${process.env.REACT_APP_BACKEND_URL}/sites/${siteName}/images`, params, {
withCredentials: true,
});

// trigger a re-render of the modal
const { data: { images } } = await axios.get(`${process.env.REACT_APP_BACKEND_URL}/sites/${siteName}/images`, {
withCredentials: true,
});
this.setState({ images });
} catch (err) {
console.log(err);
}
}

onImageSelect = async (event) => {
const imgReader = new FileReader();
const imgName = event.target.files[0].name;
imgReader.onload = (() => {
/** Github only requires the content of the image
* imgReader returns `data:image/png;base64, {fileContent}`
* hence the split
*/

const imgData = imgReader.result.split(',')[1];

this.uploadImage(imgName, imgData);
});
imgReader.readAsDataURL(event.target.files[0]);
}

export default class ImagesModal extends PureComponent {
render() {
const { siteName, onClose, onImageSelect } = this.props;
const { images } = this.state;
const { siteName, images, onClose, onImageSelect, readImageToUpload } = this.props;
return (!!images.length
&& (
<div className={elementStyles.overlay}>
Expand All @@ -107,7 +44,7 @@ export default class ImagesModal extends Component {
onClick={() => document.getElementById('file-upload').click()}
/>
<input
onChange={this.onImageSelect}
onChange={readImageToUpload}
type="file"
id="file-upload"
accept="image/png, image/jpeg, image/gif"
Expand All @@ -125,10 +62,9 @@ export default class ImagesModal extends Component {
</div>
</div>
</div>

)
);
}
};
}

ImageCard.propTypes = {
Expand Down
66 changes: 64 additions & 2 deletions src/layouts/EditPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import marked from 'marked';
import { Base64 } from 'js-base64';
import SimplePage from '../templates/SimplePage';
import ImagesModal from '../components/ImagesModal';
import ImageSettingsModal from '../components/ImageSettingsModal';
import {
frontMatterParser, concatFrontMatterMdBody, prependImageSrc, prettifyPageFileName,
} from '../utils';
Expand Down Expand Up @@ -37,7 +38,9 @@ export default class EditPage extends Component {
sha: null,
editorValue: '',
frontMatter: '',
images: [],
isSelectingImage: false,
pendingImageUpload: null
};
this.mdeRef = React.createRef();
}
Expand All @@ -50,6 +53,7 @@ export default class EditPage extends Component {
withCredentials: true,
});
const { content, sha } = resp.data;

// split the markdown into front matter and content
const { frontMatter, mdBody } = frontMatterParser(Base64.decode(content));
this.setState({
Expand All @@ -62,6 +66,13 @@ export default class EditPage extends Component {
}
}

getImages = async (siteName) => {
const { data: { images } } = await axios.get(`${process.env.REACT_APP_BACKEND_URL}/sites/${siteName}/images`, {
withCredentials: true,
});
this.setState({ images });
}

updatePage = async () => {
try {
const { match } = this.props;
Expand Down Expand Up @@ -126,10 +137,40 @@ export default class EditPage extends Component {
});
}

uploadImage = async (imageName, imageContent) => {
try {
// toggle state so that image renaming modal appears
this.setState({
pendingImageUpload: {
fileName: imageName,
path: `images%2F${imageName}`,
content: imageContent,
},
});
} catch (err) {
console.log(err);
}
}

readImageToUpload = async (event) => {
const imgReader = new FileReader();
const imgName = event.target.files[0].name;
imgReader.onload = (() => {
/** Github only requires the content of the image
* imgReader returns `data:image/png;base64, {fileContent}`
* hence the split
*/

const imgData = imgReader.result.split(',')[1];
this.uploadImage(imgName, imgData);
});
imgReader.readAsDataURL(event.target.files[0]);
}

render() {
const { match } = this.props;
const { siteName, fileName } = match.params;
const { editorValue, isSelectingImage } = this.state;
const { editorValue, images, isSelectingImage, pendingImageUpload } = this.state;
return (
<>
<Header
Expand All @@ -142,7 +183,9 @@ export default class EditPage extends Component {
<ImagesModal
siteName={siteName}
onClose={() => this.setState({ isSelectingImage: false })}
images={images}
onImageSelect={this.onImageClick}
readImageToUpload={this.readImageToUpload}
/>
)}
<div className={editorStyles.pageEditorSidebar}>
Expand All @@ -165,7 +208,10 @@ export default class EditPage extends Component {
'|',
{
name: 'image',
action: () => this.setState({ isSelectingImage: true }),
action: async () => {
await this.getImages(siteName);
this.setState({ isSelectingImage: true })
},
className: 'fa fa-picture-o',
title: 'Insert Image',
default: true,
Expand Down Expand Up @@ -195,6 +241,22 @@ export default class EditPage extends Component {
callback={this.deletePage}
/>
</div>
{
pendingImageUpload
&& (
<ImageSettingsModal
image={pendingImageUpload}
match={match}
// eslint-disable-next-line react/jsx-boolean-value
isPendingUpload={true}
onClose={() => {
this.getImages(siteName);
this.setState({ pendingImageUpload: null });
}}
toReload={false}
/>
)
}
</>
);
}
Expand Down