Skip to content

Commit 891753d

Browse files
authored
Add parameters directly from Chartable model's create view
* Added code to create Chart data from chartable model directly * Improved type checking to convert incorrectly saved blank arrays to objects * laravel mix updates to vue file * Added deprecation Notice for `RedirectsOnCreate` in readme. * Updated Changelog
1 parent 4428697 commit 891753d

File tree

6 files changed

+19
-39
lines changed

6 files changed

+19
-39
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ChangeLog
22
This document is meant for tracking notable changes to `nova-chartjs`
33

4-
## ChangeList
5-
Coming Soon
4+
## 0.1.0
5+
1. Chart metric values can now directly be added while creating new Chartable model.
6+
2. Deprecated `RedirectsOnCreate` Trait

README.md

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -285,35 +285,6 @@ class Employee extends Model implements Chartable
285285
}
286286
}
287287
```
288-
## Redirecting to Edit View after creating a new Chartable Model
289-
290-
You can optionally add the trait `RedirectsOnCreate` to your Chartable Model's Nova Resource to redirect to edit view right after creating a chartable model.
291-
292-
**_NOTE:_** You will need Laravel Nova version `2.0.4` or later to use this trait. Please make sure to update you Laravel Nova package to version `2.0.4` or later before adding this trait.
293-
294-
```php
295-
namespace App\Nova;
296-
297-
use KirschbaumDevelopment\NovaChartjs\InlinePanel;
298-
use KirschbaumDevelopment\NovaChartjs\Traits\RedirectsOnCreate;
299-
300-
class Employee extends Resource
301-
{
302-
use RedirectsOnCreate;
303-
304-
//...
305-
public function fields(Request $request)
306-
{
307-
return [
308-
//...
309-
310-
new InlinePanel($this, $request, 'Chart Name'),
311-
];
312-
}
313-
}
314-
```
315-
316-
This will help make it clearer for the user that they should add the Chart data right after creating the `Chartable` model record.
317288

318289
## Changelog
319290

dist/js/field.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32963,7 +32963,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
3296332963
* Set the initial, internal value for the field.
3296432964
*/
3296532965
setInitialValue: function setInitialValue() {
32966-
this.value = this.field.value && _typeof(this.field.value) == "object" ? this.field.value : {};
32966+
this.value = this.field.value && _typeof(this.field.value) === "object" && this.field.value.constructor === Object ? this.field.value : {};
3296732967
},
3296832968

3296932969

resources/js/components/FormField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
* Set the initial, internal value for the field.
2626
*/
2727
setInitialValue() {
28-
this.value = this.field.value && (typeof this.field.value == "object")? this.field.value : {};
28+
this.value = this.field.value && (typeof this.field.value === "object") && (this.field.value.constructor === Object)? this.field.value : {};
2929
},
3030
3131
/**

src/NovaChartjs.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class NovaChartjs extends Field
2626
public function __construct($name, $attribute = null, callable $resolveCallback = null)
2727
{
2828
parent::__construct($name, $attribute, $resolveCallback);
29-
$this->showOnCreation = false;
3029

3130
$this->withMeta([
3231
'showLabel' => false,
@@ -111,7 +110,6 @@ protected function fillAttributeFromRequest(NovaRequest $request, $requestAttrib
111110
{
112111
if ($request->exists($requestAttribute)) {
113112
$value = json_decode($request[$requestAttribute], true);
114-
115113
$model->{$attribute} = $this->isNullValue($value) ? null : $value;
116114
}
117115
}

src/Traits/HasChart.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
trait HasChart
99
{
10+
/** @var array */
11+
protected $unsavedMetricValues = [];
12+
1013
/**
1114
* Get the Chartable Model's metric values.
1215
*
@@ -20,13 +23,17 @@ public function novaChartjsMetricValue(): MorphOne
2023
/**
2124
* Delete a models chart data before model is deleted.
2225
*/
23-
public static function bootHasNovaChartjsChart()
26+
public static function bootHasChart()
2427
{
2528
static::deleting(function ($model) {
2629
if ($model->novaChartjsMetricValue) {
2730
$model->novaChartjsMetricValue->delete();
2831
}
2932
});
33+
34+
static::created(function ($model) {
35+
$model->novaChartjsMetricValue()->create(['metric_values' => $model->unsavedMetricValues]);
36+
});
3037
}
3138

3239
/**
@@ -37,8 +44,11 @@ public static function bootHasNovaChartjsChart()
3744
public function setNovaChartjsMetricValueAttribute($value): void
3845
{
3946
if (! $this->novaChartjsMetricValue) {
40-
$novaChartjsMetricValue = new NovaChartjsMetricValue(['metric_values' => $value]);
41-
$this->novaChartjsMetricValue()->save($novaChartjsMetricValue);
47+
if ($this->getKey()) {
48+
$this->novaChartjsMetricValue()->create(['metric_values' => $value]);
49+
} else {
50+
$this->unsavedMetricValues = $value;
51+
}
4252
} else {
4353
$this->novaChartjsMetricValue->metric_values = $value;
4454
$this->novaChartjsMetricValue->save();
@@ -48,7 +58,7 @@ public function setNovaChartjsMetricValueAttribute($value): void
4858
/**
4959
* Return a list of all models available for comparison to root model.
5060
*
51-
* @return \Illuminate\Database\Eloquent\Collection
61+
* @return array
5262
*/
5363
public static function getNovaChartjsComparisonData(): array
5464
{

0 commit comments

Comments
 (0)