added default units

This commit is contained in:
John Overton
2025-03-08 20:29:44 -06:00
parent 6ad7990862
commit eaf3b8d12f
5 changed files with 207 additions and 9 deletions

View File

@@ -13,6 +13,11 @@ export async function GET() {
data: {
familyName: 'My Family', // Default family name
timezone: 'America/Chicago', // Default timezone
defaultBottleUnit: 'OZ',
defaultSolidsUnit: 'TBSP',
defaultHeightUnit: 'IN',
defaultWeightUnit: 'LB',
defaultTempUnit: 'F',
},
});
}
@@ -42,6 +47,11 @@ export async function POST(req: NextRequest) {
familyName: body.familyName,
timezone: body.timezone,
securityPin: body.securityPin,
defaultBottleUnit: body.defaultBottleUnit,
defaultSolidsUnit: body.defaultSolidsUnit,
defaultHeightUnit: body.defaultHeightUnit,
defaultWeightUnit: body.defaultWeightUnit,
defaultTempUnit: body.defaultTempUnit,
},
});
@@ -84,6 +94,11 @@ export async function PUT(req: NextRequest) {
familyName: body.familyName,
timezone: body.timezone,
securityPin: body.securityPin,
defaultBottleUnit: body.defaultBottleUnit,
defaultSolidsUnit: body.defaultSolidsUnit,
defaultHeightUnit: body.defaultHeightUnit,
defaultWeightUnit: body.defaultWeightUnit,
defaultTempUnit: body.defaultTempUnit,
},
});

28
app/api/units/route.ts Normal file
View File

@@ -0,0 +1,28 @@
import { NextResponse } from 'next/server';
import prisma from '../db';
import { ApiResponse } from '../types';
import { Unit } from '@prisma/client';
export async function GET() {
try {
const units = await prisma.unit.findMany({
orderBy: {
unitName: 'asc',
},
});
return NextResponse.json<ApiResponse<Unit[]>>({
success: true,
data: units,
});
} catch (error) {
console.error('Error fetching units:', error);
return NextResponse.json<ApiResponse<Unit[]>>(
{
success: false,
error: 'Failed to fetch units',
},
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,21 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Settings" (
"id" TEXT NOT NULL PRIMARY KEY,
"familyName" TEXT NOT NULL DEFAULT 'My Family',
"timezone" TEXT NOT NULL DEFAULT 'America/Chicago',
"securityPin" TEXT NOT NULL DEFAULT '111222',
"defaultBottleUnit" TEXT NOT NULL DEFAULT 'OZ',
"defaultSolidsUnit" TEXT NOT NULL DEFAULT 'TBSP',
"defaultHeightUnit" TEXT NOT NULL DEFAULT 'IN',
"defaultWeightUnit" TEXT NOT NULL DEFAULT 'LB',
"defaultTempUnit" TEXT NOT NULL DEFAULT 'F',
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_Settings" ("createdAt", "familyName", "id", "securityPin", "timezone", "updatedAt") SELECT "createdAt", "familyName", "id", "securityPin", "timezone", "updatedAt" FROM "Settings";
DROP TABLE "Settings";
ALTER TABLE "new_Settings" RENAME TO "Settings";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@@ -222,10 +222,15 @@ model Note {
}
model Settings {
id String @id @default(uuid())
familyName String @default("My Family")
timezone String @default("America/Chicago")
securityPin String @default("111222")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
id String @id @default(uuid())
familyName String @default("My Family")
timezone String @default("America/Chicago")
securityPin String @default("111222")
defaultBottleUnit String @default("OZ") // Default unit for bottle feeding
defaultSolidsUnit String @default("TBSP") // Default unit for solid feeding
defaultHeightUnit String @default("IN") // Default unit for height measurement
defaultWeightUnit String @default("LB") // Default unit for weight measurement
defaultTempUnit String @default("F") // Default unit for temperature
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

View File

@@ -1,7 +1,7 @@
'use client';
import React, { useEffect, useState } from 'react';
import { Baby } from '@prisma/client';
import { Baby, Unit } from '@prisma/client';
import { Settings } from '@/app/api/types';
import { Settings as Plus, Edit, Download, Upload } from 'lucide-react';
import { Button } from '@/src/components/ui/button';
@@ -46,6 +46,7 @@ export default function SettingsForm({
const [localSelectedBabyId, setLocalSelectedBabyId] = useState<string | undefined>(selectedBabyId);
const [showChangePinModal, setShowChangePinModal] = useState(false);
const [isRestoring, setIsRestoring] = useState(false);
const [units, setUnits] = useState<Unit[]>([]);
const fileInputRef = React.useRef<HTMLInputElement>(null);
useEffect(() => {
@@ -55,9 +56,10 @@ export default function SettingsForm({
const fetchData = async () => {
try {
setLoading(true);
const [settingsResponse, babiesResponse] = await Promise.all([
const [settingsResponse, babiesResponse, unitsResponse] = await Promise.all([
fetch('/api/settings'),
fetch('/api/baby')
fetch('/api/baby'),
fetch('/api/units')
]);
if (settingsResponse.ok) {
@@ -69,6 +71,11 @@ export default function SettingsForm({
const babiesData = await babiesResponse.json();
setBabies(babiesData.data);
}
if (unitsResponse.ok) {
const unitsData = await unitsResponse.json();
setUnits(unitsData.data);
}
} catch (error) {
console.error('Error fetching data:', error);
} finally {
@@ -283,6 +290,128 @@ export default function SettingsForm({
</div>
</div>
</div>
<div className="border-t border-slate-200 pt-6">
<h3 className="form-label mb-4">Default Units</h3>
<div className="space-y-4">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{/* Bottle Feeding Unit */}
<div>
<Label className="form-label">Bottle Feeding</Label>
<Select
value={settings?.defaultBottleUnit || 'OZ'}
onValueChange={(value) => handleSettingsChange({ defaultBottleUnit: value })}
disabled={loading}
>
<SelectTrigger>
<SelectValue placeholder="Select unit" />
</SelectTrigger>
<SelectContent>
{units
.filter(unit => ['OZ', 'ML'].includes(unit.unitAbbr))
.map((unit) => (
<SelectItem key={unit.unitAbbr} value={unit.unitAbbr}>
{unit.unitName} ({unit.unitAbbr})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Solid Feeding Unit */}
<div>
<Label className="form-label">Solid Feeding</Label>
<Select
value={settings?.defaultSolidsUnit || 'TBSP'}
onValueChange={(value) => handleSettingsChange({ defaultSolidsUnit: value })}
disabled={loading}
>
<SelectTrigger>
<SelectValue placeholder="Select unit" />
</SelectTrigger>
<SelectContent>
{units
.filter(unit => ['TBSP', 'G'].includes(unit.unitAbbr))
.map((unit) => (
<SelectItem key={unit.unitAbbr} value={unit.unitAbbr}>
{unit.unitName} ({unit.unitAbbr})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Height Unit */}
<div>
<Label className="form-label">Height</Label>
<Select
value={settings?.defaultHeightUnit || 'IN'}
onValueChange={(value) => handleSettingsChange({ defaultHeightUnit: value })}
disabled={loading}
>
<SelectTrigger>
<SelectValue placeholder="Select unit" />
</SelectTrigger>
<SelectContent>
{units
.filter(unit => ['IN', 'CM'].includes(unit.unitAbbr))
.map((unit) => (
<SelectItem key={unit.unitAbbr} value={unit.unitAbbr}>
{unit.unitName} ({unit.unitAbbr})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Weight Unit */}
<div>
<Label className="form-label">Weight</Label>
<Select
value={settings?.defaultWeightUnit || 'LB'}
onValueChange={(value) => handleSettingsChange({ defaultWeightUnit: value })}
disabled={loading}
>
<SelectTrigger>
<SelectValue placeholder="Select unit" />
</SelectTrigger>
<SelectContent>
{units
.filter(unit => ['LB', 'KG', 'G'].includes(unit.unitAbbr))
.map((unit) => (
<SelectItem key={unit.unitAbbr} value={unit.unitAbbr}>
{unit.unitName} ({unit.unitAbbr})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Temperature Unit */}
<div>
<Label className="form-label">Temperature</Label>
<Select
value={settings?.defaultTempUnit || 'F'}
onValueChange={(value) => handleSettingsChange({ defaultTempUnit: value })}
disabled={loading}
>
<SelectTrigger>
<SelectValue placeholder="Select unit" />
</SelectTrigger>
<SelectContent>
{units
.filter(unit => ['F', 'C'].includes(unit.unitAbbr))
.map((unit) => (
<SelectItem key={unit.unitAbbr} value={unit.unitAbbr}>
{unit.unitName} ({unit.unitAbbr})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
</div>
</div>
</div>
</FormPageContent>