Week 004 — Refactoring Already
Less than a month in and already in the middle of a refactor. Apparently this is just what building software looks like.
Week of October 21, 2024 refactor, decision Apparently this is just what building software looks like.
2024 Apparently this is just what building software looks like.
Progress
Renamed and restructured additem.php into item.php — a unified page for viewing, editing, deleting, and reporting an item lost. Navigation now makes sense: click an item in the list, land on its page, do whatever needs doing from there. Added an Add Item button to the list page, clear-after-save behavior, and default sort by name instead of ID.
Also added two new fields to the item model: last_modified and current_status. Small additions, but current_status is the foundation of what makes srchParty useful — an item needs to be able to be safe, lost, or found.
The Hard Part
Updating an existing item when that update includes a changed image turned out to be genuinely difficult — not algorithmically, but structurally.
The root problem: $_FILES in PHP always evaluates as true as long as the file input field exists in the HTML form, even when the user hasn't selected a new image. There's no reliable built-in way to detect "did the image actually change?" using the standard form-submit approach.
Working around this produced increasingly tangled conditional logic. Every attempt at a fix introduced a new edge case. Eventually the right call was to stop patching and restructure: break image processing into its own function, separate new-item and update-item paths, and switch from a traditional form submit to a JS fetch() call with a manually constructed FormData object. With fetch(), the payload is built explicitly — you include the new image only when one was actually selected.
This felt like a step backward in the short term. More work for something that was partially working. But the alternative was a growing pile of conditionals that would become impossible to follow.
Decision Made
Use fetch() with manually constructed FormData for all item saves going forward, both new items and updates. The extra upfront work buys consistency: one approach handles all cases, error handling can be standardized across the board, and there's no hidden form behavior to account for.
Next Week
Finish the fetch refactor, get image updates working cleanly, and push forward on item state tracking — the piece that makes an item actually meaningful in srchParty.
