Skip to content

Commit

Permalink
wip - Dropzone Component #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovert Lota Palonpon committed Apr 2, 2019
1 parent 60528e2 commit 32fb16d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/V1/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function restore(Request $request, $id)
*/
public function storeAvatar(Request $request, User $user) : JsonResponse
{
return response()->json();
return response()->json($user);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Traits/UploadsFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Traits;

use Uploader;
use App\Utils\Uploader;
use Illuminate\Http\UploadedFile;

trait UploadsFiles
Expand Down
84 changes: 62 additions & 22 deletions resources/js/ui/Dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@ import {
import {
Block as BlockIcon,
CheckCircle as CheckCircleIcon,
Queue as QueueIcon,
} from '@material-ui/icons';

import * as StringUtils from '../utils/String';
import { LinearDeterminate } from './Loaders';

const getFileStatusClass = status => {
switch (status) {
case 'queued':
return 'fileQueued';
break;

case 'uploaded':
return 'fileSuccess';
return 'fileUploaded';
break;

case 'rejected':
return 'fileError';
return 'fileRejected';
break;

default:
return 'filePrimary';
return 'fileUploading';
break;
}
};
Expand All @@ -42,12 +47,21 @@ let FileIcon = props => {
const { classes, status } = props;

switch (status) {
case 'queued':
return <QueueIcon className={classes.queued} fontSize="large" />;
break;

case 'uploaded':
return <CheckCircleIcon color="primary" fontSize="large" />;
return (
<CheckCircleIcon
className={classes.uploaded}
fontSize="large"
/>
);
break;

case 'rejected':
return <BlockIcon className={classes.error} fontSize="large" />;
return <BlockIcon className={classes.rejected} fontSize="large" />;
break;

default:
Expand All @@ -60,15 +74,20 @@ let FileIcon = props => {

FileIcon.propTypes = {
classes: PropTypes.object.isRequired,
status: PropTypes.oneOf(['uploading', 'uploaded', 'rejected']).isRequired,
status: PropTypes.oneOf(['queued', 'uploading', 'uploaded', 'rejected'])
.isRequired,
};

FileIcon = withStyles(theme => ({
success: {
queued: {
color: theme.palette.grey[500],
},

uploaded: {
color: colors.green[600],
},

error: {
rejected: {
color: theme.palette.error.light,
},

Expand All @@ -82,11 +101,17 @@ FileIcon = withStyles(theme => ({
}))(FileIcon);

function Dropzone(props) {
const { classes, acceptedFileTypes, maxFiles, maxFileSize } = props;
const {
classes,
acceptedFileTypes,
maxFiles,
maxFileSize,
handleUpload,
} = props;

const [files, setFiles] = useState([]);

const getFileErrorMessage = file => {
const getfileRejectedMessage = file => {
let errors = [];

if (
Expand Down Expand Up @@ -116,7 +141,8 @@ function Dropzone(props) {
acceptedFiles = acceptedFiles.map(file =>
Object.assign(file, {
url: URL.createObjectURL(file),
status: 'uploading',
status: 'queued',
message: 'File is on the waiting queue.',
}),
);

Expand All @@ -126,11 +152,21 @@ function Dropzone(props) {
status: 'rejected',
message: file.hasOwnProperty('message')
? file.message
: getFileErrorMessage(file),
: getfileRejectedMessage(file),
}),
);

setFiles(files.concat(acceptedFiles, rejectedFiles));
setFiles(
files.concat(acceptedFiles, rejectedFiles).map((file, key) => {
if (key === 0 && file.status === 'queued') {
file.status = 'uploading';

return file;
}

return file;
}),
);
},
noClick: true,
noKeyboard: true,
Expand All @@ -143,9 +179,9 @@ function Dropzone(props) {
files.forEach(file => {
// Make sure to revoke the data uris to avoid memory leaks.
URL.revokeObjectURL(file.preview);

// Process uploading here.
});

// If there aren't any one being uploaded, pick one from the queue.
},
[files],
);
Expand Down Expand Up @@ -321,9 +357,9 @@ function Dropzone(props) {

Dropzone.propTypes = {
acceptedFileTypes: PropTypes.array,
onDrop: PropTypes.func,
maxFiles: PropTypes.number,
maxFileSize: PropTypes.number,
handleUpload: PropTypes.func.isRequired,
};

Dropzone.defaultProps = {
Expand Down Expand Up @@ -401,16 +437,20 @@ const styles = theme => ({
},
},

fileSuccess: {
border: `2px solid ${colors.green[600]}`,
fileQueued: {
border: `2px solid ${theme.palette.grey[500]}`,
},

fileError: {
border: `2px solid ${theme.palette.error.light}`,
fileUploading: {
border: `2px solid ${theme.palette.primary.main}`,
},

filePrimary: {
border: `2px solid ${theme.palette.primary.main}`,
fileUploaded: {
border: `2px solid ${colors.green[600]}`,
},

fileRejected: {
border: `2px solid ${theme.palette.error.light}`,
},

fileAdd: {
Expand Down
6 changes: 5 additions & 1 deletion resources/js/views/__backoffice/users/Forms/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const Avatar = props => {
Avatar Upload
</Typography>

<Dropzone maxFiles={2} maxFileSize={2} />
<Dropzone
maxFiles={2}
maxFileSize={2}
handleUpload={file => console.log(file)}
/>

<div className={classes.sectionSpacer} />

Expand Down

0 comments on commit 32fb16d

Please sign in to comment.