diff --git a/app/Livewire/QrCodeComponent.php b/app/Livewire/QrCodeComponent.php index 672b4b3..2d15297 100644 --- a/app/Livewire/QrCodeComponent.php +++ b/app/Livewire/QrCodeComponent.php @@ -18,6 +18,7 @@ class QrCodeComponent extends Component use LivewireAlert; protected QrCode $qrCode; + public string $uri; public int $format; public array $options; public array $colors = [ @@ -29,18 +30,12 @@ class QrCodeComponent extends Component public ?string $logoPath = null; public function mount() - { - $this->init(); - } - - protected function init($notify = false) { $this->format = FormatEnum::Text->value; - $this->options = [ - 'text' => 'https://2am.tech', - ]; + $this->options = ['text' => 'https://2am.tech']; + $this->qrCode = QrCodeFactory::build($this->format, $this->options); - $this->build(notify: $notify); + $this->setUri(); } #[On('create-qr-code')] @@ -48,8 +43,6 @@ public function create(int $format, array $options) { $this->format = $format; $this->options = $options; - - $this->build(); } #[On('apply-colors')] @@ -59,8 +52,6 @@ public function applyColors(array $foreground, array $background) 'background' => $background, 'foreground' => $foreground, ]; - - $this->build(); } #[On('apply-label')] @@ -73,31 +64,25 @@ public function applyLabel(string $label, int $size, string $alignment) 'align' => $alignment, ] : null; - - $this->build(); } #[On('apply-margin')] public function applyMargin(int $margins) { $this->margins = $margins; - - $this->build(); } #[On('apply-logo')] public function applyLogo(string $path) { $this->logoPath = $path; - - $this->build(); } - public function build($notify = true) + public function build() { - $this->normalizeOptions(); - try { + $this->normalizeOptions(); + /** @var QrCode $qrCode */ $qrCode = QrCodeFactory::build($this->format, $this->options); @@ -127,21 +112,18 @@ public function build($notify = true) } $this->qrCode = $qrCode; + $this->setUri(); - if ($notify) { - $this->alert('success', 'QR Code updated!'); - } + $this->alert('success', 'QR Code updated!'); } catch (Exception $exception) { $this->init(); $this->alert('error', $exception->getMessage()); } } - public function getUri() + public function setUri() { - return isset($this->qrCode) - ? $this->qrCode->writeDataUri() - : ''; + $this->uri = $this->qrCode->writeDataUri(); } public function download(string $extension) @@ -159,8 +141,8 @@ public function download(string $extension) } /** - * set the options the meet current's format data type * @return void + * @throws Exception */ protected function normalizeOptions(): void { @@ -173,6 +155,19 @@ protected function normalizeOptions(): void ->timezone(null) ->getTimestamp(); } + + if ($this->format === FormatEnum::Youtube->value) { + $url = $this->options['videoId']; + + $matches = null; + preg_match('/(?<=v=).*(?=&)/', $url, $matches); + + if (!is_array($matches) || empty($matches)) { + throw new Exception('Invalid URl'); + } + + $this->options['videoId'] = $matches[0]; + } } public function render() diff --git a/app/Livewire/Traits/FormsTrait.php b/app/Livewire/Traits/FormsTrait.php index d1943b3..532d640 100644 --- a/app/Livewire/Traits/FormsTrait.php +++ b/app/Livewire/Traits/FormsTrait.php @@ -68,7 +68,7 @@ protected function getRulesByForamt(int $format): array 'password' => 'string|required', ], FormatEnum::Youtube->value => [ - 'videoId' => 'string', + 'videoId' => 'regex:(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))', ], FormatEnum::MeCard->value => [ 'firstName' => 'string|required', diff --git a/resources/views/components/icons/tools.blade.php b/resources/views/components/icons/tools.blade.php new file mode 100644 index 0000000..1c5e287 --- /dev/null +++ b/resources/views/components/icons/tools.blade.php @@ -0,0 +1,3 @@ + + + diff --git a/resources/views/components/inputs/upload.blade.php b/resources/views/components/inputs/upload.blade.php index af9d1a7..2b3fca3 100644 --- a/resources/views/components/inputs/upload.blade.php +++ b/resources/views/components/inputs/upload.blade.php @@ -3,7 +3,7 @@
- Upload a file + {{ $this->file ? $this->file->getClientOriginalName() : __('Upload a file') }}
diff --git a/resources/views/components/options/colors.blade.php b/resources/views/components/options/colors.blade.php index 35f6552..32e6d10 100644 --- a/resources/views/components/options/colors.blade.php +++ b/resources/views/components/options/colors.blade.php @@ -1,7 +1,7 @@

Colors

- - Loren ipsum dolor describe function + + Define the colors for both QR Code lines and backgorund
diff --git a/resources/views/components/options/label.blade.php b/resources/views/components/options/label.blade.php index 4dd5955..3ff52fb 100644 --- a/resources/views/components/options/label.blade.php +++ b/resources/views/components/options/label.blade.php @@ -1,7 +1,7 @@

Label

- - Loren ipsum dolor describe function + + Define a Label and how it will be displayed under your generated QR Code diff --git a/resources/views/components/options/logo.blade.php b/resources/views/components/options/logo.blade.php index 6bc35f1..f765f07 100644 --- a/resources/views/components/options/logo.blade.php +++ b/resources/views/components/options/logo.blade.php @@ -1,7 +1,7 @@

Logo

- - Loren ipsum dolor describe function + + Set up a logo for your QR Code diff --git a/resources/views/components/options/margin.blade.php b/resources/views/components/options/margin.blade.php index c20c4d7..d47aacc 100644 --- a/resources/views/components/options/margin.blade.php +++ b/resources/views/components/options/margin.blade.php @@ -1,7 +1,7 @@

Margin

- - Loren ipsum dolor describe function + + Define the margins for your QR Code diff --git a/resources/views/forms/youtube.blade.php b/resources/views/forms/youtube.blade.php index a47429e..9673e9e 100644 --- a/resources/views/forms/youtube.blade.php +++ b/resources/views/forms/youtube.blade.php @@ -1,7 +1,7 @@
- +
diff --git a/resources/views/livewire/qr_code_component.blade.php b/resources/views/livewire/qr_code_component.blade.php index a75cbf1..be5bccf 100644 --- a/resources/views/livewire/qr_code_component.blade.php +++ b/resources/views/livewire/qr_code_component.blade.php @@ -2,11 +2,18 @@
- +
+ +
+ Create QR Code +
+ +
+
Download PNG