-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b77b7f5
commit 085afc7
Showing
4 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,46 @@ | ||
"use client"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { Input } from "@/components/ui/input"; | ||
import { uploadData } from "@/models/upload.server"; | ||
import { ChangeEvent, useState } from "react"; | ||
|
||
export default function Upload() { | ||
const [selectedFile, setSelectedFile] = useState<File | null>(null); | ||
|
||
const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
const target = event.target as HTMLInputElement; | ||
if (!target.files) return; | ||
const file = target.files[0]; | ||
setSelectedFile(file); | ||
}; | ||
|
||
const handleUpload = async () => { | ||
if (selectedFile) { | ||
const reader = new FileReader(); | ||
|
||
reader.onload = async e => { | ||
const text = e.target?.result; | ||
if (!text) return; | ||
if (typeof text !== "string") return; | ||
try { | ||
const json = JSON.parse(text); | ||
uploadData(json); // Call the uploadData function with the parsed JSON | ||
} catch (error) { | ||
console.error("Error parsing JSON:", error); | ||
} | ||
}; | ||
|
||
reader.readAsText(selectedFile); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Input type="file" onChange={handleFileChange} accept=".json" /> | ||
<Button variant="link" onClick={handleUpload}> | ||
Upload Data | ||
</Button> | ||
</> | ||
); | ||
} |
This file contains 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 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,43 @@ | ||
"use server"; | ||
|
||
import { updateAbout } from "@/models/about.server"; | ||
import { createContact } from "@/models/contact.server"; | ||
import { createExperience } from "@/models/experience.server"; | ||
import { createProject } from "@/models/project.server"; | ||
|
||
export async function uploadData({ | ||
about, | ||
projects, | ||
experiences, | ||
messages, | ||
}: { | ||
about: About; | ||
projects: Project[]; | ||
experiences: Experience[]; | ||
messages: Contact[]; | ||
}) { | ||
// Upload projects | ||
for (const project of projects) { | ||
await createProject({ | ||
...project, | ||
date: new Date(project.date), | ||
}); | ||
} | ||
|
||
// Upload experiences | ||
for (const experience of experiences) { | ||
await createExperience({ | ||
...experience, | ||
startDate: new Date(experience.startDate), | ||
endDate: experience.endDate ? new Date(experience.endDate) : null, | ||
}); | ||
} | ||
|
||
// Upload contacts | ||
for (const contact of messages) { | ||
await createContact(contact); | ||
} | ||
|
||
// Set About data | ||
await updateAbout(about); | ||
} |