Skip to content

Commit

Permalink
chore: added a automatically random ID for short link if user not pro…
Browse files Browse the repository at this point in the history
…vides one
  • Loading branch information
paulocastellano committed Nov 21, 2024
1 parent 2eaadaa commit 8bb79d9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
12 changes: 8 additions & 4 deletions app/Http/Controllers/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ public function store(CreateRequest $request)
return back();
}

$key = $request->key ? $request->key : Str::random(6);

$link = Link::create([
'workspace_id' => $workspace->id,
'domain' => $request->domain,
'key' => $request->key,
'key' => $key,
'url' => $request->url,
'link' => "https://{$request->domain}/{$request->key}",
'link' => "https://{$request->domain}/{$key}",
'utm_source' => $request->utm_source,
'utm_medium' => $request->utm_medium,
'utm_campaign' => $request->utm_campaign,
Expand All @@ -99,11 +101,13 @@ public function update($id, UpdateRequest $request)

$link = Link::where('workspace_id', $workspace->id)->where('id', $id)->firstOrFail();

$key = $request->key ? $request->key : Str::random(6);

$link->update([
'domain' => $request->domain,
'key' => $request->key,
'key' => $key,
'url' => $request->url,
'link' => "https://{$request->domain}/{$request->key}",
'link' => "https://{$request->domain}/{$key}",
'utm_source' => $request->utm_source,
'utm_medium' => $request->utm_medium,
'utm_campaign' => $request->utm_campaign,
Expand Down
16 changes: 10 additions & 6 deletions app/Http/Requests/Link/CreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ class CreateRequest extends FormRequest
public function rules(): array
{
return [
'key' => [
'required',
'string',
'max:255',
Rule::unique('links')->where('domain', $this->domain)->ignore($this->route('id')),
],
'key' => Rule::when(
fn() => $this->key,
[
'required',
'string',
'max:255',
Rule::unique('links')->where('domain', $this->domain)->ignore($this->route('id')),
]
),

'domain' => [
'required',
'string',
Expand Down
16 changes: 10 additions & 6 deletions app/Http/Requests/Link/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ class UpdateRequest extends FormRequest
public function rules(): array
{
return [
'key' => [
'required',
'string',
'max:255',
Rule::unique('links')->where('domain', $this->domain)->ignore($this->route('id')),
],
'key' => Rule::when(
fn() => $this->key,
[
'required',
'string',
'max:255',
Rule::unique('links')->where('domain', $this->domain)->ignore($this->route('id')),
]
),

'domain' => [
'required',
'string',
Expand Down
1 change: 0 additions & 1 deletion lang/php_en.json

This file was deleted.

2 changes: 1 addition & 1 deletion resources/js/Components/Tag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defineProps({

<template>
<div
:class="`inline-flex bg-${tag.color}-100 text-${tag.color}-500 ring-1 ring-inset ring-${tag.color}-300 ring-opacity-40 text-xs truncate font-medium px-2 py-1 rounded`"
:class="`inline-flex text-${tag.color}-500 ring-1 ring-inset ring-${tag.color}-300 ring-opacity-40 text-xs truncate font-medium px-2 py-1 rounded`"
>
{{ tag.name }}
</div>
Expand Down
30 changes: 15 additions & 15 deletions resources/js/Pages/Link/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ onMounted(() => {
leave-to-class="transform opacity-0 scale-95"
>
<MenuItems
class="absolute right-0 z-10 mt-2 w-56 origin-top-right divide-y divide-gray-100 rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none"
class="absolute right-0 z-10 mt-2 w-56 origin-top-right divide-y divide-gray-100 dark:divide-gray-700 rounded-md bg-white dark:bg-zinc-900 shadow-lg ring-1 ring-black dark:ring-zinc-700 ring-opacity-5 focus:outline-none"
>
<div class="py-1">
<MenuItem v-slot="{ active }">
Expand All @@ -260,13 +260,13 @@ onMounted(() => {
"
:class="[
active
? 'bg-gray-100 text-gray-900'
: 'text-gray-700',
'group flex items-center px-4 py-2 text-sm',
? 'bg-gray-100 dark:bg-zinc-800 text-gray-900 dark:text-zinc-300'
: 'text-gray-700 dark:text-zinc-300',
'flex items-center px-4 py-2 text-sm cursor-pointer',
]"
>
<PhPencil
class="mr-3 h-5 w-5 text-gray-400 group-hover:text-gray-500"
class="mr-3 h-5 w-5 text-gray-400 dark:text-zinc-300"
aria-hidden="true"
/>
Edit
Expand All @@ -279,13 +279,13 @@ onMounted(() => {
"
:class="[
active
? 'bg-gray-100 text-gray-900'
: 'text-gray-700',
'group flex items-center px-4 py-2 text-sm cursor-pointer',
? 'bg-gray-100 dark:bg-zinc-800 text-gray-900 dark:text-zinc-300'
: 'text-gray-700 dark:text-zinc-300',
'flex items-center px-4 py-2 text-sm cursor-pointer',
]"
>
<PhQrCode
class="mr-3 h-5 w-5 text-gray-400 group-hover:text-gray-500"
class="mr-3 h-5 w-5 text-gray-400 dark:text-zinc-300"
aria-hidden="true"
/>
QR Code
Expand All @@ -303,13 +303,13 @@ onMounted(() => {
"
:class="[
active
? 'bg-gray-100 text-gray-900'
: 'text-gray-700',
'group flex items-center px-4 py-2 text-sm cursor-pointer',
? 'bg-gray-100 dark:bg-zinc-800 text-gray-900 dark:text-zinc-300'
: 'text-gray-700 dark:text-zinc-300',
'flex items-center px-4 py-2 text-sm cursor-pointer',
]"
>
<PhCopy
class="mr-3 h-5 w-5 text-gray-400 group-hover:text-gray-500"
class="mr-3 h-5 w-5 text-gray-400 dark:text-zinc-300"
aria-hidden="true"
/>
Copy Link ID
Expand All @@ -334,9 +334,9 @@ onMounted(() => {
"
:class="[
active
? 'bg-red-50 text-red-600'
? 'bg-zinc-100 dark:bg-zinc-800 text-red-600'
: 'text-red-500',
'group flex items-center px-4 py-2 text-sm cursor-pointer',
'flex items-center px-4 py-2 text-sm cursor-pointer',
]"
>
<PhTrash
Expand Down

0 comments on commit 8bb79d9

Please sign in to comment.