-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from CMP26Projects/insert-term-update-term
Insert term update term
- Loading branch information
Showing
7 changed files
with
303 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { useState } from "react"; | ||
import PageTitle from "../common/PageTitle"; | ||
import TextInput from "../common/Inputs"; | ||
import Button from "../common/Button"; | ||
import CustomCheckbox from "../common/CustomCheckbox"; | ||
import "./InsertTermPage.scss"; | ||
import { useInsertTermMutation } from "../../redux/slices/termApiSlice"; | ||
import { toast } from "react-toastify"; | ||
|
||
export default function InsertTermPage() { | ||
const [termName, setTermName] = useState(""); | ||
const [termStartDate, setTermStartDate] = useState(""); | ||
const [termEndDate, setTermEndDate] = useState(""); | ||
const [understandCheckbox, setUnderstandCheckbox] = useState(false); | ||
|
||
const [insertTerm, { isLoading }] = useInsertTermMutation(); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
// TODO: send request with this info to the server | ||
console.log({ | ||
termName, | ||
startDate: termStartDate, | ||
endDate: termEndDate, | ||
understandCheckbox, | ||
}); | ||
try { | ||
const res = await insertTerm({ | ||
termName, | ||
startDate: termStartDate, | ||
endDate: termEndDate, | ||
}).unwrap(); | ||
console.log(res); | ||
if (res.status === 400 || res.status === 500 ) | ||
throw new Error("Something went wrong while inserting the term"); | ||
toast.success("تم إنشاء الفترة بنجاح"); | ||
} catch (err) { | ||
console.log(err); | ||
toast.error("حدث خطأ أثناء إنشاء الفترة"); | ||
toast.error(JSON.stringify(err)); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="insert-term container"> | ||
<PageTitle title="إنشاء فترة " /> | ||
<form className="insert-term__form" onSubmit={handleSubmit}> | ||
<TextInput | ||
label="اسم الفترة الجديدة" | ||
type="text" | ||
name="termName" | ||
value={termName} | ||
onChange={(e) => setTermName(e.target.value)} | ||
placeholder="اسم الفترة" | ||
required | ||
/> | ||
<TextInput | ||
label="تاريخ بداية الفترة" | ||
type="date" | ||
name="termStartDate" | ||
value={termStartDate} | ||
onChange={(e) => setTermStartDate(e.target.value)} | ||
placeholder="تاريخ بداية الفترة" | ||
required | ||
/> | ||
<TextInput | ||
label="تاريخ نهاية الفترة" | ||
type="date" | ||
name="termEndDate" | ||
value={termEndDate} | ||
onChange={(e) => setTermEndDate(e.target.value)} | ||
placeholder="تاريخ نهاية الفترة" | ||
required | ||
/> | ||
<p className="insert-term__note"> | ||
**إنشاء فترة هو إجراء لا رجعة به يجب مراعاة انه بعد إنشاء الفترة | ||
ستتحول كل الصفحات الاخرى الى احدث فترة تم إنشاءها | ||
</p> | ||
<CustomCheckbox | ||
labels={["لقد فهمت"]} | ||
values={["understand"]} | ||
checkedValues={understandCheckbox ? ["understand"] : []} | ||
onChange={(e) => setUnderstandCheckbox(e.target.checked)} | ||
name="understand" | ||
required | ||
/> | ||
<Button | ||
className="insert-term__btn Button--medium Button--primary-darker" | ||
type="submit" | ||
> | ||
إنشاء الفترة | ||
</Button> | ||
</form> | ||
{isLoading && ( | ||
<p | ||
style={{ | ||
textAlign: "center", | ||
}} | ||
> | ||
جاري إنشاء الفترة | ||
</p> | ||
)} | ||
<div className="actions-row"> | ||
<Button | ||
className="insert-term__btn Button--medium Button--danger" | ||
disabled | ||
> | ||
الذهاب للترقيات | ||
</Button> | ||
<Button | ||
className="insert-term__btn Button--medium Button--dark" | ||
disabled | ||
> | ||
العودة للإجراءت | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
.insert-term { | ||
&__form { | ||
display: grid; | ||
grid-template-columns: 1fr 1fr; | ||
gap: 1rem; | ||
margin-block: 1rem; | ||
direction: rtl; | ||
|
||
.input:nth-child(1), | ||
.insert-term__note, | ||
.insert-term__btn { | ||
grid-column: 1 / 3; | ||
} | ||
|
||
.insert-term__note { | ||
direction: rtl; | ||
font-size: 13px; | ||
color: var(--grey-400); | ||
} | ||
} | ||
|
||
.actions-row { | ||
direction: rtl; | ||
display: grid; | ||
grid-template-columns: repeat(2, 1fr); | ||
gap: 1rem; | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
client/src/components/update-term-page/UpdateTermPage.jsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { useEffect, useState } from "react"; | ||
import PageTitle from "../common/PageTitle"; | ||
import TextInput from "../common/Inputs"; | ||
import Button from "../common/Button"; | ||
import CustomCheckbox from "../common/CustomCheckbox"; | ||
import "../insert-term/InsertTermPage.scss"; | ||
import { | ||
useUpdateTermMutation, | ||
useGetCurTermQuery, | ||
} from "../../redux/slices/termApiSlice"; | ||
import { toast } from "react-toastify"; | ||
|
||
export default function UpdateTermPage() { | ||
const [termName, setTermName] = useState(""); | ||
const [termStartDate, setTermStartDate] = useState(""); | ||
const [termEndDate, setTermEndDate] = useState(""); | ||
const [understandCheckbox, setUnderstandCheckbox] = useState(false); | ||
|
||
const [updateTerm, { isLoading }] = useUpdateTermMutation(); | ||
|
||
const { data, isFetching: isFetchingTerm } = useGetCurTermQuery(); | ||
|
||
useEffect(() => { | ||
if (data && data.body) { | ||
setTermName(data.body.termName); | ||
let date = new Date(data.body.startDate).toISOString().split("T")[0]; | ||
setTermStartDate(date); | ||
date = new Date(data.body.endDate).toISOString().split("T")[0]; | ||
setTermEndDate(date); | ||
} | ||
}, [data]); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
// TODO: send request with this info to the server | ||
console.log({ | ||
termName, | ||
startDate: termStartDate, | ||
endDate: termEndDate, | ||
understandCheckbox, | ||
}); | ||
try { | ||
const res = updateTerm({ | ||
termName, | ||
startDate: termStartDate, | ||
endDate: termEndDate, | ||
}).unwrap(); | ||
if (res.status === 400 || res.status === 500) | ||
throw new Error("Something went wrong while updating the term"); | ||
toast.success("تم تعديل الفترة بنجاح"); | ||
} catch (err) { | ||
console.log(err); | ||
toast.error("حدث خطأ أثناء تعديل الفترة"); | ||
toast.error(JSON.stringify(err)); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="insert-term container"> | ||
<PageTitle title="تعديل الفترة" /> | ||
{isFetchingTerm && ( | ||
<p | ||
style={{ | ||
textAlign: "center", | ||
}} | ||
> | ||
جاري تحميل الفترة | ||
</p> | ||
)} | ||
<form className="insert-term__form" onSubmit={handleSubmit}> | ||
<TextInput | ||
label="اسم الفترة" | ||
type="text" | ||
name="termName" | ||
value={termName} | ||
onChange={(e) => setTermName(e.target.value)} | ||
placeholder="اسم الفترة" | ||
required | ||
/> | ||
<TextInput | ||
label="تاريخ بداية الفترة" | ||
type="date" | ||
name="termStartDate" | ||
value={termStartDate} | ||
onChange={(e) => setTermStartDate(e.target.value)} | ||
placeholder="تاريخ بداية الفترة" | ||
required | ||
/> | ||
<TextInput | ||
label="تاريخ نهاية الفترة" | ||
type="date" | ||
name="termEndDate" | ||
value={termEndDate} | ||
onChange={(e) => setTermEndDate(e.target.value)} | ||
placeholder="تاريخ نهاية الفترة" | ||
required | ||
/> | ||
<p className="insert-term__note"> | ||
**إنشاء فترة هو إجراء لا رجعة به يجب مراعاة انه بعد إنشاء الفترة | ||
ستتحول كل الصفحات الاخرى الى احدث فترة تم إنشاءها | ||
</p> | ||
<CustomCheckbox | ||
labels={["لقد فهمت"]} | ||
values={["understand"]} | ||
checkedValues={understandCheckbox ? ["understand"] : []} | ||
onChange={(e) => setUnderstandCheckbox(e.target.checked)} | ||
name="understand" | ||
required | ||
/> | ||
<Button | ||
className="insert-term__btn Button--medium Button--primary-darker" | ||
type="submit" | ||
> | ||
تعديل الفترة | ||
</Button> | ||
</form> | ||
{isLoading && ( | ||
<p | ||
style={{ | ||
textAlign: "center", | ||
}} | ||
> | ||
جاري تعديل الفترة | ||
</p> | ||
)} | ||
</div> | ||
); | ||
} |
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
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