Spaces:
Sleeping
Sleeping
| import { farmApplications, FarmApplication, User } from "@/data/dummyData"; | |
| const LOCAL_KEY = "farm_local_applications"; | |
| function readLocal(): FarmApplication[] { | |
| if (typeof window === "undefined") return []; | |
| try { | |
| const raw = localStorage.getItem(LOCAL_KEY); | |
| if (!raw) return []; | |
| const parsed = JSON.parse(raw); | |
| if (!Array.isArray(parsed)) return []; | |
| return parsed as FarmApplication[]; | |
| } catch { | |
| return []; | |
| } | |
| } | |
| function writeLocal(apps: FarmApplication[]) { | |
| if (typeof window === "undefined") return; | |
| try { | |
| localStorage.setItem(LOCAL_KEY, JSON.stringify(apps)); | |
| } catch { | |
| // ignore | |
| } | |
| } | |
| export function getUserFarmApplications(user?: User | null): FarmApplication[] { | |
| if (!user) return []; | |
| const base = farmApplications.filter( | |
| (app) => app.email.toLowerCase() === user.email.toLowerCase(), | |
| ); | |
| const local = readLocal().filter( | |
| (app) => app.email.toLowerCase() === user.email.toLowerCase(), | |
| ); | |
| return [...base, ...local].sort( | |
| (a, b) => new Date(b.submittedDate).getTime() - new Date(a.submittedDate).getTime(), | |
| ); | |
| } | |
| export function getFarmApplicationById(id: string): FarmApplication | undefined { | |
| const base = farmApplications.find((app) => app.id === id); | |
| if (base) return base; | |
| const local = readLocal().find((app) => app.id === id); | |
| return local; | |
| } | |
| export interface NewFarmApplicationInput { | |
| farmName: string; | |
| farmerName: string; | |
| waterArea: string; | |
| species: string; | |
| state: string; | |
| district: string; | |
| address: string; | |
| enrollmentId: string; | |
| email: string; | |
| phone: string; | |
| } | |
| export function createFarmApplicationForUser( | |
| user: User, | |
| data: NewFarmApplicationInput, | |
| ): FarmApplication { | |
| const now = new Date(); | |
| const id = `FL${now.getTime()}`; | |
| const applicationNo = `SHAP/F/${now.getFullYear()}/${now | |
| .getTime() | |
| .toString() | |
| .slice(-4)}`; | |
| const app: FarmApplication = { | |
| id, | |
| applicationNo, | |
| farmName: data.farmName, | |
| farmerName: data.farmerName, | |
| enrollmentId: data.enrollmentId, | |
| email: user.email, | |
| phone: data.phone, | |
| state: data.state, | |
| district: data.district, | |
| address: data.address, | |
| waterArea: data.waterArea, | |
| species: data.species, | |
| status: "submitted", | |
| currentStage: "submission", | |
| submittedDate: now.toISOString().slice(0, 10), | |
| assignedFOI: undefined, | |
| assignedFO: undefined, | |
| assignedCCOfficer: undefined, | |
| assignedAuditor: undefined, | |
| paymentStatus: "pending", | |
| paymentAmount: 0, | |
| audits: [], | |
| certificates: [], | |
| cropDetails: [], | |
| }; | |
| const all = readLocal(); | |
| writeLocal([...all, app]); | |
| return app; | |
| } | |