Skip to content

Commit fe9645b

Browse files
Merge pull request #1196 from adrianvrj/feat-1175
#1175 banner details form
2 parents 41212c7 + 6bd76a8 commit fe9645b

1 file changed

Lines changed: 52 additions & 7 deletions

File tree

components/admin/formSteps/BannerDetailsForm.tsx

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, { FunctionComponent } from "react";
22
import TextInput from "../textInput";
33
import { UpdateQuest } from "../../../types/backTypes";
44
import Button from "@components/UI/button";
5+
import { AdminService } from "@services/authService";
6+
import { useNotification } from "@context/NotificationProvider";
57

68
type BannerDetailsFormProps = {
79
questInput: UpdateQuest;
@@ -13,10 +15,13 @@ type BannerDetailsFormProps = {
1315

1416
const BannerDetailsForm: FunctionComponent<BannerDetailsFormProps> = ({
1517
questInput,
18+
setQuestInput,
1619
handleQuestInputChange,
1720
onSubmit,
1821
submitButtonDisabled,
1922
}) => {
23+
const { showNotification } = useNotification();
24+
2025
return (
2126
<div className="flex flex-col gap-4">
2227
<TextInput
@@ -54,13 +59,53 @@ const BannerDetailsForm: FunctionComponent<BannerDetailsFormProps> = ({
5459
label="Link"
5560
placeholder="Enter Link"
5661
/>
57-
<TextInput
58-
name="banner.image"
59-
value={questInput.banner?.image || ""}
60-
onChange={handleQuestInputChange}
61-
label="Image URL"
62-
placeholder="Enter Image URL"
63-
/>
62+
<div className="flex flex-col gap-5">
63+
<TextInput
64+
name="banner.image"
65+
value={questInput.banner?.image || ""}
66+
onChange={handleQuestInputChange}
67+
label="Image URL"
68+
placeholder="Enter Image URL"
69+
/>
70+
<input
71+
type="file"
72+
name="banner_image_file"
73+
accept=".webp"
74+
className="border border-[#f4faff4d] rounded-lg p-2 w-80"
75+
onChange={async (event) => {
76+
const file = event.target.files?.[0];
77+
if (!file) {
78+
return;
79+
}
80+
if (!file.type.includes('webp')) {
81+
showNotification("Only .webp files are allowed", "error");
82+
return;
83+
}
84+
const imageName = `${questInput.id}_banner`;
85+
const imageUrl = `${process.env.NEXT_PUBLIC_API_LINK}/images/${imageName}`;
86+
try {
87+
await AdminService.uploadImage(file, imageName);
88+
setQuestInput((prev) => ({
89+
...prev,
90+
banner: {
91+
tag: prev.banner?.tag || "",
92+
title: prev.banner?.title || "",
93+
description: prev.banner?.description || "",
94+
cta: prev.banner?.cta || "",
95+
href: prev.banner?.href || "",
96+
image: imageUrl,
97+
},
98+
}));
99+
showNotification("Banner image uploaded successfully", "success");
100+
} catch (err) {
101+
showNotification(
102+
err instanceof Error ? err.message : "Failed to upload banner image",
103+
"error"
104+
);
105+
}
106+
}}
107+
/>
108+
</div>
64109
<div className="w-full sm:w-fit">
65110
<Button onClick={onSubmit} disabled={submitButtonDisabled}>
66111
<p>Save Changes</p>

0 commit comments

Comments
 (0)