Skip to content

Commit

Permalink
[Users] Created edit page, close #29
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovert Lota Palonpon committed Apr 4, 2019
1 parent d070b82 commit 48188d1
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 171 deletions.
24 changes: 23 additions & 1 deletion app/Http/Controllers/Api/V1/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function store(Request $request) : JsonResponse

'type' => 'required_if:step,1|in:superuser,user',
'email' => 'required_if:step,1|email|unique:users,email,NULL,id,deleted_at,NULL',
'username' => 'nullable|unique:users'
'username' => 'nullable|unique:users,username,NULL,id,deleted_at,NULL'
]);

// Return here if the user is just in the first step.
Expand Down Expand Up @@ -93,6 +93,28 @@ public function show(Request $request, User $user) : JsonResponse
*/
public function update(Request $request, User $user) : JsonResponse
{
$request->validate([
'firstname' => 'required_if:step,0|string|max:255',
'lastname' => 'required_if:step,0|string|max:255',

'gender' => 'nullable|in:female,male',
'birthdate' =>
'nullable|date:Y-m-d|before:'.now()->subYear(10)->format('Y-m-d'),
'address' => 'nullable|string|max:510',

'type' => 'required_if:step,1|in:superuser,user',
'email' =>
"required_if:step,1|email|unique:users,email,{$user->id},id,deleted_at,NULL",
'username' =>
"nullable|unique:users,username,{$user->id},id,deleted_at,NULL"
]);

$attributes = $request->all();
unset($attributes['step']);

$user->fill($attributes);
$user->update();

return response()->json($user);
}

Expand Down
35 changes: 35 additions & 0 deletions resources/js/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ export default class User {
return response.data;
}

/**
* Show a user.
*
* @param {number} id
*
* @return {object}
*/
static async show(id) {
const response = await axios.get(`/api/v1/users/${id}`);

if (response.status !== 200) {
return {};
}

return response.data;
}

/**
* Update a user.
*
* @param {number} id
* @param {object} attributes
*
* @return {object}
*/
static async update(id, attributes) {
const response = await axios.patch(`/api/v1/users/${id}`, attributes);

if (response.status !== 200) {
return {};
}

return response.data;
}

/**
* Delete a user.
*
Expand Down
12 changes: 1 addition & 11 deletions resources/js/views/__backoffice/users/Create.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Create extends Component {
activeStep: 0,
formValues: [],
user: {},
errors: {},
message: {},
};

Expand Down Expand Up @@ -115,14 +114,7 @@ class Create extends Component {

render() {
const { classes, ...other } = this.props;
const {
loading,
activeStep,
formValues,
user,
errors,
message,
} = this.state;
const { loading, activeStep, formValues, user, message } = this.state;

const steps = ['Profile', 'Account', 'Avatar'];

Expand All @@ -144,7 +136,6 @@ class Create extends Component {
values={
formValues[0] ? formValues[0] : defaultValues
}
errors={errors}
handleSubmit={this.handleSubmit}
/>
);
Expand All @@ -159,7 +150,6 @@ class Create extends Component {
email: '',
username: '',
}}
errors={errors}
handleSubmit={this.handleSubmit}
handleBack={this.handleBack}
/>
Expand Down
Loading

0 comments on commit 48188d1

Please sign in to comment.