Browse Source

fixed note updates

Josh Kamau 5 years ago
parent
commit
d374400f2e

+ 29 - 13
app/Http/Controllers/NoteController.php

@@ -11,6 +11,7 @@ use Cookie;
 
 use App\Models\Note;
 use App\Models\Client;
+use App\Models\Section;
 use App\Models\SectionTemplate;
 
 class NoteController extends Controller
@@ -34,13 +35,17 @@ class NoteController extends Controller
 
     public function sectionCreateForm($note_uid, $section_template_uid, Request $request)
     {
-
         $note = Note::where('uid', $note_uid)->first();
-
         $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
-
         $section = null; // convenience
+        include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
+    }
 
+    public function sectionUpdateForm($section_uid, Request $request)
+    {
+        $section = Section::where('uid', $section_uid)->first();
+        $note = Note::where('id', $section->note_id)->first();
+        $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
         include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
     }
 
@@ -61,8 +66,8 @@ class NoteController extends Controller
             $note = Note::where('uid', $note_uid)->first();
             $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
         } else {
-            $note = $section->note();
-            $sectionTemplate = $section->sectionTemplate();
+            $note = Note::where('id', $section->note_id)->first();
+            $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
         }
 
         $newContentData = [];
@@ -72,16 +77,21 @@ class NoteController extends Controller
         // if UPDATE, $section, and $request
         include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
 
-        $newContentData = ['dog' => 'bark', 'cat' => 'meow'];
         // now, create summaryHtml appropriate
         ob_start();
-        echo 'this is not going to scream anywhere.';
         include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
         $newSummaryHtml = ob_get_contents();
         ob_end_clean();
 
         if($section){
             // call Java to update section
+            $data = [
+                'uid' => $section->uid,
+                'contentData' => json_encode($newContentData),
+                'summaryHtml' => $newSummaryHtml
+            ];
+            $response = $this->calljava($request, '/api/section/update', $data);
+            //TODO: handle if response->success == false
         }else{
             // call Java to create section
             $data = [
@@ -90,12 +100,18 @@ class NoteController extends Controller
                 'contentData' => json_encode($newContentData),
                 'summaryHtml' => $newSummaryHtml
             ];
-            $url = env('BACKEND_URL', 'http://localhost:8080') . '/api/section/create';
-            $response = Http::asForm()
-            ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
-            ->post($url, $data)
-            ->json();
-            dd($response);
+            $response = $this->callJava($request, '/api/section/create', $data);
+            //TODO: handle if response->success == false
         }
+        return redirect(route('render-note',$note->uid));
+    }
+
+    private function callJava($request, $endPoint, $data){
+        $url = env('BACKEND_URL', 'http://localhost:8080') . $endPoint;
+        $response = Http::asForm()
+        ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
+        ->post($url, $data)
+        ->json();
+        return $response;
     }
 }

+ 9 - 0
app/Models/Section.php

@@ -9,4 +9,13 @@ class Section extends Model
 
     protected $table = "section";
 
+    public function note()
+    {
+        return $this->belongsTo(Note::class);
+    }
+
+    public function sectionTemplate()
+    {
+        return $this->belongsTo(SectionTemplate::class);
+    }
 }

+ 15 - 8
resources/views/client/note.blade.php

@@ -9,21 +9,28 @@
                 </div>
                 <div class="card-body">
                     <div class="mb-2">
-                        <a href="{{route('select_section_template_form', $note->uid)}}" 
-                            up-modal="#select_section_template_form" 
-                            up-history="false"
-                            class="btn btn-primary">Add section</a>
+                        <a href="{{route('select_section_template_form', $note->uid)}}"
+                            up-modal="#select_section_template_form" up-history="false" class="btn btn-primary">Add
+                            section</a>
                     </div>
                     @if(!$note->sections->count())
                     <div class="alert alert-info">No sections on this note.</div>
-                    @else 
-                    
+                    @else
+
+                    @foreach ($note->sections as $section)
                     <div class="card">
+                        <div class="card-header">
+                            {{$section->sectionTemplate->title}}
+                        </div>
                         <div class="card-body">
-
+                            {!! $section->summary_html !!}
+                        </div>
+                        <div class="card-footer">
+                            <a href="{{route('section_update_form', $section->uid)}}"
+                                up-modal="#section_form" up-history="false" class="btn btn-primary">Update section</a>
                         </div>
                     </div>
-                    
+                    @endforeach
                     @endif
                 </div>
             </div>

+ 0 - 11
resources/views/client/section_create_form.blade.php

@@ -1,11 +0,0 @@
-<div id="section_create_form">
-    <div class="card">
-        <div class="card-header">
-            Create section
-        </div>
-        <div class="card-body">
-            Note UID: {{$note->uid}}
-            Section template UID: {{$sectionTemplate->uid}}
-        </div>
-    </div>
-</div>

+ 1 - 1
resources/views/client/select_section_template_form.blade.php

@@ -7,7 +7,7 @@
                 @foreach ($sectionTemplates as $sectionTemplate)
                 <li>
                     <a href="{{route('section_create_form', [$note->uid, $sectionTemplate->uid])}}" 
-                        up-modal="#section_create_form" up-history="false">
+                        up-modal="#section_form" up-history="false">
                         {{$sectionTemplate->title}}
                     </a>
                 </li>

+ 1 - 0
routes/web.php

@@ -74,4 +74,5 @@ Route::bind('url_slug', function($value, $route)
 Route::get('/note/{note_uid}', 'NoteController@renderNote')->name('render-note');
 Route::get('/select_section_template_form/{note_uid}', 'NoteController@selectSectionTemplateForm')->name('select_section_template_form');
 Route::get('/section_create_form/{note_uid}/{section_template_uid}', 'NoteController@sectionCreateForm')->name('section_create_form');
+Route::get('/section_update_form/{section_uid}', 'NoteController@sectionUpdateForm')->name('section_update_form');
 Route::post("/process_form_submit", 'NoteController@processFormSubmit')->name('process_form_submit');

+ 18 - 5
storage/sections/test1/form.blade.php

@@ -1,4 +1,13 @@
-<div id="section_create_form">
+<?php
+$contentData = [
+    'nickname'=>'',
+    'favoriteColor'=>''
+]; 
+if($section){
+    $contentData = json_decode($section->content_data, true);
+}
+?>
+<div id="section_form">
 
     <div class="card">
         <div class="card-header">
@@ -10,15 +19,19 @@
         </div>
         <div class="card-body">
             <form method="POST" up-target="#note-container" action="/process_form_submit" up-history="false">
-                <input type="hidden" name="note_uid" value="<?= $note->uid?>">
-                <input type="hidden" name="section_template_uid" value="<?= $sectionTemplate->uid ?>">
+                <?php if($section): ?>
+                    <input type="hidden" name="section_uid" value="<?= $section->uid?>">
+                <?php else: ?>
+                    <input type="hidden" name="note_uid" value="<?= $note->uid?>">
+                    <input type="hidden" name="section_template_uid" value="<?= $sectionTemplate->uid ?>">
+                <?php endif; ?>
                 <div class="form-group">
                     <label for="">Nickname</label>
-                    <input type="text" class="form-control" name="nickname" placeholder="nickname">
+                    <input type="text" class="form-control" name="nickname" value="<?= $contentData['nickname'] ?>" placeholder="nickname">
                 </div>
                 <div class="form-group">
                     <label for="">Favorite color</label>
-                    <input type="text" class="form-control" name="favoriteColor" placeholder="Favorite color">
+                    <input type="text" class="form-control" name="favoriteColor" value="<?= $contentData['favoriteColor'] ?>" placeholder="Favorite color">
                 </div>
                 <div class="form-group">
                     <button class="btn btn-primary">Submit</button>

+ 7 - 1
storage/sections/test1/processor.php

@@ -1,3 +1,9 @@
 <?php 
 
-$name = "Hello";
+$nickname = $request->get('nickname');
+$favoriteColor = $request->get('favoriteColor');
+
+$newContentData = [
+    'nickname'=>$nickname,
+    'favoriteColor'=>$favoriteColor
+];

+ 3 - 2
storage/sections/test1/summary.php

@@ -1,2 +1,3 @@
-<h1>This will not show without dd()</h1>
-<?php  var_dump($newContentData); ?>
+<div class="p-5" style="background-color:<?= $newContentData['favoriteColor']; ?>; ">
+    <h1><?= $newContentData['nickname']; ?></h1>
+</div>