Skip to content

Commit 085afc7

Browse files
committed
feat: save/upload data
1 parent b77b7f5 commit 085afc7

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

src/app/(admin)/admin/Download.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { getAllProjects } from "@/models/project.server";
88

99
export default function Download() {
1010
const getData = async () => {
11-
const [about, projects, experience, messages] = await Promise.all([
11+
const [about, projects, experiences, messages] = await Promise.all([
1212
await getAbout(),
1313
await getAllProjects(),
1414
await getAllExperiences(),
1515
await getAllMessages(),
1616
]);
17-
return { about, projects, experience, messages };
17+
return { about, projects, experiences, messages };
1818
};
1919

2020
const saveData = async () => {

src/app/(admin)/admin/Upload.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use client";
2+
3+
import { Button } from "@/components/ui/button";
4+
import { Input } from "@/components/ui/input";
5+
import { uploadData } from "@/models/upload.server";
6+
import { ChangeEvent, useState } from "react";
7+
8+
export default function Upload() {
9+
const [selectedFile, setSelectedFile] = useState<File | null>(null);
10+
11+
const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
12+
const target = event.target as HTMLInputElement;
13+
if (!target.files) return;
14+
const file = target.files[0];
15+
setSelectedFile(file);
16+
};
17+
18+
const handleUpload = async () => {
19+
if (selectedFile) {
20+
const reader = new FileReader();
21+
22+
reader.onload = async e => {
23+
const text = e.target?.result;
24+
if (!text) return;
25+
if (typeof text !== "string") return;
26+
try {
27+
const json = JSON.parse(text);
28+
uploadData(json); // Call the uploadData function with the parsed JSON
29+
} catch (error) {
30+
console.error("Error parsing JSON:", error);
31+
}
32+
};
33+
34+
reader.readAsText(selectedFile);
35+
}
36+
};
37+
38+
return (
39+
<>
40+
<Input type="file" onChange={handleFileChange} accept=".json" />
41+
<Button variant="link" onClick={handleUpload}>
42+
Upload Data
43+
</Button>
44+
</>
45+
);
46+
}

src/app/(admin)/admin/layout.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Header from "./Header";
22
import NavItem from "./NavItem";
33
import Download from "./Download";
4+
import Upload from "./Upload";
45

56
export const dynamic = "force-dynamic";
67
export const revalidate = 0;
@@ -25,7 +26,10 @@ export default function Layout({ children }: { children: React.ReactNode }) {
2526
<div className="h-px bg-border my-2 mx-4"></div>
2627
<NavItem to="/admin/messages">Messages</NavItem>
2728
</div>
28-
<Download />
29+
<div className="flex flex-col gap-2">
30+
<Download />
31+
<Upload />
32+
</div>
2933
</div>
3034
{children}
3135
</div>

src/models/upload.server.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use server";
2+
3+
import { updateAbout } from "@/models/about.server";
4+
import { createContact } from "@/models/contact.server";
5+
import { createExperience } from "@/models/experience.server";
6+
import { createProject } from "@/models/project.server";
7+
8+
export async function uploadData({
9+
about,
10+
projects,
11+
experiences,
12+
messages,
13+
}: {
14+
about: About;
15+
projects: Project[];
16+
experiences: Experience[];
17+
messages: Contact[];
18+
}) {
19+
// Upload projects
20+
for (const project of projects) {
21+
await createProject({
22+
...project,
23+
date: new Date(project.date),
24+
});
25+
}
26+
27+
// Upload experiences
28+
for (const experience of experiences) {
29+
await createExperience({
30+
...experience,
31+
startDate: new Date(experience.startDate),
32+
endDate: experience.endDate ? new Date(experience.endDate) : null,
33+
});
34+
}
35+
36+
// Upload contacts
37+
for (const contact of messages) {
38+
await createContact(contact);
39+
}
40+
41+
// Set About data
42+
await updateAbout(about);
43+
}

0 commit comments

Comments
 (0)