Skip to content

Commit

Permalink
additional-info Form finally working
Browse files Browse the repository at this point in the history
  • Loading branch information
mrswastik-robot committed Apr 18, 2024
1 parent e131eba commit b83308a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,32 @@ type Input = z.infer<typeof EventType>;

type Props = {};

type EventType = {
id: string;
title: string;
description: string;
eventImageURL: string;
dateOfEvent: string;
locationOfEvent: string;
durationOfEvent: number;
registrationLink: string;
uid: string;
createdAt: string;
name: string;
profilePic: string;
sponsors: string[];
};
// type EventType = {
// id: string;
// title: string;
// description: string;
// eventImageURL: string;
// dateOfEvent: string;
// locationOfEvent: string;
// durationOfEvent: number;
// registrationLink: string;
// uid: string;
// createdAt: string;
// name: string;
// profilePic: string;
// sponsors: string[];
// };

const AdditionalForm = (props: Props) => {
const router = useRouter();

const { toast } = useToast();

const form = useForm<Input>({
resolver: zodResolver(EventType),
// resolver: zodResolver(EventType),
defaultValues: {
sponsors: [],
locationOfEvent: "",
sponsors: [],
locationOfEvent: "",
},
});

Expand All @@ -85,19 +85,37 @@ const AdditionalForm = (props: Props) => {
const eventId = useSelector((state: RootState) => state.eventID.event_id);
console.log(eventId);

async function onSubmit(data: Input) {
if (eventId === null) {
console.log("Event ID is null");
return;
}
async function updateEvent( eventId: string,data: Input )
{
const eventRef = doc(db, "events", eventId);

console.log(landmark);
console.log(sponsors);

const eventRef = doc(db, "events", eventId);
// Append the landmark to the location of the event
const locationOfEvent = data.locationOfEvent + ", " + landmark;
console.log(locationOfEvent);

try {
await updateDoc(eventRef, {
sponsors: sponsors,
locationOfEvent: data.locationOfEvent + " " + landmark,
});
// Get the current document
const docSnap = await getDoc(eventRef);

// Check if the sponsors field exists
if (docSnap.exists() && docSnap.data().sponsors) {
// If the sponsors field exists, update it
console.log(docSnap.data().title)
await updateDoc(eventRef, {
sponsors: arrayUnion(...sponsors),
locationOfEvent: docSnap.data().locationOfEvent + ", " + landmark,
});
} else {
// If the sponsors field doesn't exist, set it
await updateDoc(eventRef, {
sponsors: sponsors,
locationOfEvent: docSnap.data().locationOfEvent + ", " + landmark,
});
}

console.log("Update successful");
toast({
title: "Event Updated",
Expand All @@ -106,6 +124,32 @@ const AdditionalForm = (props: Props) => {
} catch (error) {
console.error("Error updating document: ", error);
}

}

function onSubmit(data: Input) {
if (eventId === null) {
toast({
title: "Event ID is null",
description: "Event ID is null. Please try again.",
});
console.log("Event ID is null");
return;
}

// locationOfEvent: data.locationOfEvent + ", " + landmark;

updateEvent(eventId, data);

toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
})

}

return (
Expand All @@ -120,7 +164,6 @@ const AdditionalForm = (props: Props) => {
control={form.control}
name="locationOfEvent"
render={({ field }) => (
<div className=" flex-col space-y-1">
<FormItem>
<FormLabel>Landmark</FormLabel>
<FormControl>
Expand All @@ -133,7 +176,6 @@ const AdditionalForm = (props: Props) => {
{/* <div className="text-[12px] opacity-70">This is the location of the event.</div> */}
<FormMessage />
</FormItem>
</div>
)}
/>

Expand Down Expand Up @@ -197,9 +239,11 @@ const AdditionalForm = (props: Props) => {
</FormItem>
)}
/>

<Button className=" w-full my-4" type="submit">
Update
</Button>

</form>
</Form>
</div>
Expand Down
9 changes: 5 additions & 4 deletions app/homepage2/createEvent/createEvent-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ const CreatEventPage = () => {
description: "",
eventImageURL: "",
// dateOfEvent: 0000/00/00,
// locationOfEvent: "",
locationOfEvent: "",
landmarkLocationOfEvent: "",
// durationOfEvent:,
registrationLink: "",
// sponsors: [],
sponsors: [],
},
});

Expand Down Expand Up @@ -900,8 +900,8 @@ const CreatEventPage = () => {
)}
/>

{/* //spnosors section
<FormField
{/* //spnosors section */}
{/* <FormField
control={form.control}
name="sponsors"
render={({field}) => (
Expand Down Expand Up @@ -950,6 +950,7 @@ const CreatEventPage = () => {
</FormItem>
)}
/> */}

<Button type="submit"
className=" w-full"
// disabled={isGuest === 'true'}
Expand Down

0 comments on commit b83308a

Please sign in to comment.