Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Status as integer (correction) #15

Closed
envolute opened this issue Feb 28, 2024 · 5 comments
Closed

[Bug]: Status as integer (correction) #15

envolute opened this issue Feb 28, 2024 · 5 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@envolute
Copy link

What happened?

Hello Mokhosh!!

Congratulations on the work, really good and simple to implement in the project.

I have a Model where the "Status" field is an integer. I use an Enum (int) to define the values. Even so, I configured it and, initially, everything worked normally. However, I identified and fixed some problems:

1º - Status Title:
I saw that it shows the status title based on the Enum value, as in my case it is an integer, it shows the ID.
Correction: Observing the "IsKanbanStatus" trait, I saw that it was enough to implement the getTitle() method passing the titles, which shows the Status correctly... See the code below...

enum statusEnum:int implements HasLabel
{
    use IsKanbanStatus;
    case TODO = 0;
    case DOING = 1;
    case DONE = 2;
    public function getLabel(): ?string {
        return match ($this) {
            self::TODO => 'To-Do',
            self::DOING => 'In Progress',
            self::DONE => 'Done',
        };
    }
    public function getTitle(): ?string {
        return $this->getLabel();
    }
}

2º - Conflict between Status and Record
When I tried to drag an item to another status, I saw that I could drag internal elements from Records separately. I was able to drag a div from a record into another record or to another status and, obviously, this generated an error...
I noticed that the problem is that the code identifies the "Status" container by the tag ID, and the tag ID is generated from the status value, which in my case, is an integer.
This generates a <div id=1> conflict, as the record tag ID is also loaded from the record ID in the database
So, depending on the value of the item, we can have 2 elements with the same id. See the image below...
Desktop-screenshot
Of course this problem will not occur with items with larger IDs. Ex: id="185466", But if you test with few items, it may occur...

Correction: As the objective is to load the status ID from a string, so as not to conflict with the record ID, I added a prefix to the status ID tag.

[Final Step-by-step]

File [pages/KanbanBoard.php #37]
Add prefix variable:
protected static string $statusPrefix = 'status';
So I can change the prefix if necessary

File: [kanban-status.blade.php #7]
I changed from:
id="{{ $status['id'] }}"
for
id="{{ self::$statusPrefix }}-{{ $status['id'] }}"

File: [kanban-scripts.blade.php #32]
I changed from:
const statuses = @js($statuses->map(fn ($status) => $status['id']))
for
const statuses = @js($statuses->map(fn ($status) => self::$statusPrefix.'-'.$status['id']))

Then, for the system to pick up the correct Status, I just remove the prefix where necessary

File: [concerns/HasStatusChange.php #12 / #30]
Add this line: $status = str_replace($statusPrefix.'-', '', $status);

public function statusChanged(int $recordId, string $status, array $fromOrderedIds, array $toOrderedIds): void
{
         $status = str_replace($statusPrefix.'-', '', $status);
         $this->onStatusChanged($recordId, $status, $fromOrderedIds, $toOrderedIds);
}
public function sortChanged(int $recordId, string $status, array $orderedIds): void
{
        $status = str_replace($statusPrefix.'-', '', $status);
        $this->onSortChanged($recordId, $status, $orderedIds);
}

Desktop-screenshot (1)

How to reproduce the bug

Use an Enum (int) to define the Status values

Package Version

2.3.0

PHP Version

8.1.10

Laravel Version

10.10

Which operating systems does with happen with?

Windows

Which browsers does with happen with?

Chrome

Notes

No response

@envolute envolute added the bug Something isn't working label Feb 28, 2024
@envolute
Copy link
Author

An observation:
Before making adjustments to the code I saw that in addition to the problem of "dragging" elements, statuses with larger values did not allow moving items... I believe this problem may be related to issue #12
If so, this fix would also resolve this...

@envolute
Copy link
Author

An adjustment:
In the code above there is an error related to the file [concerns/HasStatusChange.php #Lines: 12 / 30]
Wrong:
$status = str_replace($statusPrefix.'-', '', $status);
Right:
$status = str_replace(self::$statusPrefix.'-', '', $status);

@mokhosh
Copy link
Owner

mokhosh commented Feb 28, 2024

Thanks! I appreciate the detailed issue.

As for 1º, I had talked about this in the videos, but now I've added it to the docs so it's more clear how you can customize the title.

For 2º, I fixed it in a different way that doesn't require you to choose a prefix, add it as a salt in the font end, and then remove it in the backend.

@mokhosh mokhosh closed this as completed Feb 28, 2024
@mokhosh
Copy link
Owner

mokhosh commented Feb 28, 2024

@mokhosh mokhosh added documentation Improvements or additions to documentation good first issue Good for newcomers labels Feb 28, 2024
@envolute
Copy link
Author

Great, mokhosh!!
I updated and everything is actually working perfectly...

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants