Skip to content

Commit

Permalink
Infinite iterative folder/document creation controllers/models defined
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Nov 9, 2020
1 parent 28c0a34 commit 003ed30
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 96 deletions.
29 changes: 0 additions & 29 deletions server/app/Http/Controllers/DocumentController.php

This file was deleted.

45 changes: 39 additions & 6 deletions server/app/Http/Livewire/Document/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Livewire\Component;
use App\Models\Document;
use App\Models\Folder;

class Create extends Component
{
public $isCreating = false;
public $name = "";
public $message = "";
public $emitTo;
public $folderId;

public function confirmCreating()
{
Expand All @@ -19,21 +22,51 @@ public function confirmCreating()
public function createNewDocument()
{
try {
$document = Document::create([
'name' => $this->name
]);
if($this->folderId == null)
{
$document = Document::create([
'name' => $this->name,
]);
} else {
$document = Document::create([
'name' => $this->name,
'folder_id' => (int)$this->folderId
]);
}

$document->user()->attach(auth()->user());
} catch (\Throwable $e) {
$this->emitTo('dashboard', 'flashMessage', "Oluşturma işlemi başarısız oldu!");
$this->emitTo($this->emitTo, 'flashMessage', "Oluşturma işlemi başarısız oldu!");
}
$this->emitTo('dashboard', 'flashMessage', $this->name . " isimli doküman başarıyla eklendi!");
$this->emitTo($this->emitTo, 'flashMessage', $this->name . " isimli doküman başarıyla eklendi!");

$this->emitTo('document.show', 'newDocumentCreated');
if ($this->folderId == null)
{
$documents = Document::where('folder_id', null)->orderBy('updated_at', 'DESC')->get();
} else {
$documents = Folder::findOrFail($this->folderId)->document()->orderBy('updated_at', 'DESC')->get();
}

$this->emitTo('document.show', 'newDocumentCreated', $documents);
$this->isCreating = false;
$this->name = "";
}

public function mount($emitTo, $folderId)
{
$this->folderId = $folderId;

$this->emitTo = $emitTo;

if(!$emitTo) {
$this->emitTo = 'dashboard';
}

if(!$folderId) {
$this->folderId = null;
}
}

public function render()
{
return view('livewire.document.create');
Expand Down
8 changes: 8 additions & 0 deletions server/app/Http/Livewire/Document/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
namespace App\Http\Livewire\Document;

use Livewire\Component;
use App\Models\Document;

class Edit extends Component
{
public $document;

public function mount($documentId)
{
$this->document = Document::findOrFail($documentId);
}

public function render()
{
return view('livewire.document.edit');
Expand Down
24 changes: 20 additions & 4 deletions server/app/Http/Livewire/Document/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ class Show extends Component

protected $listeners = ['newDocumentCreated'];

public function newDocumentCreated()
public function newDocumentCreated($documents)
{
$this->documents = auth()->user()->document()->orderBy('updated_at', 'DESC')->get();
if (!$documents)
{
$this->documents = auth()->user()->document()
->orderBy('updated_at', 'DESC')
->where('folder_id', null)
->get();
} else {
$this->documents = $documents;
}
}

public function mount()
public function mount($documents)
{
$this->documents = auth()->user()->document()->orderBy('updated_at', 'DESC')->get();
if (!$documents)
{
$this->documents = auth()->user()->document()
->orderBy('updated_at', 'DESC')
->where('folder_id', null)
->get();
} else {
$this->documents = $documents;
}
}

public function render()
Expand Down
43 changes: 37 additions & 6 deletions server/app/Http/Livewire/Folder/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Create extends Component
public $isCreatingFolder = false;
public $name = "";
public $message = "";
public $emitTo;
public $folderId;

public function confirmCreatingFolder()
{
Expand All @@ -19,21 +21,50 @@ public function confirmCreatingFolder()
public function createNewFolder()
{
try {
$folder = Folder::create([
'name' => $this->name
]);
if ($this->folderId == null) {
$folder = Folder::create([
'name' => $this->name
]);
} else {
$folder = Folder::create([
'name' => $this->name,
'parent_folder_id' => (int)$this->folderId
]);
}


$folder->user()->attach(auth()->user());
} catch (\Throwable $e) {
$this->emitTo('dashboard', 'flashMessage', "Oluşturma işlemi başarısız oldu!");
$this->emitTo($this->emitTo, 'flashMessage', "Oluşturma işlemi başarısız oldu!");
}
$this->emitTo('dashboard', 'flashMessage', $this->name . " isimli klasör başarıyla eklendi!");
$this->emitTo($this->emitTo, 'flashMessage', $this->name . " isimli klasör başarıyla eklendi!");

$this->emitTo('folder.show', 'newFolderCreated');
if ($this->folderId == null) {
$folders = Folder::where('parent_folder_id', null)->orderBy('updated_at', 'DESC')->get();
} else {
$folders = Folder::where('parent_folder_id', $this->folderId)->orderBy('updated_at', 'DESC')->get();
}

$this->emitTo('folder.show', 'newFolderCreated', $folders, $this->folderId);
$this->isCreatingFolder = false;
$this->name = "";
}

public function mount($emitTo, $folderId)
{
$this->folderId = $folderId;

$this->emitTo = $emitTo;

if(!$emitTo) {
$this->emitTo = 'dashboard';
}

if(!$folderId) {
$this->folderId = null;
}
}

public function render()
{
return view('livewire.folder.create');
Expand Down
13 changes: 13 additions & 0 deletions server/app/Http/Livewire/Folder/ListContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

namespace App\Http\Livewire\Folder;

use App\Models\Folder;
use Livewire\Component;

class ListContents extends Component
{
public $subfolders;
public $documents;
public $folderId;
public $message;

public function mount($folderId)
{
$this->folderId = $folderId;
$this->documents = Folder::findOrFail($folderId)->document()->orderBy('updated_at', 'DESC')->get();
$this->subfolders = Folder::where('parent_folder_id', $folderId)->orderBy('updated_at', 'DESC')->get();
}

public function render()
{
return view('livewire.folder.list-contents');
Expand Down
22 changes: 16 additions & 6 deletions server/app/Http/Livewire/Folder/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ class Show extends Component
public $folders;

protected $listeners = ['newFolderCreated'];

public function newFolderCreated()
public function newFolderCreated($folders, $folderId)
{
$this->folders = auth()->user()->folder()->orderBy('updated_at', 'DESC')->get();
if(!$folders)
{
$this->folders = auth()->user()->folder()->where('parent_folder_id', null)->orderBy('updated_at', 'DESC')->get();
} else {
$this->folders = auth()->user()->folder()->where('parent_folder_id', $folderId)->orderBy('updated_at', 'DESC')->get();
}
}

public function mount()
public function mount($folders, $folderId)
{
$this->folders = auth()->user()->folder()->orderBy('updated_at', 'DESC')->get();
if(!$folders)
{
$this->folders = auth()->user()->folder()->where('parent_folder_id', null)->orderBy('updated_at', 'DESC')->get();
} else {
$this->folders = auth()->user()->folder()->where('parent_folder_id', $folderId)->orderBy('updated_at', 'DESC')->get();
}
}

public function render()
{
return view('livewire.folder.show');
Expand Down
9 changes: 9 additions & 0 deletions server/app/Http/Middleware/CheckIfUserOwnsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public function handle(Request $request, Closure $next)
}
}

$folderId = $request->route('folderId');
if ($folderId)
{
if (Auth::user()->folder()->find($folderId) === null)
{
return redirect(route('dashboard'));
}
}

return $next($request);
}
}
2 changes: 1 addition & 1 deletion server/app/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Document extends Model
use HasFactory;

protected $fillable = [
'name', 'content'
'name', 'content', 'folder_id'
];

public function user()
Expand Down
2 changes: 1 addition & 1 deletion server/app/Models/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Folder extends Model
{
use HasFactory;

protected $fillable = ['name'];
protected $fillable = ['name', 'parent_folder_id'];

public function user()
{
Expand Down
8 changes: 0 additions & 8 deletions server/resources/views/document/show.blade.php

This file was deleted.

8 changes: 4 additions & 4 deletions server/resources/views/livewire/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
</ol>
</nav>
</div>
<livewire:document.create />
<livewire:folder.create />
<livewire:document.create :emitTo="null" :folderId="null" />
<livewire:folder.create :emitTo="null" :folderId="null"/>

<livewire:folder.show />
<livewire:document.show />
<livewire:folder.show :folders="null" :folderId="null" />
<livewire:document.show :documents="null" />
</div>
</div>
12 changes: 1 addition & 11 deletions server/resources/views/livewire/document/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@
</x-slot>

<x-slot name="content">
@if (!empty($message))
<div class="flex items-center bg-blue-500 text-white text-sm font-bold px-4 py-3 w-3/4" role="alert">
<svg class="fill-current w-4 h-4 mr-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path
d="M12.432 0c1.34 0 2.01.912 2.01 1.957 0 1.305-1.164 2.512-2.679 2.512-1.269 0-2.009-.75-1.974-1.99C9.789 1.436 10.67 0 12.432 0zM8.309 20c-1.058 0-1.833-.652-1.093-3.524l1.214-5.092c.211-.814.246-1.141 0-1.141-.317 0-1.689.562-2.502 1.117l-.528-.88c2.572-2.186 5.531-3.467 6.801-3.467 1.057 0 1.233 1.273.705 3.23l-1.391 5.352c-.246.945-.141 1.271.106 1.271.317 0 1.357-.392 2.379-1.207l.6.814C12.098 19.02 9.365 20 8.309 20z" />
</svg>
<p>{{ $message }}</p>
</div>
@endif
Oluşturmak istediğiniz dokümanın ismini yazınız.


<div class="mt-4" x-data="{}">
<x-jet-input type="text" class="mt-1 block w-3/4" placeholder="{{ __('Name') }}" x-ref="name"
wire:model.defer="name" />
Expand All @@ -33,7 +23,7 @@
{{ __('Close') }}
</x-jet-secondary-button>

<x-jet-button class="ml-2" wire:click="createNewDocument" wire:loading.attr="disabled">
<x-jet-button class="ml-2" wire:click="createNewDocument({{ $folderId }})" wire:loading.attr="disabled">
{{ __('Create') }}
</x-jet-button>
</x-slot>
Expand Down
9 changes: 6 additions & 3 deletions server/resources/views/livewire/document/edit.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<div>
{{-- Stop trying to control. --}}
</div>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
{{ $document->name }}
{{ $document->content }}
</div>
</div>
6 changes: 3 additions & 3 deletions server/resources/views/livewire/document/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ class="inline-flex items-center px-4 py-2 border border-transparent text-center
<div class="grid grid-cols-2 lg:grid-cols-4 gap-4" wire:loading.remove>
@foreach ($documents as $document)
<div class="float-left bg-white rounded shadow hover:bg-gray-100 transition duration-500">
<a href="{{ route('showDocument', $document->id) }}" class="block p-5">
<a href="{{ route('showDocument', $document["id"]) }}" class="block p-5">
<svg class="-ml-1 mr-3 h-6 w-6 float-left" fill="none" stroke="currentColor" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z">
</path>
</svg>
<div class="flex flex-col">
<div class="font-semibold w-full">{{ $document->name }}</div>
<div class="font-weight:300 text-gray-600 text-sm w-full">{{ $document->updated_at->isoFormat('LLL') }}
<div class="font-semibold w-full">{{ $document["name"] }}</div>
<div class="font-weight:300 text-gray-600 text-sm w-full">{{ \Carbon\Carbon::parse($document["updated_at"])->isoFormat('LLL') }}
</div>
</div>
</a>
Expand Down
Loading

0 comments on commit 003ed30

Please sign in to comment.