Skip to content

Commit

Permalink
Fixes #15, by adding api call (#33)
Browse files Browse the repository at this point in the history
* Fixes #15, by adding api call

* Update status text

* Using this.$parent.resource.id.value now.
  • Loading branch information
bernhardh authored Dec 9, 2020
1 parent 4a72e00 commit ef08431
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ Toggle::make('Active')
->speed(500)
```
The default is 300ms


### Toggle on index
You can activate the toggle on index as well with
```php
Toggle::make('Active')
->editableIndex()
```
23 changes: 16 additions & 7 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</div>
<span v-else
class="inline-block rounded-full w-4 h-4"
:style="bgColor"
:style="bgColor"
/>
<span class="pl-2" v-if="label != null" >{{ label }}</span>
</div>
Expand All @@ -32,19 +32,28 @@ export default {
mounted() {
this.value = this.field.value || false
this.field.fill = formData => {
formData.append(this.field.attribute, this.trueValue)
}
},
methods: {
toggle() {
this.value = !this.value
Nova.request().post('/nova-vendor/nova-toggle/toggle/' + this.resourceName, {
value: this.value,
fieldName: this.field.attribute,
resourceId: this.resourceId
}).then((res) => {
if(res.data.success)
this.$toasted.show(this.field.indexName + ' changed', {type: 'success'});
else
this.$toasted.show(this.field.indexName + ' change error', {type: 'error'});
})
},
},
computed: {
resourceId() {
return this.$parent.resource.id.value;
},
editableIndex(){
return this.field.editable_index != undefined
},
Expand All @@ -58,7 +67,7 @@ export default {
label() {
if(! this.field.hide_label_on_index) {
return this.field.value == true ? this.trueLabel : this.falseLabel
return this.value == true ? this.trueLabel : this.falseLabel
}else {
return null;
}
Expand All @@ -73,7 +82,7 @@ export default {
},
bgColor(){
return 'background-color:' + (this.field.value == true ? this.trueColor : this.falseColor) + ';'
return 'background-color:' + (this.value == true ? this.trueColor : this.falseColor) + ';'
},
colors(){
Expand Down
28 changes: 28 additions & 0 deletions src/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Davidpiesse\NovaToggle;

use Illuminate\Http\Request;

class ApiController
{
public function index(\Laravel\Nova\Http\Requests\ResourceDetailRequest $request)
{
$resourceClass = $request->resource();
$modelClass = $resourceClass::$model;
$model = $modelClass::find($request->post('resourceId'));

if($model) {
$model->{$request->post('fieldName')} = $request->post('value');
$model->save();

return [
'success' => true
];
}

return [
'success' => false
];
}
}
20 changes: 16 additions & 4 deletions src/FieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;


class FieldServiceProvider extends ServiceProvider
{
Expand All @@ -19,15 +21,25 @@ public function boot()
Nova::script('nova-toggle', __DIR__.'/../dist/js/field.js');
Nova::style('nova-toggle', __DIR__.'/../dist/css/field.css');
});

$this->app->booted(function () {
$this->routes();
});
}



/**
* Register any application services.
* Register the tool's routes.
*
* @return void
*/
public function register()
protected function routes()
{
//
if ($this->app->routesAreCached()) {
return;
}

Route::middleware('nova')
->post('nova-vendor/nova-toggle/toggle/{resource}', [ApiController::class, 'index']);
}
}
4 changes: 2 additions & 2 deletions src/Toggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Toggle extends Boolean
{
public $component = 'nova-toggle';

public function trueLabel($label){
return $this->withMeta([
'true_label' => $label,
Expand Down Expand Up @@ -90,5 +90,5 @@ public function speed($ms){
'speed' => $ms,
]);
}

}

0 comments on commit ef08431

Please sign in to comment.