Skip to content

Commit

Permalink
UP
Browse files Browse the repository at this point in the history
  • Loading branch information
RafsanAmin committed Oct 11, 2024
1 parent 1fba7ff commit f835648
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 29 deletions.
29 changes: 21 additions & 8 deletions src/app/club/Components/Admin/AddQuestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ const AddQuestions = ({
questionsData,
answersData,
setData,
setQues,
setAns,
}: {
questionsData: any[];
answersData: any[];
setData: (questionData: any, answerData: any) => void;
setQues?: (s: any) => void;
setAns?: (s: any) => void;
}) => {
const [questions, setQuestions] =
useState<questionInterface[]>(questionsData);
const [answers, setAnswers] = useState<answerInterface[]>(answersData);
const [questions, setQuestions] = useState<questionInterface[]>([]);
const [answers, setAnswers] = useState<answerInterface[]>([]);

useEffect(() => {
setAnswers(answersData);
Expand All @@ -68,7 +71,6 @@ const AddQuestions = ({

const [point, setPoint] = useState(1);


const setOption = (i: number, value: string) => {
switch (i) {
case 0:
Expand All @@ -94,7 +96,7 @@ const AddQuestions = ({

const addQuestion = () => {
setQuestions((oldValue: any) => {
return [
let sqd = [
...oldValue,
{
question: question,
Expand All @@ -106,10 +108,12 @@ const AddQuestions = ({
option3: option3,
},
];
setQues && setQues(sqd);
return sqd;
});

setAnswers((oldValue: any) => {
return [
let sad = [
...oldValue,
{
mcq: isMCQ,
Expand All @@ -118,6 +122,10 @@ const AddQuestions = ({
correctAnswers: correctAnswers,
},
];

setAns && setAns(sad);

return sad;
});

setQuestion("");
Expand Down Expand Up @@ -173,7 +181,12 @@ const AddQuestions = ({
};

useEffect(() => {
setData(questions, answers);
console.log("-------------");

console.log("qb", questions);
console.log("ab", answers);

console.log("-------------");
}, [questions, answers]);

return (
Expand All @@ -198,7 +211,7 @@ const AddQuestions = ({
correctOption={answers[i].correctOption}
correctAnswers={answers[i].correctAnswers}
index={i}
key={i}
key={i + Math.random()}
onValueChange={onValueChange}
deleteQuestion={deleteQuestion}
/>
Expand Down
99 changes: 78 additions & 21 deletions src/app/club/admin/eventEdit/[eventID]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const Page = ({ params }: { params: { eventID: string } }) => {
setAdminAuth(false);
setAuthLoading(false);
}

console.log("loaded");
if (params.eventID != "new") {
const event = getDoc(doc(db, "events", params.eventID)).then((event) => {
if (event.exists()) {
Expand Down Expand Up @@ -154,7 +154,7 @@ const Page = ({ params }: { params: { eventID: string } }) => {
} else {
setNotfound(false);
}
}, [user]);
}, [params.eventID, user]);

const handleSubmit = async (event: any) => {
event.preventDefault();
Expand Down Expand Up @@ -288,6 +288,33 @@ const Page = ({ params }: { params: { eventID: string } }) => {
});
};

const savelocal = async (event: any) => {
localStorage.setItem(
`event.${eventUID}`,
JSON.stringify({
questions,
answers,
}),
);
toast.info("Saved questions and answers.");
goToAdminPanel();
};

const loadLocal = async () => {
const local = JSON.parse(localStorage.getItem(`event.${eventUID}`) || "{}");
console.dir(local);
setData(local?.questions, local?.answers);
toast.info("Loaded local questions and answers.");
};

useEffect(() => {
console.log("-------------");

console.log("q", questions);
console.log("a", answers);

console.log("-------------");
}, [questions, answers]);
const [notfound, setNotfound] = useState(false);

const [changeImage, setChangeImage] = useState<boolean>();
Expand Down Expand Up @@ -595,6 +622,8 @@ const Page = ({ params }: { params: { eventID: string } }) => {
questionsData={questions}
answersData={answers}
setData={setData}
setQues={(s) => setQuestions(s)}
setAns={(s) => setAnswers(s)}
/>

<Modal state={deleteModalOpen}>
Expand Down Expand Up @@ -651,26 +680,54 @@ const Page = ({ params }: { params: { eventID: string } }) => {
)}
</div>

<div className="flex gap-5">
<div className="flex flex-wrap gap-5">
{params.eventID != "new" && (
<div className="w-full justify-self-end py-3 md:w-auto md:py-0">
<button
style={{
pointerEvents: loading ? "none" : "auto",
}}
className="w-full rounded-xl bg-red-600 px-8 py-2 text-lg text-white transition-all hover:bg-red-500 hover:text-red-800"
onClick={deleteWarning}
>
{loading ? (
<CgSpinner className="mx-auto h-7 w-7 animate-spin text-white" />
) : (
<div className="flex items-center justify-center gap-3">
Delete
<RiDeleteBin6Line />
</div>
)}
</button>
</div>
<>
<div className="w-full justify-self-end py-3 md:w-auto md:py-0">
<button
style={{
pointerEvents: loading ? "none" : "auto",
}}
className="w-full rounded-xl bg-red-600 px-8 py-2 text-lg text-white transition-all hover:bg-red-500 hover:text-red-800"
onClick={deleteWarning}
>
{loading ? (
<CgSpinner className="mx-auto h-7 w-7 animate-spin text-white" />
) : (
<div className="flex items-center justify-center gap-3">
Delete
<RiDeleteBin6Line />
</div>
)}
</button>
</div>
<div className="w-full justify-self-end py-3 md:w-auto md:py-0">
<button
className="w-full rounded-xl bg-primary px-8 py-2 text-lg text-white transition-all hover:bg-secondary_light hover:text-primary"
type="button"
onClick={savelocal}
>
{loading ? (
<CgSpinner className="mx-auto h-7 w-7 animate-spin text-white" />
) : (
"Save Questions Locally"
)}
</button>
</div>
<div className="w-full justify-self-end py-3 md:w-auto md:py-0">
<button
className="w-full rounded-xl bg-primary px-8 py-2 text-lg text-white transition-all hover:bg-secondary_light hover:text-primary"
type="button"
onClick={loadLocal}
>
{loading ? (
<CgSpinner className="mx-auto h-7 w-7 animate-spin text-white" />
) : (
"Load Local Questions"
)}
</button>
</div>
</>
)}

<div className="w-full justify-self-end py-3 md:w-auto md:py-0">
Expand Down

0 comments on commit f835648

Please sign in to comment.