DataInventory

feat: add date support for steward

3/13/2020 3:29:55 PM

Details

diff --git a/app/Controllers/Http/ElementController.js b/app/Controllers/Http/ElementController.js
index 99c36e6..d6b554a 100644
--- a/app/Controllers/Http/ElementController.js
+++ b/app/Controllers/Http/ElementController.js
@@ -55,7 +55,7 @@ class ElementController {
   async store ({ session, request, response }) {
     const data = request.only(['name', 'theme_id', 'priority', 'refresh_rate'])
 
-    const validation = await validateAll(request.all(), {
+    const validation = await validateAll(data, {
       name: 'required|max:255',
       theme_id: 'required',
       priority: 'required',
diff --git a/app/Controllers/Http/StewardController.js b/app/Controllers/Http/StewardController.js
index 25e14a5..9a6c5c9 100644
--- a/app/Controllers/Http/StewardController.js
+++ b/app/Controllers/Http/StewardController.js
@@ -58,16 +58,26 @@ class StewardController {
    */
   async store ({ request, response }) {
 
-    // const data = request.only(['contact_id', 'element_id'])
-    var data = {}
-    data['contact_id'] = request.input('contact_id')
-    data['start_date'] = new Date()
-    data['end_date'] = new Date()
+    const data = request.only(['contact_id', 'element_id', 'start_date', 'end_date'])
 
-    var element = request.input('element_id')
+    const validation = await validateAll(data, {
+      'contact_id': 'required',
+      'element_id': 'required'
+    })
 
-    var steward = await Steward.create(data)
-    await steward.elements().attach([element])
+    if(validation.fails()) {
+      session
+        .withErrors(validation.messages())
+        .flashAll()
+      return response.redirect('back')
+    }
+    var fData = {
+      'contact_id': data['contact_id'],
+      'start_date': data['start_date'] || null,
+      'end_date': data['end_date'] || null
+    }
+    var steward = await Steward.create(fData)
+    await steward.elements().attach([data['element_id']])
     
     return response.redirect(Env.get('APP_URL') + '/stewards')
 
@@ -118,7 +128,7 @@ class StewardController {
    * @param {Response} ctx.response
    */
   async update ({ params, request, response }) {
-    const data = request.only(['contact_id', 'element_id'])
+    const data = request.only(['contact_id', 'element_id', 'start_date', 'end_date'])
 
     const validation = await validateAll(data, {
       contact_id: 'required',
@@ -137,7 +147,11 @@ class StewardController {
     await steward.elements().detach([prev_steward.id])
     await steward.elements().attach([data['element_id']])
     
-    steward.merge({contact_id: data['contact_id']})
+    steward.merge({
+      'contact_id': data['contact_id'],
+      'start_date': data['start_date'] || null,
+      'end_date': data['end_date'] || null
+    })
     await steward.save()
 
     return response.redirect(Env.get('APP_URL') + '/stewards')
diff --git a/resources/views/stewards/create.edge b/resources/views/stewards/create.edge
index 27aff11..b089855 100644
--- a/resources/views/stewards/create.edge
+++ b/resources/views/stewards/create.edge
@@ -28,14 +28,26 @@
     </div>
   </div>
 
+  <div class="field">
+    <label class="label">Start Date</label>
+    <input class="input start-datepicker" type="text" name="start_date" value="{{ old('start_date', '') }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('start_date'), hasErrorFor('start_date')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">End Date</label>
+    <input class="input end-datepicker" type="text" name="end_date" value="{{ old('end_date', '') }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('end_date'), hasErrorFor('end_date')) }}
+  </div>
+
   <div class="field is-grouped">
-      <div class="control">
-        <button class="button is-link ss-submit" type="submit">Submit</button>
-      </div>
-      <div class="control">
-      <button class="button is-text"><a href="{{ APP_URL() + route('/stewards.index') }}">Cancel</a></button>
-      </div>
+    <div class="control">
+      <button class="button is-link ss-submit" type="submit">Submit</button>
+    </div>
+    <div class="control">
+    <button class="button is-text"><a href="{{ APP_URL() + route('/stewards.index') }}">Cancel</a></button>
     </div>
+  </div>
     <!--
     <div class="notification is-warning">
       <button class="delete"></button>
@@ -44,4 +56,21 @@
     -->
 </form>
 
+@section('scripts')
+<script>
+  const start = datepicker('.start-datepicker', {
+    formatter: (input, date, instance) => {
+      const value = date.toLocaleDateString()
+      input.value = value // => '1/1/2099'
+    }
+  })
+  const end = datepicker('.end-datepicker', {
+    formatter: (input, date, instance) => {
+      const value = date.toLocaleDateString()
+      input.value = value // => '1/1/2099'
+    }
+  })
+</script>
+@endsection
+
 @endsection 
\ No newline at end of file
diff --git a/resources/views/stewards/edit.edge b/resources/views/stewards/edit.edge
index 3393500..93c9c66 100644
--- a/resources/views/stewards/edit.edge
+++ b/resources/views/stewards/edit.edge
@@ -28,14 +28,26 @@
     </div>
   </div>
 
+  <div class="field">
+    <label class="label">Start Date</label>
+    <input class="input start-datepicker" type="text" name="start_date" value="{{ TO_LOCALE(steward.start_date) }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('start_date'), hasErrorFor('start_date')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">End Date</label>
+    <input class="input end-datepicker" type="text" name="end_date" value="{{ TO_LOCALE(steward.end_date) }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('end_date'), hasErrorFor('end_date')) }}
+  </div>
+
   <div class="field is-grouped">
-      <div class="control">
-        <button class="button is-link ss-submit" type="submit">Modify</button>
-      </div>
-      <div class="control">
-      <button class="button is-text"><a href="{{ APP_URL() + route('/stewards.index') }}">Cancel</a></button>
-      </div>
+    <div class="control">
+      <button class="button is-link ss-submit" type="submit">Modify</button>
+    </div>
+    <div class="control">
+    <button class="button is-text"><a href="{{ APP_URL() + route('/stewards.index') }}">Cancel</a></button>
     </div>
+  </div>
     <!--
     <div class="notification is-warning">
       <button class="delete"></button>
@@ -44,4 +56,21 @@
     -->
 </form>
 
+@section('scripts')
+<script>
+  const start = datepicker('.start-datepicker', {
+    formatter: (input, date, instance) => {
+      const value = date.toLocaleDateString()
+      input.value = value // => '1/1/2099'
+    }
+  })
+  const end = datepicker('.end-datepicker', {
+    formatter: (input, date, instance) => {
+      const value = date.toLocaleDateString()
+      input.value = value // => '1/1/2099'
+    }
+  })
+</script>
+@endsection
+
 @endsection 
\ No newline at end of file
diff --git a/resources/views/stewards/index.edge b/resources/views/stewards/index.edge
index 25adba7..ec1be95 100644
--- a/resources/views/stewards/index.edge
+++ b/resources/views/stewards/index.edge
@@ -18,6 +18,8 @@
         <th>Element</th>
         <th>Steward Name</th>
         <th>Steward Email</th>
+        <th>Start Date</th>
+        <th>End Date</th>
         <th>Action</th>
       </tr>
     </thead>
@@ -27,6 +29,8 @@
         <td>{{ steward.elements[0].name}}</td>
         <td>{{ steward.contact.first_name }} {{ steward.contact.last_name }}</td>
         <td>{{ steward.contact.email }}</td>
+        <td>{{ steward.start_date ? TO_LOCALE(steward.start_date) : ''}}</td>
+        <td>{{ steward.end_date ? TO_LOCALE(steward.end_date) : 'n/a' }}</td>
         <td>
           <form action="{{ APP_URL() + route('/stewards.destroy', { id: steward.id }) + '?_method=DELETE'}}" method="post">
             {{ csrfField() }}