Skip to content

Commit

Permalink
Merge pull request #26 from jade-creator/profile-component
Browse files Browse the repository at this point in the history
profile-component
  • Loading branch information
jade-creator committed Jun 14, 2021
2 parents fe22dda + fd8edc2 commit b24fe4a
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 69 deletions.
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class Kernel extends HttpKernel
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'checkProfile' => \App\Http\Middleware\CheckProfile::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'hasProfile' => \App\Http\Middleware\HasProfile::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'role' => \App\Http\Middleware\RoleMiddleware::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function create(){
]);
$this->profile->user_id = $this->user_id;
$this->profile->save();
// $this->emit('saved');
return redirect('profile/view/' . $this->user_id);

return redirect()->route('profile.view', ['role' => Auth::user()->role->name, 'user_id' => Auth::user()->id]);
}
}
26 changes: 14 additions & 12 deletions app/Http/Livewire/ProfileComponent/ProfileEditFormComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Http\Livewire\ProfileComponent;

use App\Models\Patient;
use App\Models\Role;
use App\Models\User;
use App\Models\Profile;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class ProfileEditFormComponent extends Component
Expand All @@ -20,23 +22,17 @@ class ProfileEditFormComponent extends Component
public $status;
public $note;

public function mount(string $role, int $user_id)
public function mount()
{
Role::where('name', $role)->firstOrFail();

$this->user = User::with(['profile', $role])
->findOrFail($user_id);
Role::where('name', $this->role)->firstOrFail();

$this->user = User::with('profile')->findOrFail($this->user_id);
$this->profile = $this->user->profile;
$this->subEntity = $this->user->$role;

if (is_null($this->subEntity)) {
abort(404);
}

if($role == 'patient'){
if($this->role == 'patient'){
$this->subEntity = Auth::user()->patient;
$this->status = $this->subEntity->isActive ? 'Active' : 'Inactive';
$this->note = $this->subEntity->note; //need update method
$this->note = $this->subEntity->note;
}
}

Expand All @@ -61,5 +57,11 @@ public function update(){
$this->validate();
$this->user->update();
$this->profile->update();

if($this->role == 'patient'){
Patient::where('user_id', $this->user_id)->update([
'note' => $this->note
]);
}
}
}
30 changes: 30 additions & 0 deletions app/Http/Middleware/CheckProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CheckProfile
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if(!Auth::check()){
return redirect('/login');
}

if(Auth::user()->profile) {
return redirect()->route('profile.view', ['role' => Auth::user()->role->name, 'user_id' => Auth::user()->id]);
}

return $next($request);
}
}
31 changes: 31 additions & 0 deletions app/Http/Middleware/HasProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HasProfile
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if(!Auth::check()){
return redirect('/login');
}

if(!Auth::user()->profile) {
return redirect()->route('profile.add')
->with('warning','Please complete your profile to proceed. Thank you!');
}

return $next($request);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<div class="">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<div class="px-3 px-sm-4">
<div class="pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Personal Details</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group mr-2">
<button type="button" class="btn btn-sm btn-outline-secondary">Share</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Export</button>
</div>
<button type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle">
<svg xmlns="http:https://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>
This week
</button>
</div>
</div>

@include('partials.alerts')

<form method="post" wire:submit.prevent="create">
<div class="form-row">
<div class="form-group col">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
<div class="">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<div class="px-3 px-sm-4">
<div class="pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Profile</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group mr-2">
<button type="button" class="btn btn-sm btn-outline-secondary">Share</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Export</button>
</div>
<button type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle">
<svg xmlns="http:https://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>
This week
</button>
</div>
</div>
<form method="post" wire:submit.prevent="update">
@if ($role == 'patient')
<div class="form-row">
<div class="form-group col">
<label for="id">ID</label>
<input class="form-control" id="id" name="id" type="text" readonly placeholder="{{ $this->subEntity->id ?? 'Not Registered' }}">
<label for="name">Name</label>
<input type="text" class="form-control @error('user.name') is-invalid @enderror" id="name" name="name" required autofocus wire:model.defer="user.name">
@error('user.name')
<div class="invalid-feedback">
{{$message}}
</div>
@enderror
</div>
<div class="form-group col">
<label for="status">Status</label>
<input class="form-control" id="status" name="status" type="text" readonly wire:model.defer="status">
</div>
@if ($role == 'patient')
<div class="form-group col">
<label for="status">Status</label>
<input class="form-control" id="status" name="status" type="text" readonly wire:model.defer="status">
</div>
@else
<div class="form-group col">
<label for="name">Name</label>
<input type="text" class="form-control @error('user.name') is-invalid @enderror" id="name" name="name" required autofocus wire:model.defer="user.name">
@error('user.name')
<div class="invalid-feedback">
{{$message}}
</div>
@enderror
</div>
@endif
</div>
@if ($role == 'patient')
@else
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control @error('user.name') is-invalid @enderror" id="name" name="name" required autofocus wire:model.defer="user.name">
Expand All @@ -46,6 +30,7 @@
@enderror
</div>
@endif

<div class="form-row">
<div class="form-group col">
<label for="birthdate">Birthdate</label>
Expand Down Expand Up @@ -102,9 +87,7 @@
<div class="form-group">
<label for="note">Note</label>
<textarea class="form-control" id="note" name="note" required autofocus wire:model.defer="note" wire:loading.attr="disabled"
@if (is_null($this->note))
placeholder="Nothing to see here..."
@endif>
placeholder="...">
</textarea>
</div>
@endif
Expand Down
9 changes: 9 additions & 0 deletions resources/views/partials/alerts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@
<span aria-hidden="true">&times;</span>
</button>
</div>
@endif

@if (session('warning'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
{!! session('warning') !!}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
@endif
18 changes: 9 additions & 9 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@

Route::middleware('auth')->group( function() {
//PROFILE
Route::get('/profile', ProfileComponent\ProfileAddFormComponent::class)->name('profile.add');
Route::get('/profile/{role}/{user_id}', ProfileComponent\ProfileEditFormComponent::class)->name('profile.view');
Route::get('/profile', ProfileComponent\ProfileAddFormComponent::class)->middleware('checkProfile')->name('profile.add');
Route::get('/profile/{role}/{user_id}', ProfileComponent\ProfileEditFormComponent::class)->middleware('hasProfile')->name('profile.view');

//ADMIN, DOCTOR.
Route::middleware('role:admin|doctor|receptionist')->group( function() {
//ADMIN, DOCTOR, RECEPTIONISTS
Route::middleware(['role:admin|doctor|receptionist', 'hasProfile'])->group( function() {
//DASHBOARD
Route::get('/dashboard', DashboardComponent::class)->name('dashboard.view');

Expand Down Expand Up @@ -109,20 +109,20 @@
});

//ADMIN
Route::get('/prescriptions/add', PrescriptionComponent\PrescriptionAddFormComponent::class)->middleware('role:admin')->name('prescriptions.add');
Route::get('/prescriptions/add', PrescriptionComponent\PrescriptionAddFormComponent::class)->middleware(['role:admin', 'hasProfile'])->name('prescriptions.add');

//ADMIN, DOCTOR
Route::middleware('role:admin|doctor')->group( function() {
Route::middleware(['role:admin|doctor', 'hasProfile'])->group( function() {
//PRESCRIPTIONS
Route::get('/prescriptions/edit/{prescription}', PrescriptionComponent\PrescriptionEditFormComponent::class)->name('prescriptions.edit');
Route::get('/prescriptions/restore/{prescription_id}', [PrescriptionComponent\PrescriptionViewComponent::class, 'restore'])->name('prescriptions.restore');
});

//ADMIN, RECEPTIONIST
Route::get('/appointments/add', AppointmentComponent\AppointmentAddFormComponent::class)->middleware('role:admin|receptionist')->name('appointments.add');
Route::get('/appointments/add', AppointmentComponent\AppointmentAddFormComponent::class)->middleware(['role:admin|receptionist', 'hasProfile'])->name('appointments.add');

//DOCTOR
Route::middleware('role:doctor')->group( function() {
Route::middleware(['role:doctor', 'hasProfile'])->group( function() {
//DOCUMENTS
Route::get('/doctor/appointments/add', Doctor\AppointmentComponent\DoctorAppointmentAddFormComponent::class)->name('doctor.appointments.add');

Expand All @@ -131,7 +131,7 @@
});

//PATIENT
Route::middleware('role:patient')->group( function() {
Route::middleware(['role:patient', 'hasProfile'])->group( function() {
//APPOINTMENTS
Route::get('/patient/appointments/add', Patient\AppointmentComponent\PatientAppointmentAddFormComponent::class)->name('patient.appointments.add');
Route::get('/patient/appointments', Patient\AppointmentComponent\PatientAppointmentViewComponent::class)->name('patient.appointments.view');
Expand Down

0 comments on commit b24fe4a

Please sign in to comment.