diff --git a/api/common/Controller/Mailboxes.php b/api/common/Controller/Mailboxes.php index 97fb792..a224799 100644 --- a/api/common/Controller/Mailboxes.php +++ b/api/common/Controller/Mailboxes.php @@ -83,16 +83,25 @@ class Mailboxes $body = $request->getBody(); $json = \Safe\json_decode($body->getContents()); if (!isset($json->mailboxes)) { - throw new MissingArgument('mailboxes', 'array', 'mailboxes names'); + throw new MissingArgument('mailboxes', 'array', 'mailboxes ids'); } $output = [ 'mailboxes' => $json->mailboxes, 'total' => count($json->mailboxes), - 'unregistered' => 0 + 'unregistered' => [ + 'total' => 0, + 'mailboxes' => [] + ] ]; - foreach ($json->mailboxes as $mailbox_name) { - if ($service->unregister($mailbox_name)) { - $output['unregistered'] ++; + foreach ($json->mailboxes as $mailbox_id) { + $mailbox = $service->getRepository()->fetchById($mailbox_id); + if ($service->unregister($mailbox->getName())) { + $output['unregistered']['total'] ++; + $output['unregistered']['mailboxes'] []= [ + 'id' => $mailbox->getId(), + 'name' => $mailbox->getName(), + 'unregistered' => true + ]; } } return $this->withJson($response, $output); diff --git a/api/common/Service/Mailboxes.php b/api/common/Service/Mailboxes.php index 58b5bf7..908cf00 100644 --- a/api/common/Service/Mailboxes.php +++ b/api/common/Service/Mailboxes.php @@ -118,6 +118,7 @@ class Mailboxes extends Base try { $mailbox = $this->getRepository()->fetchByName($mailbox_name); } catch (BlankResult $e) { + $this->getLogger()->error($e); // It's already unregistered return true; } diff --git a/api/src/Model/Message.php b/api/src/Model/Message.php index a5f9af7..e89e27a 100644 --- a/api/src/Model/Message.php +++ b/api/src/Model/Message.php @@ -146,7 +146,7 @@ class Message implements Model public function doesHaveAttachments(): Message { - if (!isset($this->getState()['has_attachments'])) { + if (!isset($this->getStates()['has_attachments'])) { $this->newState('has_attachments'); } $this->getState('has_attachments')->setValue(true); diff --git a/ui/common/Controller/Api.php b/ui/common/Controller/Api.php new file mode 100644 index 0000000..4202787 --- /dev/null +++ b/ui/common/Controller/Api.php @@ -0,0 +1,17 @@ +getBody(); + $json = \Safe\json_decode($body->getContents(), JSON_OBJECT_AS_ARRAY); + + return $service->sendRequest($json); + } +} diff --git a/ui/common/Service/Api.php b/ui/common/Service/Api.php new file mode 100644 index 0000000..0b56b92 --- /dev/null +++ b/ui/common/Service/Api.php @@ -0,0 +1,47 @@ +setClient($client) + ->setFactory($factory); + } + + protected ClientInterface $client; + protected RequestFactoryInterface $factory; + + public function getClient(): ClientInterface + { + return $this->client; + } + public function getFactory(): RequestFactoryInterface + { + return $this->factory; + } + public function setClient(ClientInterface $client): Api + { + $this->client = $client; + return $this; + } + public function setFactory(RequestFactoryInterface $factory): Api + { + $this->factory = $factory; + return $this; + } + + public function sendRequest(array $request_data): ResponseInterface + { + $request = $this->getFactory()->createRequest(strtoupper($request_data['method']) ?? 'GET', $request_data['uri']); + if (strtolower($request_data['method']) !== 'get') { + $request->getBody()->write(\Safe\json_encode($request_data['data'])); + $request->withHeader('Content-Type', 'application/json'); + } + return $this->getClient()->sendRequest($request); + } +} diff --git a/ui/resources/routes/98_api.php b/ui/resources/routes/98_api.php new file mode 100644 index 0000000..4baf51f --- /dev/null +++ b/ui/resources/routes/98_api.php @@ -0,0 +1,4 @@ +post('/api', Api::class); \ No newline at end of file diff --git a/ui/resources/routes/99_home.php b/ui/resources/routes/99_home.php index f989a1e..b378b41 100644 --- a/ui/resources/routes/99_home.php +++ b/ui/resources/routes/99_home.php @@ -1,4 +1,4 @@ get('[/]', Home::class); \ No newline at end of file +$app->get('[/]', Home::class); diff --git a/ui/resources/views/emails/mailboxes.blade.php b/ui/resources/views/emails/mailboxes.blade.php index fda295c..3081487 100644 --- a/ui/resources/views/emails/mailboxes.blade.php +++ b/ui/resources/views/emails/mailboxes.blade.php @@ -33,8 +33,8 @@ ).append( $('').addClass('ui mini circular red icon button').append( $('').addClass('remove icon') - ).click(() => { - this.unregister() + ).click(e => { + this.unregister($(e.currentTarget)) }) ) } else { @@ -42,7 +42,7 @@ register.append( $('').addClass('ui mini circular icon button').append( $('').addClass('save icon') - ).click((e) => { + ).click(e => { this.register($(e.currentTarget)) }) ) @@ -72,20 +72,25 @@ }) }) } - unregister() { + unregister(button) { const uri = '/mailboxes/unregister' const data = { mailboxes: [this.id] } + button.html('').append( + $('').addClass('redo loading icon') + ) return Send.delete({ uri, data }).then(response => { - response.mailboxes.forEach(mb => { - if (mb.id === this.id && mb.removed) { + response.unregistered.mailboxes.forEach(mb => { + if (mb.id === this.id && mb.unregistered) { this.id = null this.registered = false - mailboxes.draw().table() + const tr = button.parent().parent() + tr.html('') + this.draw(tr) } }) }) diff --git a/ui/resources/views/layout/body/footer/scripts/main.blade.php b/ui/resources/views/layout/body/footer/scripts/main.blade.php index b36b304..721f189 100644 --- a/ui/resources/views/layout/body/footer/scripts/main.blade.php +++ b/ui/resources/views/layout/body/footer/scripts/main.blade.php @@ -1,38 +1,33 @@