Problem with colors for states
Hi! I am trying sharp out but I ran into a javascript issue with states.

This is the lists configuration with the state defined.
public function buildListConfig()
{
$this->setInstanceIdAttribute('id')
->setSearchable()
->setDefaultSort('created_at', 'desc')
->setPaginated()
->setEntityState('published', PostPublishedState::class)
->setReorderable(new PostReorderHandler());
}
Here is the `PostPublishedState:
<?php
namespace App\Sharp;
use App\Post;
use Code16\Sharp\EntityList\Commands\EntityState;
class PostPublishedState extends EntityState
{
/**
* @return mixed
*/
protected function buildStates()
{
$this->addState("1", "Published", self::PRIMARY_COLOR);
$this->addState("0", "Draft", self::SECONDARY_COLOR);
}
protected function updateState($instanceId, $stateId)
{
Post::findOrFail($instanceId)->update(
[
'published' => $stateId,
]
);
return $this->refresh($instanceId);
}
}
Can anyone provide me with any pointers as to where I can go from here? :) Any more information i can provide?
Hi I’m away from keyboard for a few days, but I think the issue is related to some data with a state value which is not allowed by your code (a value which is not 0 nor 1). I think, if this is it, that front code should be more permissive...
@emil-nasso did you have the chance to dig more on this issue?
@dvlpp Not yet. I can probably look into it next week. Will be back with more info.
@dvlpp I did some more digging here.
I have a "published"-field on my entitry. It is created as a "boolean" in laravel migrations but it really is stored as the strings "1" or "0".
The problem was a issue with types.
To test this out I added both string and int-representations of the states like this:
When looking at the data in the ajax request you can clearly that only the int-versions of the states are published. For the model the field consists of strings. This is probably why the colors can't be mapped.

The problem seems to be that something converts the states from strings to ints.
I fixed the problem by casting the field on the eloquent model to ints like this:
protected $casts = [
'published' => 'integer',
];
After that, everything worked as expected.
This was mostly done to demonstrate the problem. I will change the implementation to cast to bool instead as that makes more sense. :)
Update. Boolean also didn't work. :)
This:

Gets exposed as this:
I'll have to keep it as int for now and cast to int on the model.