Skip to content

Commit e7fd7c4

Browse files
author
Jovert Lota Palonpon
committed
wip
1 parent 62b3495 commit e7fd7c4

File tree

4 files changed

+71
-39
lines changed

4 files changed

+71
-39
lines changed

app/Http/Controllers/Api/V1/UsersController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers\Api\V1;
44

55
use App\User;
6+
use Illuminate\Http\UploadedFile;
67
use App\Http\Controllers\Controller;
78
use Illuminate\Http\JsonResponse;
89
use Illuminate\Http\Request;
@@ -135,9 +136,9 @@ public function restore(Request $request, $id)
135136
*
136137
* @return Illuminate\Http\JsonResponse
137138
*/
138-
public function storeAvatar(Request $request, User $user) : JsonResponse
139+
public function storeAvatar(Request $request, User $user)
139140
{
140-
return response()->json($user);
141+
return response()->json($request->input('avatar'));
141142
}
142143

143144
/**

resources/js/ui/Dropzone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function Dropzone(props) {
233233
accept: acceptedFileTypes.join(','),
234234
maxSize: maxFileSize * 1000 * 1000,
235235
onDrop: (acceptedFiles, rejectedFiles) => {
236-
acceptedFiles = acceptedFiles.map(file =>
236+
acceptedFiles = acceptedFiles.map((file, key) =>
237237
Object.assign(file, {
238238
url: URL.createObjectURL(file),
239239
status: 'queued',

resources/js/views/__backoffice/users/Create.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class Create extends Component {
131131

132132
return (
133133
<Profile
134+
{...other}
134135
values={
135136
formValues[0] ? formValues[0] : defaultValues
136137
}
@@ -143,6 +144,7 @@ class Create extends Component {
143144
case 1:
144145
return (
145146
<Account
147+
{...other}
146148
values={{
147149
type: '',
148150
email: '',
@@ -158,6 +160,7 @@ class Create extends Component {
158160
case 2:
159161
return (
160162
<Avatar
163+
{...other}
161164
values={{}}
162165
errors={errors}
163166
handleSubmit={this.handleSubmit}

resources/js/views/__backoffice/users/Forms/Avatar.js

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,80 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33

44
import { Button, Grid, Typography, withStyles } from '@material-ui/core';
55

66
import { Dropzone } from '../../../../ui';
77

8-
const Avatar = props => {
9-
const { classes, values, errors, handleSubmit, handleSkip } = props;
8+
class Avatar extends Component {
9+
/**
10+
* Handle the file upload.
11+
*
12+
* @param {object} file The file that should be fed to the API.
13+
* @param {function} done When called, will inform that upload is done.
14+
*
15+
* @return {undefined}
16+
*/
17+
handleUpload = async (file, done) => {
18+
const { pageProps } = this.props;
19+
const { user } = pageProps;
1020

11-
return (
12-
<>
13-
<Typography variant="h6" gutterBottom>
14-
Avatar Upload
15-
</Typography>
21+
try {
22+
const formData = new FormData();
23+
formData.append('avatar', file);
1624

17-
<Dropzone
18-
maxFiles={2}
19-
maxFileSize={2}
20-
handleUpload={(file, done) => {
21-
setTimeout(() => {
22-
done();
23-
}, Math.floor(Math.random() * Math.floor(15)) * 1000);
24-
}}
25-
handleFileRemoved={removed => {
26-
setTimeout(() => {
27-
removed();
28-
}, Math.floor(Math.random() * Math.floor(10)) * 1000);
29-
}}
30-
/>
25+
const response = await fetch(`api/v1/users/${user.id}/avatar`, {
26+
method: 'POST',
27+
headers: {
28+
Authorization:
29+
axios.defaults.headers.common['Authorization'],
30+
'X-CSRF-TOKEN':
31+
axios.defaults.headers.common['X-CSRF-TOKEN'],
32+
},
33+
body: formData,
34+
});
35+
} catch (error) {}
36+
};
3137

32-
<div className={classes.sectionSpacer} />
38+
render() {
39+
const { classes, handleSkip } = this.props;
3340

34-
<Grid container spacing={24} justify="flex-end">
35-
<Grid item>
36-
<Button
37-
variant="contained"
38-
color="primary"
39-
onClick={handleSkip}
40-
>
41-
Skip
42-
</Button>
41+
return (
42+
<>
43+
<Typography variant="h6" gutterBottom>
44+
Avatar Upload
45+
</Typography>
46+
47+
<Dropzone
48+
maxFiles={2}
49+
maxFileSize={2}
50+
handleUpload={this.handleUpload}
51+
handleFileRemoved={removed => {
52+
setTimeout(() => {
53+
removed();
54+
}, Math.floor(Math.random() * Math.floor(10)) * 1000);
55+
}}
56+
/>
57+
58+
<div className={classes.sectionSpacer} />
59+
60+
<Grid container spacing={24} justify="flex-end">
61+
<Grid item>
62+
<Button
63+
variant="contained"
64+
color="primary"
65+
onClick={handleSkip}
66+
>
67+
Skip
68+
</Button>
69+
</Grid>
4370
</Grid>
44-
</Grid>
45-
</>
46-
);
47-
};
71+
</>
72+
);
73+
}
74+
}
4875

4976
Avatar.propTypes = {
77+
classes: PropTypes.object.isRequired,
5078
values: PropTypes.object.isRequired,
5179
errors: PropTypes.object,
5280
handleSubmit: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)