Repository navigation
feat(firearms): shot count tracking per firearm (#11) #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8323ea6
feat(tests): document e2e test failure due to local .env clobbering T…
unclesp1d3r d330ffa
refactor(e2e): serve Next in-process instead of a subprocess + shim
unclesp1d3r c4e4ec3
refactor(e2e): serve Next in-process (launcher + learning doc)
unclesp1d3r bdde4aa
feat(range-sessions): add range_session schema, migration, and factory
unclesp1d3r 2501765
feat(range-sessions): add pure session validation (rounds >= 1, valid…
unclesp1d3r 2955092
feat(range-sessions): add visibility-scoped session service + derived…
unclesp1d3r fa31238
feat(range-sessions): add server actions for log/update/delete/list
unclesp1d3r 48bf100
feat(range-sessions): surface lifetime total, log form, and history UI
unclesp1d3r 01d692b
test(range-sessions): e2e for derived total (AE1)
unclesp1d3r f235ac2
fix(range-sessions): close update existence leak + review/simplify fo…
unclesp1d3r 419ec4f
perf(firearms)+test(range-sessions): resolve both deferred review res…
unclesp1d3r 294f7b8
chore(dependencies): update markdownlint-cli2 to version 0.23.0 and j…
unclesp1d3r d16c3e2
chore(dependencies): update next to version 16.2.10 and @types/node t…
unclesp1d3r 8285f1f
fix(range-sessions): address PR review feedback (#34)
unclesp1d3r 9191688
chore(pre-commit): remove sqlfluff hooks to streamline configuration
unclesp1d3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [sqlfluff] | ||
| dialect = postgres |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| "use client"; | ||
|
|
||
| import { useId, useState, useTransition } from "react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Callout } from "@/components/ui/feedback"; | ||
| import { Field } from "@/components/ui/field"; | ||
| import { Input, Textarea } from "@/components/ui/input"; | ||
| import { useToast } from "@/components/ui/toast"; | ||
| import { validateRangeSession } from "@/src/domain/range-sessions/validate"; | ||
| import { firstMessage } from "@/src/domain/validation-messages"; | ||
| import { | ||
| logRangeSessionAction, | ||
| updateRangeSessionAction, | ||
| } from "./session-actions"; | ||
|
|
||
| export interface RangeSessionFormValues { | ||
| id?: string; | ||
| date: string; | ||
| /** Held as a string for the input; parsed to a number on submit. */ | ||
| roundsFired: string; | ||
| notes: string; | ||
| } | ||
|
|
||
| const DATE_CODES = ["emptyDate", "invalidDate"]; | ||
| const ROUNDS_CODES = ["invalidRoundsFired"]; | ||
|
|
||
| /** Today's calendar date (KTD5 default), local time. */ | ||
| function todayIso(): string { | ||
| const now = new Date(); | ||
| const offsetMs = now.getTimezoneOffset() * 60_000; | ||
| return new Date(now.getTime() - offsetMs).toISOString().slice(0, 10); | ||
| } | ||
|
|
||
| interface RangeSessionFormProps { | ||
| firearmId: string; | ||
| initial?: RangeSessionFormValues; | ||
| onDone: () => void; | ||
| onCancel: () => void; | ||
| } | ||
|
|
||
| export function RangeSessionForm({ | ||
| firearmId, | ||
| initial, | ||
| onDone, | ||
| onCancel, | ||
| }: RangeSessionFormProps) { | ||
| const { toast } = useToast(); | ||
| const isEdit = Boolean(initial?.id); | ||
| const [values, setValues] = useState<RangeSessionFormValues>( | ||
| initial ?? { date: todayIso(), roundsFired: "", notes: "" }, | ||
| ); | ||
| const [codes, setCodes] = useState<string[]>([]); | ||
| const [serverError, setServerError] = useState<string | null>(null); | ||
| const [pending, startTransition] = useTransition(); | ||
| const dateId = useId(); | ||
| const roundsId = useId(); | ||
| const notesId = useId(); | ||
|
|
||
| function set<K extends keyof RangeSessionFormValues>(key: K, value: string) { | ||
| setValues((v) => ({ ...v, [key]: value })); | ||
| } | ||
|
|
||
| function submit(event: React.FormEvent<HTMLFormElement>) { | ||
| event.preventDefault(); | ||
| const roundsFired = Number(values.roundsFired); | ||
| const found = validateRangeSession({ | ||
| firearmId, | ||
| date: values.date, | ||
| roundsFired, | ||
| }); | ||
| setCodes(found); | ||
| setServerError(null); | ||
| if (found.length > 0) { | ||
| document | ||
| .getElementById( | ||
| found.includes("invalidRoundsFired") ? roundsId : dateId, | ||
| ) | ||
| ?.focus(); | ||
| return; | ||
| } | ||
| startTransition(async () => { | ||
| const payload = { date: values.date, roundsFired, notes: values.notes }; | ||
| const result = | ||
| isEdit && initial?.id | ||
| ? await updateRangeSessionAction(initial.id, payload) | ||
| : await logRangeSessionAction({ firearmId, ...payload }); | ||
| if (result.ok) { | ||
| toast({ | ||
| message: isEdit ? "Session updated" : "Session logged", | ||
| detail: `${roundsFired} rounds`, | ||
| }); | ||
| onDone(); | ||
| } else if (result.codes) { | ||
| setCodes(result.codes); | ||
| } else { | ||
| setServerError(result.error ?? "Could not save."); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| return ( | ||
| <form onSubmit={submit} className="flex flex-col gap-4" noValidate> | ||
| {serverError ? <Callout tone="danger">{serverError}</Callout> : null} | ||
| <div className="grid gap-4 sm:grid-cols-2"> | ||
| <Field | ||
| label="Date" | ||
| controlId={dateId} | ||
| required | ||
| error={firstMessage(codes, DATE_CODES)} | ||
| > | ||
| <Input | ||
| id={dateId} | ||
| type="date" | ||
| value={values.date} | ||
| onChange={(e) => set("date", e.target.value)} | ||
| aria-invalid={DATE_CODES.some((c) => codes.includes(c))} | ||
| /> | ||
| </Field> | ||
| <Field | ||
| label="Rounds fired" | ||
| controlId={roundsId} | ||
| required | ||
| error={firstMessage(codes, ROUNDS_CODES)} | ||
| > | ||
| <Input | ||
| id={roundsId} | ||
| type="number" | ||
| inputMode="numeric" | ||
| min={1} | ||
| step={1} | ||
| value={values.roundsFired} | ||
| onChange={(e) => set("roundsFired", e.target.value)} | ||
| aria-invalid={codes.includes("invalidRoundsFired")} | ||
| /> | ||
| </Field> | ||
| </div> | ||
| <Field label="Notes" controlId={notesId}> | ||
| <Textarea | ||
| id={notesId} | ||
| value={values.notes} | ||
| onChange={(e) => set("notes", e.target.value)} | ||
| /> | ||
| </Field> | ||
| <div className="flex items-center gap-2"> | ||
| <Button type="submit" disabled={pending}> | ||
| {pending ? "Saving…" : isEdit ? "Save changes" : "Log session"} | ||
| </Button> | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| onClick={onCancel} | ||
| disabled={pending} | ||
| > | ||
| Cancel | ||
| </Button> | ||
| </div> | ||
| </form> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.