Skip to content

Instantly share code, notes, and snippets.

@oliuz
Forked from yelocode/RegisterForm.php
Created November 6, 2023 18:27
Show Gist options
  • Save oliuz/19b7fc4470aeca6e77b87d876f345474 to your computer and use it in GitHub Desktop.
Save oliuz/19b7fc4470aeca6e77b87d876f345474 to your computer and use it in GitHub Desktop.
Livewire 3 course components and views
@props(['name', 'title'])
<div
x-data="{ show : false , name : '{{ $name }}' }"
x-show="show"
x-on:hashchange.window="show = (location.hash === '#'+name)"
x-on:open-modal.window="($event.detail.name === name) ? (show = true, location.hash = '#'+name) : ''"
x-on:close-modal.window="location.hash = '#'"
x-on:keydown.escape.window="location.hash = '#'"
style="display:none;" class="fixed z-50 inset-0" x-transition>
{{-- Gray Background --}}
<div x-on:click="location.hash = '#'" class="fixed inset-0 bg-gray-300 opacity-40"></div>
{{-- Modal Body --}}
<div class="bg-white rounded m-auto fixed inset-0 max-w-2xl overflow-y-auto" style="max-height:500px">
@if (isset($title))
<div class="px-4 py-3 flex items-center justify-between border-b border-gray-300">
<div class="text-xl text-gray-800">{{ $title }}</div>
<a href="#">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</a>
</div>
@endif
<div class="p-4">
{{ $body }}
</div>
</div>
</div>
<div class="mt-10 p-5 mx-auto sm:w-full sm:max-w-sm shadow border-teal-500 border-t-2">
<div class="flex ">
<h2 class="text-center font-semibold text-2x text-gray-800 mb-5">Create New Account</h2>
</div>
@if (session('success'))
<span class="text-green-500">{{ session('success') }}</span>
@endif
<form wire:submit="create" action="" class="">
<label class="mt-3 block text-sm font-medium leading-6 text-gray-900">Name</label>
<input wire:model='name' type="text" id="name" placeholder="name.."
class="ring-1 ring-inset ring-gray-300 bg-gray-100 text-gray-900 text-sm rounded block w-full p-2.5 mx-auto">
@error('name')
<span class="text-red-500 text-xs">{{ $message }} </span>
@enderror
<label class="mt-3 block text-sm font-medium leading-6 text-gray-900">Email</label>
<input wire:model='email' type="email" id="email" placeholder="email.."
class="ring-1 ring-inset ring-gray-300 bg-gray-100 text-gray-900 text-sm rounded block w-full p-2.5 mx-auto">
@error('email')
<span class="text-red-500 text-xs">{{ $message }} </span>
@enderror
<label class="mt-3 block text-sm font-medium leading-6 text-gray-900">Password</label>
<input wire:model='password' type="password" id="password" placeholder=""
class="ring-1 ring-inset ring-gray-300 bg-gray-100 text-gray-900 text-sm rounded block w-full p-2.5 mx-auto">
@error('password')
<span class="text-red-500 text-xs">{{ $message }} </span>
@enderror
<label class="mt-3 block text-sm font-medium leading-6 text-gray-900">Profile picture</label>
<input wire:model='image' accept="image/png, image/jpeg" type="file" id="image"
class="ring-1 ring-inset ring-gray-300 bg-gray-100 text-gray-900 text-sm rounded block w-full p-2.5 mx-auto">
@error('image')
<span class="text-red-500 text-xs">{{ $message }} </span>
@enderror
@if ($image)
<img class="rounded w-10 h-10 mt-5 block" src="{{ $image->temporaryUrl() }}" alt="">
@endif
<div wire:loading wire:target="image">
<span class="text-green-500"> Uploading Image ... </span>
</div>
<div class="mt-3" wire:loading.delay>
<span class="text-green-500"> <svg aria-hidden="true"
class="w-8 h-8 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor" />
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill" />
</svg> </span>
</div>
<button type="button" @click="$dispatch('user-created')"
class="block mt-3 px-4 py-2 bg-teal-500 text-white font-semibold rounded hover:bg-teal-600">
Reload List
</button>
<button wire:loading.class.remove="bg-blue-500" wire:loading.attr="disabled" type="submit"
class="block mt-3 px-4 py-2 bg-teal-500 text-white font-semibold rounded hover:bg-teal-600">Create
+ </button>
</form>
</div>
<?php
namespace App\Livewire;
use App\Models\User;
use Livewire\Attributes\Rule;
use Livewire\Component;
use Livewire\WithFileUploads;
class RegisterForm extends Component
{
use WithFileUploads;
#[Rule('required|min:3|max:50')]
public $name;
#[Rule('required|email|max:255|unique:users')]
public $email;
#[Rule('required|min:3')]
public $password;
#[Rule('nullable|sometimes|image|max:1024')]
public $image;
public function create(){
$validated = $this->validate();
if($this->image){
$validated['image'] = $this->image->store('uploads','public');
}
$user = User::create($validated);
$this->reset('name','email','password','image');
session()->flash('success','User Created!');
$this->dispatch('user-created',$user);
$this->dispatch('close-modal');
}
public function render()
{
return view('livewire.register-form');
}
}
<div class=" p-5 mx-auto max-w-md">
<div wire:offline>
<div class="flex items-center p-4 mb-4 text-sm text-yellow-800 rounded-lg bg-yellow-50 dark:bg-gray-800 dark:text-yellow-300"
role="alert">
<svg class="flex-shrink-0 inline w-4 h-4 mr-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
fill="currentColor" viewBox="0 0 20 20">
<path
d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5ZM9.5 4a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM12 15H8a1 1 0 0 1 0-2h1v-3H8a1 1 0 0 1 0-2h2a1 1 0 0 1 1 1v4h1a1 1 0 0 1 0 2Z" />
</svg>
<span class="sr-only">Info</span>
<div>
<span class="font-medium">Warning alert!</span> You are offline. Make sure you have internet please.
</div>
</div>
</div>
<h2 class="text-2xl mb-3">Users List</h2>
<input wire:offline.remove swire:model.live='search' type="text" placeholder="Search..."
class="ring-1 ring-inset ring-gray-300 bg-gray-100 text-gray-900 text-sm rounded block w-full p-2.5 mx-auto">
<div role="status"
class="my-3 p-4 border border-gray-200 divide-y divide-gray-200 rounded shadow dark:divide-gray-700 md:p-6 dark:border-gray-700">
@foreach ($this->users as $user)
<div wire:key="{{ $user->id }}" class="flex items-center justify-between py-3">
<div class="">
<div class="text-gray-900 rounded-full dark:bg-gray-600 w-24 mb-23 truncate">{{ $user->name }}
</div>
<div class="text-xs text-gray-600 w-32 rounded-full dark:bg-gray-700">{{ $user->email }}</div>
</div>
<button wire:click="viewUser({{ $user }})"
class="text-white bg-teal-500 px-3 py-1 rounded-full">View</button>
</div>
@endforeach
</div>
@if ($selectedUser)
<x-modal name="user-details" title="View User">
<x-slot:body>
Name : {{ $selectedUser->name }}
<br>
Email : {{ $selectedUser->email }}
</x-slot:body>
</x-modal>
@endif
</div>
<?php
namespace App\Livewire;
use App\Models\User;
use League\Flysystem\MountManager;
use Livewire\Attributes\Computed;
use Livewire\Attributes\On;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;
class UsersList extends Component
{
use WithPagination;
#[Url(as : 's', history : true, keep : true)]
public $search = '';
public User $selectedUser;
public function viewUser(User $user){
$this->selectedUser = $user;
$this->dispatch('open-modal', name: 'user-details');
}
#[Computed()]
public function users(){
return User::latest()
->where('name','like',"%{$this->search}%")
->paginate(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment