added update job
This commit is contained in:
parent
d6ef713370
commit
9b65e0a320
@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import Breadcrumb from "../common/Breadcrumb";
|
import Breadcrumb from "../common/Breadcrumb";
|
||||||
import Label from "../common/Label";
|
import Label from "../common/Label";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
@ -8,6 +8,7 @@ import {
|
|||||||
useJobTags,
|
useJobTags,
|
||||||
useServiceProjectJobDetails,
|
useServiceProjectJobDetails,
|
||||||
useServiceProjects,
|
useServiceProjects,
|
||||||
|
useUpdateServiceProjectJob,
|
||||||
} from "../../hooks/useServiceProject";
|
} from "../../hooks/useServiceProject";
|
||||||
import { ITEMS_PER_PAGE } from "../../utils/constants";
|
import { ITEMS_PER_PAGE } from "../../utils/constants";
|
||||||
import DatePicker from "../common/DatePicker";
|
import DatePicker from "../common/DatePicker";
|
||||||
@ -21,9 +22,11 @@ import {
|
|||||||
useAppForm,
|
useAppForm,
|
||||||
} from "../../hooks/appHooks/useAppForm";
|
} from "../../hooks/appHooks/useAppForm";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
import { useDispatch } from "react-redux";
|
||||||
|
|
||||||
const ManageJob = ({ Job }) => {
|
const ManageJob = ({ Job }) => {
|
||||||
const { projectId } = useParams();
|
const { projectId } = useParams();
|
||||||
|
const dispatch = useDispatch()
|
||||||
const methods = useAppForm({
|
const methods = useAppForm({
|
||||||
resolver: zodResolver(jobSchema),
|
resolver: zodResolver(jobSchema),
|
||||||
defaultValues: defaultJobValue,
|
defaultValues: defaultJobValue,
|
||||||
@ -52,10 +55,26 @@ const ManageJob = ({ Job }) => {
|
|||||||
const { mutate: CreateJob, isPending } = useCreateServiceProjectJob(() => {
|
const { mutate: CreateJob, isPending } = useCreateServiceProjectJob(() => {
|
||||||
reset();
|
reset();
|
||||||
});
|
});
|
||||||
|
const { mutate: UpdateJob, isPending: Updating } = useUpdateServiceProjectJob(
|
||||||
|
() => { dispatch()}
|
||||||
|
);
|
||||||
const onSubmit = (formData) => {
|
const onSubmit = (formData) => {
|
||||||
if (Job) {
|
if (Job) {
|
||||||
|
const existingEmployeeIds = JobData?.assignees?.map((e) => e.id) || [];
|
||||||
|
|
||||||
|
const oldEmployees = JobData.assignees.map((e) => ({
|
||||||
|
employeeId: e.id,
|
||||||
|
isActive: formData.assignees.includes(e.id),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const newEmployees = formData.assignees
|
||||||
|
.filter((id) => !existingEmployeeIds.includes(id))
|
||||||
|
.map((id) => ({
|
||||||
|
employeeId: id,
|
||||||
|
isActive: true,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const updatedEmployees = [...oldEmployees, ...newEmployees];
|
||||||
|
|
||||||
const payload = [
|
const payload = [
|
||||||
{
|
{
|
||||||
@ -83,7 +102,14 @@ const ManageJob = ({ Job }) => {
|
|||||||
path: "/tags",
|
path: "/tags",
|
||||||
value: formData.tags,
|
value: formData.tags,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
op: "replace",
|
||||||
|
path: "/assignees",
|
||||||
|
value: updatedEmployees,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
UpdateJob({ id: Job, payload });
|
||||||
} else {
|
} else {
|
||||||
formData.assignees = formData.assignees.map((emp) => ({
|
formData.assignees = formData.assignees.map((emp) => ({
|
||||||
employeeId: emp,
|
employeeId: emp,
|
||||||
@ -185,9 +211,13 @@ const ManageJob = ({ Job }) => {
|
|||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
className="btn btn-sm btn-primary"
|
className="btn btn-sm btn-primary"
|
||||||
disabled={isPending}
|
disabled={isPending || Updating}
|
||||||
>
|
>
|
||||||
{isPending ? "Please wait..." : "Submit"}
|
{isPending || Updating
|
||||||
|
? "Please wait..."
|
||||||
|
: Job
|
||||||
|
? "Update"
|
||||||
|
: "Submit"}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user