-
-
Save Daltonic/156c78cd0388590e4f583fc94dba1090 to your computer and use it in GitHub Desktop.
Dapp Votes
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
import { formatTimestamp, updatePoll } from '@/services/blockchain' | |
import { globalActions } from '@/store/globalSlices' | |
import { PollParams, PollStruct, RootState } from '@/utils/types' | |
import React, { ChangeEvent, FormEvent, useEffect, useState } from 'react' | |
import { FaTimes } from 'react-icons/fa' | |
import { useDispatch, useSelector } from 'react-redux' | |
import { toast } from 'react-toastify' | |
const UpdatePoll: React.FC<{ pollData: PollStruct }> = ({ pollData }) => { | |
const dispatch = useDispatch() | |
const { setUpdateModal } = globalActions | |
const { wallet, updateModal } = useSelector((states: RootState) => states.globalStates) | |
const [poll, setPoll] = useState<PollParams>({ | |
image: '', | |
title: '', | |
description: '', | |
startsAt: '', | |
endsAt: '', | |
}) | |
useEffect(() => { | |
if (pollData) { | |
const { image, title, description, startsAt, endsAt } = pollData | |
setPoll({ | |
image, | |
title, | |
description, | |
startsAt: formatTimestamp(startsAt), | |
endsAt: formatTimestamp(endsAt), | |
}) | |
} | |
}, [pollData]) | |
const handleUpdate = async (e: FormEvent) => { | |
e.preventDefault() | |
if (!poll.image || !poll.title || !poll.description || !poll.startsAt || !poll.endsAt) return | |
if (wallet === '') return toast.warning('Connect wallet first!') | |
poll.startsAt = new Date(poll.startsAt).getTime() | |
poll.endsAt = new Date(poll.endsAt).getTime() | |
await toast.promise( | |
new Promise<void>((resolve, reject) => { | |
updatePoll(pollData.id, poll) | |
.then((tx) => { | |
closeModal() | |
console.log(tx) | |
resolve(tx) | |
}) | |
.catch((error) => reject(error)) | |
}), | |
{ | |
pending: 'Approve transaction...', | |
success: 'Poll updated successfully 👌', | |
error: 'Encountered error 🤯', | |
} | |
) | |
} | |
const handleChange = (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { | |
const { name, value } = e.target | |
setPoll((prevState) => ({ | |
...prevState, | |
[name]: value, | |
})) | |
} | |
const closeModal = () => { | |
dispatch(setUpdateModal('scale-0')) | |
} | |
return ( | |
<div | |
className={`fixed top-0 left-0 w-screen h-screen flex items-center justify-center | |
bg-black bg-opacity-50 transform z-50 transition-transform duration-300 ${updateModal}`} | |
> | |
<div className="bg-[#0c0c10] text-[#BBBBBB] shadow-lg shadow-[#1B5CFE] rounded-xl w-11/12 md:w-2/5 h-7/12 p-6"> | |
<div className="flex flex-col"> | |
<div className="flex flex-row justify-between items-center"> | |
<p className="font-semibold">Edit Poll</p> | |
<button onClick={closeModal} className="border-0 bg-transparent focus:outline-none"> | |
<FaTimes /> | |
</button> | |
</div> | |
<form | |
onSubmit={handleUpdate} | |
className="flex flex-col justify-center items-start rounded-xl mt-5 mb-5" | |
> | |
<div className="py-4 w-full border border-[#212D4A] rounded-full flex items-center px-4 mb-3 mt-2"> | |
<input | |
placeholder="Poll Title" | |
className="bg-transparent outline-none w-full placeholder-[#929292] text-sm" | |
name="title" | |
value={poll.title} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<div | |
className="py-4 w-full border border-[#212D4A] rounded-full | |
flex items-center px-4 mb-3 mt-2 space-x-2 relative" | |
> | |
<span | |
className="bg-[#1B5CFE] bg-opacity-20 text-[#4C6AD7] | |
absolute left-[2.5px] py-3 rounded-full px-5 w-48" | |
> | |
<span className="text-transparent">.</span> | |
</span> | |
<input | |
className="bg-transparent outline-none w-full placeholder-transparent text-sm" | |
name="startsAt" | |
type="datetime-local" | |
placeholder="Start Date" | |
value={poll.startsAt} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<div | |
className="py-4 w-full border border-[#212D4A] rounded-full | |
flex items-center px-4 mb-3 mt-2 space-x-2 relative" | |
> | |
<span | |
className="bg-[#1B5CFE] bg-opacity-20 text-[#4C6AD7] | |
absolute left-[2.5px] py-3 rounded-full px-5 w-48" | |
> | |
<span className="text-transparent">.</span> | |
</span> | |
<input | |
className="bg-transparent outline-none w-full placeholder-[#929292] text-sm" | |
name="endsAt" | |
type="datetime-local" | |
value={poll.endsAt} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<div className="py-4 w-full border border-[#212D4A] rounded-full flex items-center px-4 mb-3 mt-2"> | |
<input | |
placeholder="Banner URL" | |
type="url" | |
className="bg-transparent outline-none w-full placeholder-[#929292] text-sm" | |
name="image" | |
accept="image/*" | |
value={poll.image} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<div className="py-4 w-full border border-[#212D4A] rounded-xl flex items-center px-4 h-20 mt-2"> | |
<textarea | |
placeholder="Poll Description" | |
className="bg-transparent outline-none w-full placeholder-[#929292] text-sm" | |
name="description" | |
value={poll.description} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<button | |
className="h-[48px] w-full block mt-2 px-3 rounded-full text-sm font-bold | |
transition-all duration-300 bg-[#1B5CFE] hover:bg-blue-500" | |
> | |
Update Poll | |
</button> | |
</form> | |
</div> | |
</div> | |
</div> | |
) | |
} | |
export default UpdatePoll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment