sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Problem with colors for states

Open emil-nasso opened this issue 6 years ago • 5 comments

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

image

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?

emil-nasso avatar Dec 27 '19 10:12 emil-nasso

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...

dvlpp avatar Dec 28 '19 15:12 dvlpp

@emil-nasso did you have the chance to dig more on this issue?

dvlpp avatar Jan 24 '20 12:01 dvlpp

@dvlpp Not yet. I can probably look into it next week. Will be back with more info.

emil-nasso avatar Jan 28 '20 08:01 emil-nasso

@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:

PostPublishedState php — cards  SSH: docker ninjazombie se  2020-02-09 15-32-41

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.

https://dl.dropboxusercontent.com/s%2Fmzv7lm933gkf7pj%2FSharp%252C%2520Posts%2520%2520Sharp%25204.2.1%25202020-02-09%252015-25-36.png

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. :)

emil-nasso avatar Feb 09 '20 14:02 emil-nasso

Update. Boolean also didn't work. :)

This: PostPublishedState php — cards  SSH: docker ninjazombie se  2020-02-09 15-40-04

Gets exposed as this:

Sharp, Posts | Sharp 4 2 1 2020-02-09 15-40-59

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

emil-nasso avatar Feb 09 '20 14:02 emil-nasso