Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert sector page #37

Merged
merged 6 commits into from
Dec 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
⚛️ feat: insert sector complete
  • Loading branch information
amir-kedis committed Dec 26, 2023
commit 5fee4de3af5e364f7676d83437e7a413643f6bcc
51 changes: 50 additions & 1 deletion client/src/components/insert-sector/InsertSector.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import Button from "../common/Button";
import PageTitle from "../common/PageTitle";
import TextInput from "../common/Inputs";
import "./InsertSector.scss";
import { useInsertSectorMutation } from "../../redux/slices/sectorApiSlice";
import { toast } from "react-toastify";
import { useGetCaptainsQuery } from "../../redux/slices/captainsApiSlice";
import CustomSelect from "../common/CustomSelect";

export default function InsertSector() {
const [sectorBaseName, setSectorBaseName] = useState("");
@@ -14,6 +16,23 @@ export default function InsertSector() {
const [insertSector, { isLoading: isLoadingInsertSector }] =
useInsertSectorMutation();

let unitCaptains = [];

const { data, isFetching } = useGetCaptainsQuery();

if (data && !isFetching) {
unitCaptains = data.body.filter((captain) => captain.type === "unit");
if (unitCaptains.length === 0) {
unitCaptains = [{ captainId: "", firstName: "لا يوجد قادة" }];
}
}

useEffect(() => {
if (unitCaptains.length === 1) {
setUnitSectorLeader(unitCaptains[0].captainId);
}
}, [unitCaptains]);

const handleSubmit = async (e) => {
e.preventDefault();
const newSector = {
@@ -59,10 +78,40 @@ export default function InsertSector() {
placeholder="مثل: أ, ب, ج"
required
/>
<CustomSelect
data={unitCaptains}
label="قائد القطاع"
name="unitSectorLeader"
selectedValue={unitSectorLeader}
onChange={(e) => {
setUnitSectorLeader(e.target.value);
}}
required
displayMember="firstName"
valueMember="captainId"
/>
{isFetching && (
<p
style={{
direction: "rtl",
}}
>
جاري التحميل
</p>
)}
</div>
<Button className="insert-sector__btn Button--medium Button--primary-darker">
إنشاء
</Button>
{isLoadingInsertSector && (
<p
style={{
direction: "rtl",
}}
>
جاري الإنشاء
</p>
)}
</form>
</div>
);
8 changes: 7 additions & 1 deletion client/src/redux/slices/captainsApiSlice.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,13 @@ export const captainsApi = apiSlice.injectEndpoints({
method: "GET",
}),
}),
GetUnitCaptains: builder.query({
query: () => ({
url: `${CAPTAINS_URL}/`,
method: "GET",
}),
}),
}),
});

export const { useGetCaptainsQuery } = captainsApi;
export const { useGetCaptainsQuery, useGetUnitCaptainsQuery } = captainsApi;