From fbf2143c0ca3d64245563f792d94774292f02def Mon Sep 17 00:00:00 2001 From: Azman Abdullah Date: Tue, 23 Jul 2024 20:52:04 +0700 Subject: [PATCH] fix(navigate): fix cache session issue not update after submit completed --- composer.json | 2 +- example/index.php | 18 +++--------------- src/CookieSessionHandler.php | 23 +++++++++++++++++++---- src/Form.php | 2 +- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 98a690e..8938adc 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "azmanabdlh/gargantua", - "version": "1.0.0", + "version": "1.1.0", "description": "Minimalist form wizard library.", "keywords": ["library", "form wizard"], "license": "MIT", diff --git a/example/index.php b/example/index.php index f767ab4..3c4979b 100644 --- a/example/index.php +++ b/example/index.php @@ -67,27 +67,15 @@
- +
- +
- -
-
- - -
-
- - -
-
- - +

Please introduce the 6 digit code we sent via email.

diff --git a/src/CookieSessionHandler.php b/src/CookieSessionHandler.php index 859ccf7..0c38487 100644 --- a/src/CookieSessionHandler.php +++ b/src/CookieSessionHandler.php @@ -35,7 +35,10 @@ private function makePages(Node $node): void { $pages = []; while (true) { $key = $node->data->pageName(); - $pages[$key] = []; + $pages[$key] = [ + "last" => !$node->canNext(), + "beginning" => !$node->canBack(), + ]; if ($node->canBack()) { $prevNode = $node->prev; @@ -59,8 +62,12 @@ private function getPage(string $key): array { return isset($this->pages[$key]) ? $this->pages[$key] : []; } + private function getCookie(): string { + return $this->request->cookies->get(self::CurrPageKey) ?? ""; + } + public function get(): string { - $currPageKey = $this->request->cookies->get(self::CurrPageKey) ?? ""; + $currPageKey = $this->getCookie(); if ($currPageKey == "") { return ""; @@ -68,8 +75,16 @@ public function get(): string { $curPage = $this->getPage($currPageKey); - if ($this->request->onSubmitted() && isset($curPage["next"])) { - $currPageKey = $curPage["next"]; + if ($this->request->onSubmitted()) { + if (isset($curPage["next"])) { + $currPageKey = $curPage["next"]; + } + + if ($curPage["last"] == 1) { + $head = array_filter($this->pages, fn($page) => $page["beginning"] == 1); + $key = array_keys($head)[0]; + $currPageKey = $key; + } } if ($this->request->onBack() && isset($curPage["prev"]) ) { diff --git a/src/Form.php b/src/Form.php index 4623fc7..311cf08 100644 --- a/src/Form.php +++ b/src/Form.php @@ -114,7 +114,7 @@ public function capture(array $cable = []): void { $this->emitter->emit(Event::RequestSubmit, $page, $this->request); - $isCompleted = !$node->canNext(); + $isCompleted = $node->canNext(); if ($node->canNext()) { $page = $node->next->data;