Skip to content

Commit 94a4982

Browse files
committed
processing pending state
1 parent 080ce8c commit 94a4982

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

app/Http/Controllers/PuppyController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function like(Request $request, Puppy $puppy)
5151
// ------------------------------
5252
public function store(Request $request)
5353
{
54+
sleep(2);
5455
// Validate the data
5556
$request->validate([
5657
'name' => 'required|string|max:255',

resources/js/components/NewPuppyForm.tsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { useForm } from '@inertiajs/react';
22
import { useRef } from 'react';
3-
import { useFormStatus } from 'react-dom';
43

5-
export function NewPuppyForm() {
6-
const { post, setData, data, errors, reset } = useForm({
4+
export function NewPuppyForm({ mainRef }: { mainRef?: React.RefObject<HTMLElement | null> }) {
5+
const { post, setData, data, errors, reset, processing } = useForm({
76
name: '',
87
trait: '',
98
image: null as File | null,
@@ -22,8 +21,8 @@ export function NewPuppyForm() {
2221
if (fileInputRef.current) {
2322
fileInputRef.current.value = '';
2423
}
25-
if (typeof window !== 'undefined') {
26-
window.scrollTo({ top: 0, behavior: 'smooth' });
24+
if (mainRef?.current) {
25+
mainRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' });
2726
}
2827
},
2928
});
@@ -68,22 +67,15 @@ export function NewPuppyForm() {
6867
{errors.image && <p className="mt-1 text-xs text-red-500">{errors.image}</p>}
6968
</fieldset>
7069
</div>
71-
<SubmitButton />
70+
<button
71+
className="mt-4 inline-block rounded bg-cyan-300 px-4 py-2 font-medium text-cyan-900 hover:bg-cyan-200 focus:ring-2 focus:ring-cyan-500 focus:outline-none disabled:cursor-not-allowed disabled:bg-slate-200"
72+
type="submit"
73+
disabled={processing}
74+
>
75+
{processing ? `Adding ${data.name}...` : 'Add puppy'}
76+
</button>
7277
</form>
7378
</div>
7479
</>
7580
);
7681
}
77-
78-
function SubmitButton() {
79-
const status = useFormStatus();
80-
return (
81-
<button
82-
className="mt-4 inline-block rounded bg-cyan-300 px-4 py-2 font-medium text-cyan-900 hover:bg-cyan-200 focus:ring-2 focus:ring-cyan-500 focus:outline-none disabled:cursor-not-allowed disabled:bg-slate-200"
83-
type="submit"
84-
disabled={status.pending}
85-
>
86-
{status.pending ? `Adding ${status?.data?.get('name') || 'puppy'}...` : 'Add puppy'}
87-
</button>
88-
);
89-
}

resources/js/pages/puppies/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Shortlist } from '@/components/Shortlist';
88

99
import { Filters, PaginatedResponse, Puppy, SharedData } from '@/types';
1010
import { usePage } from '@inertiajs/react';
11+
import { useRef } from 'react';
1112

1213
export default function App({ puppies, filters }: { puppies: PaginatedResponse<Puppy>; filters: Filters }) {
1314
return (
@@ -22,15 +23,16 @@ export default function App({ puppies, filters }: { puppies: PaginatedResponse<P
2223

2324
function Main({ paginatedPuppies, filters }: { paginatedPuppies: PaginatedResponse<Puppy>; filters: Filters }) {
2425
const { auth } = usePage<SharedData>().props;
26+
const mainRef = useRef<HTMLElement>(null);
2527

2628
return (
27-
<main>
29+
<main ref={mainRef} className="scroll-mt-6">
2830
<div className="mt-24 grid gap-8 sm:grid-cols-2">
2931
<Search filters={filters} />
3032
{auth.user && <Shortlist puppies={paginatedPuppies.data} />}
3133
</div>
3234
<PuppiesList puppies={paginatedPuppies} />
33-
{auth.user && <NewPuppyForm />}
35+
{auth.user && <NewPuppyForm mainRef={mainRef} />}
3436
</main>
3537
);
3638
}

0 commit comments

Comments
 (0)