Skip to content

Instantly share code, notes, and snippets.

@assertchris
Created December 5, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save assertchris/746ae1cbac85aafeecfb834fa3230e76 to your computer and use it in GitHub Desktop.
Save assertchris/746ae1cbac85aafeecfb834fa3230e76 to your computer and use it in GitHub Desktop.
<div class="flex flex-col w-full">
<h1 class="flex w-full text-3xl font-serif mb-2">People I'm sharing with</h1>
<div class="flex flex-col w-full mb-2">
@forelse($shares as $share)
<div class="flex flex-row w-full mb-1 py-2 items-center justify-start border-b-2 border-gray-200">
<span class="flex flex-col flex-grow">
<span>{{ $share->email }}</span>
<span class="text-sm text-gray-500">everything after {{ parse($share->share_after)->format("Y/m/d") }}</span>
</span>
<button wire:click="stop({{ $share->id }})" class="bg-blue-600 text-white py-1 px-2 rounded-sm">stop sharing</button>
</div>
@empty
No shares.
@endforelse
</div>
<details>
<summary class="outline-none mb-2">Share with someone else</summary>
<div class="flex flex-col w-full items-start">
<label class="flex flex-col w-full mb-4">
<span class="text-xs font-semibold text-gray-800">Share with this email address</span>
<input wire:change="change('email', $event.target.value)" value="{{ $email }}" class="py-1 px-2 rounded-sm" />
</label>
<label class="flex flex-col w-full mb-4">
<span class="text-xs font-semibold text-gray-800">Share everything after this date (year/month/day)</span>
<input wire:change="change('after', $event.target.value)" value="{{ $after }}" class="py-1 px-2 rounded-sm" />
</label>
<button wire:click="share" class="bg-blue-600 text-white py-1 px-2 rounded-sm self-end">share now</button>
</div>
</details>
</div>
<?php
namespace App\Http\Livewire;
use App\Models\Share;
use Carbon\Carbon;
use Livewire\Component;
class SharedByMe extends Component
{
protected $shares;
public $email;
public $after;
public function mount()
{
$this->reset();
}
public function reset()
{
$this->email = "";
$this->after = now()
->subDays(1)
->format("Y/m/d");
$this->shares = json(client()->get("/api/shared-by-me"));
}
public function change($field, $value)
{
$this->$field = $value;
}
public function share()
{
Share::updateOrCreate(
["email" => $this->email, "owner_id" => auth()->user()->id],
["share_after" => Carbon::parse($this->after)]
);
$this->reset();
}
public function stop($id)
{
Share::find($id)->delete();
$this->reset();
}
public function render()
{
$shares = $this->shares;
return view('livewire.shared-by-me', compact("shares"));
}
}
<div>
@if ($share)
<h1 class="flex w-full text-3xl font-serif mb-1 py-2 border-b-2 border-gray-200">Questions for {{ $share->owner->name }}</h1>
@endif
@foreach ($sections as $sectionKey => $subsections)
<div class="flex flex-col w-full">
<h2 class="flex w-full text-2xl font-serif mb-1">
{{ __("grow.{$sectionKey}-title") }}
</h2>
@if (__("grow.{$sectionKey}-description"))
<div class="flex w-full mb-1">
{{ __("grow.{$sectionKey}-description") }}
</div>
@endif
@foreach ($subsections as $i => $subsectionKey)
<h3 class="flex w-full text-xl font-serif mb-1">
{{ __("grow.{$sectionKey}-{$subsectionKey}-title") }}
</h3>
@if (__("grow.{$sectionKey}-{$subsectionKey}-description"))
<div class="flex w-full mb-1">
{{ __("grow.{$sectionKey}-{$subsectionKey}-description") }}
</div>
@endif
@foreach ($notes["{$sectionKey}-{$subsectionKey}"] as $note)
<div
class="flex w-full mb-1 py-2 border-b-2 border-gray-200"
wire:key="{{ $note->id }}"
>
<div
class="
group
flex flex-row w-full items-center justify-start
@if ($note->deleted_at)
opacity-25
@endif
"
>
<span
class="
flex flex-grow flex-col
@if ($note->completed_at)
line-through
text-gray-500
@endif
"
>
<span class="flex w-full">{{ $note->content }}</span>
<span class="flex w-full text-sm text-gray-500">{{ $note->created_at->diffForHumans() }}</span>
</span>
@if ($note->completed_at)
<button wire:click="uncomplete({{ $note->id }})" class="bg-blue-600 text-white py-1 px-2 rounded-sm mr-2 opacity-0 group-hover:opacity-100">wait a second!</button>
@else
<button wire:click="complete({{ $note->id }})" class="bg-blue-600 text-white py-1 px-2 rounded-sm mr-2 opacity-0 group-hover:opacity-100">complete</button>
@endif
@if ($note->deleted_at)
<button wire:click="undelete({{ $note->id }})" class="bg-blue-600 text-white py-1 px-2 rounded-sm opacity-0 group-hover:opacity-100">oh dear...</button>
@else
<button wire:click="delete({{ $note->id }})" class="bg-blue-600 text-white py-1 px-2 rounded-sm opacity-0 group-hover:opacity-100">delete</button>
@endif
</div>
</div>
@endforeach
@foreach (range(0, 2) as $i)
<div
wire:key="{{ $sectionKey . '-' . $subsectionKey . '-' . date('y-m-d-h-i-s-') . $i }}"
class="
@if ($loop->last)
mb-12
@endif
"
>
<span class="flex flex-grow">
<input wire:keydown.enter="save('{{ $sectionKey }}-{{ $subsectionKey }}', $event.target.value)" class="w-full outline-none border-b-2 border-gray-200 active:border-blue-700 mb-1 py-2" />
</span>
</div>
@endforeach
@endforeach
</div>
@endforeach
</div>
<?php
namespace App\Http\Livewire;
use App\Models\Note;
use App\Models\User;
use Livewire\Component;
class Survey extends Component
{
protected $owner;
protected $share;
protected $sections;
protected $notes;
public function mount($owner, $share = null)
{
$this->owner = $owner;
$this->share = $share;
$this->sections = config("grow.sections");
$this->reset();
}
public function reset()
{
$this->notes = [];
foreach ($this->sections as $sectionKey => $subsections) {
foreach ($subsections as $subsectionKey) {
$this->notes["{$sectionKey}-{$subsectionKey}"] = [];
}
}
$user = User::findOrFail($this->owner);
if ($this->share) {
foreach (
$user
->notes()
->where("created_at", ">=", $this->share["share_after"])
->get()
as $note
) {
$this->notes[$note->section][] = $note;
}
} else {
foreach ($user->notes as $note) {
$this->notes[$note->section][] = $note;
}
}
}
public function save($section, $value)
{
Note::create([
"section" => $section,
"content" => $value,
"owner_id" => $this->owner,
"author_id" => auth()->user()->id
]);
$this->reset();
}
public function complete($note)
{
Note::find($note)->update(["completed_at" => now()]);
$this->reset();
}
public function uncomplete($note)
{
Note::find($note)->update(["completed_at" => null]);
$this->reset();
}
public function delete($note)
{
Note::find($note)->update(["deleted_at" => now()]);
$this->reset();
}
public function undelete($note)
{
Note::find($note)->update(["deleted_at" => null]);
$this->reset();
}
public function render()
{
$sections = $this->sections;
$notes = $this->notes;
$share = $this->share;
return view("livewire.survey", compact("sections", "notes", "share"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment