forked from github/plane
fix: reset data to form logic
This commit is contained in:
parent
24b82e518c
commit
f489f97aa8
@ -61,9 +61,8 @@ interface IssueFormProps {
|
|||||||
formData: Partial<IIssue>,
|
formData: Partial<IIssue>,
|
||||||
action?: "createDraft" | "createNewIssue" | "updateDraft" | "convertToNewIssue"
|
action?: "createDraft" | "createNewIssue" | "updateDraft" | "convertToNewIssue"
|
||||||
) => Promise<void>;
|
) => Promise<void>;
|
||||||
data?: Partial<IIssue> | null;
|
initialData?: Partial<IIssue> | null;
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
prePopulatedData?: Partial<IIssue> | null;
|
|
||||||
projectId: string;
|
projectId: string;
|
||||||
setActiveProject: React.Dispatch<React.SetStateAction<string | null>>;
|
setActiveProject: React.Dispatch<React.SetStateAction<string | null>>;
|
||||||
createMore: boolean;
|
createMore: boolean;
|
||||||
@ -91,9 +90,8 @@ interface IssueFormProps {
|
|||||||
export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
||||||
const {
|
const {
|
||||||
handleFormSubmit,
|
handleFormSubmit,
|
||||||
data,
|
initialData,
|
||||||
isOpen,
|
isOpen,
|
||||||
prePopulatedData,
|
|
||||||
projectId,
|
projectId,
|
||||||
setActiveProject,
|
setActiveProject,
|
||||||
createMore,
|
createMore,
|
||||||
@ -124,7 +122,7 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
const editorSuggestions = useEditorSuggestions(workspaceSlug as string | undefined, projectId);
|
const editorSuggestions = useEditorSuggestions(workspaceSlug as string | undefined, projectId);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
formState: { errors, isSubmitting },
|
formState: { errors, isSubmitting, isDirty },
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
reset,
|
reset,
|
||||||
watch,
|
watch,
|
||||||
@ -132,30 +130,30 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
getValues,
|
getValues,
|
||||||
setValue,
|
setValue,
|
||||||
} = useForm<IIssue>({
|
} = useForm<IIssue>({
|
||||||
defaultValues: prePopulatedData ?? defaultValues,
|
defaultValues: initialData ?? defaultValues,
|
||||||
reValidateMode: "onChange",
|
reValidateMode: "onChange",
|
||||||
});
|
});
|
||||||
|
|
||||||
const issueName = watch("name");
|
const issueName = watch("name");
|
||||||
|
|
||||||
const payload: Partial<IIssue> = {
|
const payload: Partial<IIssue> = {
|
||||||
name: watch("name"),
|
name: getValues("name"),
|
||||||
description: watch("description"),
|
description: getValues("description"),
|
||||||
description_html: watch("description_html"),
|
description_html: getValues("description_html"),
|
||||||
state: watch("state"),
|
state: getValues("state"),
|
||||||
priority: watch("priority"),
|
priority: getValues("priority"),
|
||||||
assignees: watch("assignees"),
|
assignees: getValues("assignees"),
|
||||||
labels: watch("labels"),
|
labels: getValues("labels"),
|
||||||
start_date: watch("start_date"),
|
start_date: getValues("start_date"),
|
||||||
target_date: watch("target_date"),
|
target_date: getValues("target_date"),
|
||||||
project: watch("project"),
|
project: getValues("project"),
|
||||||
parent: watch("parent"),
|
parent: getValues("parent"),
|
||||||
cycle: watch("cycle"),
|
cycle: getValues("cycle"),
|
||||||
module: watch("module"),
|
module: getValues("module"),
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isOpen || data) return;
|
if (!isOpen) return;
|
||||||
|
|
||||||
setLocalStorageValue(
|
setLocalStorageValue(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@ -163,7 +161,7 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [JSON.stringify(payload), isOpen, data]);
|
}, [JSON.stringify(payload), isOpen]);
|
||||||
|
|
||||||
const handleCreateUpdateIssue = async (
|
const handleCreateUpdateIssue = async (
|
||||||
formData: Partial<IIssue>,
|
formData: Partial<IIssue>,
|
||||||
@ -171,7 +169,7 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
) => {
|
) => {
|
||||||
await handleFormSubmit(
|
await handleFormSubmit(
|
||||||
{
|
{
|
||||||
...(data ?? {}),
|
...(initialData ?? {}),
|
||||||
...formData,
|
...formData,
|
||||||
},
|
},
|
||||||
action
|
action
|
||||||
@ -248,12 +246,14 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// if form is dirty, don't reset
|
||||||
|
if (isDirty) return;
|
||||||
|
|
||||||
reset({
|
reset({
|
||||||
...defaultValues,
|
...defaultValues,
|
||||||
...(prePopulatedData ?? {}),
|
...(initialData ?? {}),
|
||||||
...(data ?? {}),
|
|
||||||
});
|
});
|
||||||
}, [prePopulatedData, reset, data]);
|
}, [reset, initialData, isDirty]);
|
||||||
|
|
||||||
// update projectId in form when projectId changes
|
// update projectId in form when projectId changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -287,7 +287,7 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
)}
|
)}
|
||||||
<form
|
<form
|
||||||
onSubmit={handleSubmit((formData) =>
|
onSubmit={handleSubmit((formData) =>
|
||||||
handleCreateUpdateIssue(formData, data ? "convertToNewIssue" : "createDraft")
|
handleCreateUpdateIssue(formData, initialData?.id ? "convertToNewIssue" : "createDraft")
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div className="space-y-5">
|
<div className="space-y-5">
|
||||||
@ -583,7 +583,7 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
variant="neutral-primary"
|
variant="neutral-primary"
|
||||||
loading={isSubmitting}
|
loading={isSubmitting}
|
||||||
onClick={handleSubmit((formData) =>
|
onClick={handleSubmit((formData) =>
|
||||||
handleCreateUpdateIssue(formData, data?.id ? "updateDraft" : "createDraft")
|
handleCreateUpdateIssue(formData, initialData?.id ? "updateDraft" : "createDraft")
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{isSubmitting ? "Saving..." : "Save Draft"}
|
{isSubmitting ? "Saving..." : "Save Draft"}
|
||||||
@ -592,7 +592,7 @@ export const DraftIssueForm: FC<IssueFormProps> = (props) => {
|
|||||||
loading={isSubmitting}
|
loading={isSubmitting}
|
||||||
variant="primary"
|
variant="primary"
|
||||||
onClick={handleSubmit((formData) =>
|
onClick={handleSubmit((formData) =>
|
||||||
handleCreateUpdateIssue(formData, data ? "convertToNewIssue" : "createNewIssue")
|
handleCreateUpdateIssue(formData, initialData?.id ? "convertToNewIssue" : "createNewIssue")
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{isSubmitting ? "Saving..." : "Add Issue"}
|
{isSubmitting ? "Saving..." : "Add Issue"}
|
||||||
|
@ -21,11 +21,10 @@ import type { IIssue } from "types";
|
|||||||
import { SUB_ISSUES } from "constants/fetch-keys";
|
import { SUB_ISSUES } from "constants/fetch-keys";
|
||||||
|
|
||||||
interface IssuesModalProps {
|
interface IssuesModalProps {
|
||||||
data?: IIssue | null;
|
|
||||||
handleClose: () => void;
|
handleClose: () => void;
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
isUpdatingSingleIssue?: boolean;
|
isUpdatingSingleIssue?: boolean;
|
||||||
prePopulateData?: Partial<IIssue>;
|
initialData?: Partial<IIssue>;
|
||||||
fieldsToShow?: (
|
fieldsToShow?: (
|
||||||
| "project"
|
| "project"
|
||||||
| "name"
|
| "name"
|
||||||
@ -48,7 +47,7 @@ const issueService = new IssueService();
|
|||||||
const moduleService = new ModuleService();
|
const moduleService = new ModuleService();
|
||||||
|
|
||||||
export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = observer((props) => {
|
export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = observer((props) => {
|
||||||
const { data, handleClose, isOpen, prePopulateData: prePopulateDataProps, fieldsToShow = ["all"], onSubmit } = props;
|
const { handleClose, isOpen, initialData, fieldsToShow = ["all"], onSubmit } = props;
|
||||||
|
|
||||||
// states
|
// states
|
||||||
const [createMore, setCreateMore] = useState(false);
|
const [createMore, setCreateMore] = useState(false);
|
||||||
@ -85,33 +84,30 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = observer(
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setPreloadedData(prePopulateDataProps ?? {});
|
setPreloadedData(initialData ?? {});
|
||||||
|
|
||||||
if (cycleId && !prePopulateDataProps?.cycle) {
|
if (cycleId && !initialData?.cycle) {
|
||||||
setPreloadedData((prevData) => ({
|
setPreloadedData((prevData) => ({
|
||||||
...(prevData ?? {}),
|
...(prevData ?? {}),
|
||||||
...prePopulateDataProps,
|
...initialData,
|
||||||
cycle: cycleId.toString(),
|
cycle: cycleId.toString(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
if (moduleId && !prePopulateDataProps?.module) {
|
if (moduleId && !initialData?.module) {
|
||||||
setPreloadedData((prevData) => ({
|
setPreloadedData((prevData) => ({
|
||||||
...(prevData ?? {}),
|
...(prevData ?? {}),
|
||||||
...prePopulateDataProps,
|
...initialData,
|
||||||
module: moduleId.toString(),
|
module: moduleId.toString(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
if (
|
if ((router.asPath.includes("my-issues") || router.asPath.includes("assigned")) && !initialData?.assignees) {
|
||||||
(router.asPath.includes("my-issues") || router.asPath.includes("assigned")) &&
|
|
||||||
!prePopulateDataProps?.assignees
|
|
||||||
) {
|
|
||||||
setPreloadedData((prevData) => ({
|
setPreloadedData((prevData) => ({
|
||||||
...(prevData ?? {}),
|
...(prevData ?? {}),
|
||||||
...prePopulateDataProps,
|
...initialData,
|
||||||
assignees: prePopulateDataProps?.assignees ?? [user?.id ?? ""],
|
assignees: initialData?.assignees ?? [user?.id ?? ""],
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}, [prePopulateDataProps, cycleId, moduleId, router.asPath, user?.id]);
|
}, [initialData, cycleId, moduleId, router.asPath, user?.id]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// if modal is closed, reset active project to null
|
// if modal is closed, reset active project to null
|
||||||
@ -123,7 +119,7 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = observer(
|
|||||||
|
|
||||||
// if data is present, set active project to the project of the
|
// if data is present, set active project to the project of the
|
||||||
// issue. This has more priority than the project in the url.
|
// issue. This has more priority than the project in the url.
|
||||||
if (data && data.project) return setActiveProject(data.project);
|
if (initialData && initialData.project) return setActiveProject(initialData.project);
|
||||||
|
|
||||||
if (prePopulateData && prePopulateData.project && !activeProject) return setActiveProject(prePopulateData.project);
|
if (prePopulateData && prePopulateData.project && !activeProject) return setActiveProject(prePopulateData.project);
|
||||||
|
|
||||||
@ -133,7 +129,7 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = observer(
|
|||||||
// in the url. This has the least priority.
|
// in the url. This has the least priority.
|
||||||
if (projects && projects.length > 0 && !activeProject)
|
if (projects && projects.length > 0 && !activeProject)
|
||||||
setActiveProject(projects?.find((p) => p.id === projectId)?.id ?? projects?.[0].id ?? null);
|
setActiveProject(projects?.find((p) => p.id === projectId)?.id ?? projects?.[0].id ?? null);
|
||||||
}, [activeProject, data, projectId, projects, isOpen, prePopulateData]);
|
}, [activeProject, initialData, projectId, projects, isOpen, prePopulateData]);
|
||||||
|
|
||||||
const createDraftIssue = async (payload: Partial<IIssue>) => {
|
const createDraftIssue = async (payload: Partial<IIssue>) => {
|
||||||
if (!workspaceSlug || !activeProject || !user) return;
|
if (!workspaceSlug || !activeProject || !user) return;
|
||||||
@ -328,15 +324,14 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = observer(
|
|||||||
<DraftIssueForm
|
<DraftIssueForm
|
||||||
isOpen={isOpen}
|
isOpen={isOpen}
|
||||||
handleFormSubmit={handleFormSubmit}
|
handleFormSubmit={handleFormSubmit}
|
||||||
prePopulatedData={prePopulateData}
|
initialData={initialData}
|
||||||
data={data}
|
|
||||||
createMore={createMore}
|
createMore={createMore}
|
||||||
setCreateMore={setCreateMore}
|
setCreateMore={setCreateMore}
|
||||||
handleClose={onClose}
|
handleClose={onClose}
|
||||||
handleDiscard={onDiscard}
|
handleDiscard={onDiscard}
|
||||||
projectId={activeProject ?? ""}
|
projectId={activeProject ?? ""}
|
||||||
setActiveProject={setActiveProject}
|
setActiveProject={setActiveProject}
|
||||||
status={data ? true : false}
|
status={initialData?.id ? true : false}
|
||||||
user={user ?? undefined}
|
user={user ?? undefined}
|
||||||
fieldsToShow={fieldsToShow}
|
fieldsToShow={fieldsToShow}
|
||||||
/>
|
/>
|
||||||
|
@ -33,18 +33,13 @@ export const DraftIssueListLayout: FC = observer(() => {
|
|||||||
(group_by: string | null, issue: IIssue, action: "update" | "delete" | "convertToIssue") => {
|
(group_by: string | null, issue: IIssue, action: "update" | "delete" | "convertToIssue") => {
|
||||||
if (!workspaceSlug || !projectId) return;
|
if (!workspaceSlug || !projectId) return;
|
||||||
|
|
||||||
const grouping = {
|
|
||||||
group_id: group_by,
|
|
||||||
sub_group_id: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (action === "update") {
|
if (action === "update") {
|
||||||
draftIssuesStore.updateDraftIssue(workspaceSlug.toString(), projectId.toString(), grouping, issue);
|
draftIssuesStore.updateDraftIssue(workspaceSlug.toString(), projectId.toString(), issue);
|
||||||
draftIssuesStore.updateIssueStructure(group_by, null, issue);
|
draftIssuesStore.updateIssueStructure(group_by, null, issue);
|
||||||
} else if (action === "delete") {
|
} else if (action === "delete") {
|
||||||
draftIssuesStore.deleteDraftIssue(workspaceSlug.toString(), projectId.toString(), issue.id);
|
draftIssuesStore.deleteDraftIssue(workspaceSlug.toString(), projectId.toString(), issue.id);
|
||||||
} else if (action === "convertToIssue") {
|
} else if (action === "convertToIssue") {
|
||||||
draftIssuesStore.convertDraftIssueToIssue(workspaceSlug.toString(), projectId.toString(), grouping, issue.id);
|
draftIssuesStore.convertDraftIssueToIssue(workspaceSlug.toString(), projectId.toString(), issue.id);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[workspaceSlug, projectId, draftIssuesStore]
|
[workspaceSlug, projectId, draftIssuesStore]
|
||||||
|
@ -28,9 +28,7 @@ export const DraftIssueQuickActions: React.FC<Props> = (props) => {
|
|||||||
setCreateUpdateIssueModal(false);
|
setCreateUpdateIssueModal(false);
|
||||||
setIssueToEdit(null);
|
setIssueToEdit(null);
|
||||||
}}
|
}}
|
||||||
// pre-populate date only if not editing
|
initialData={issueToEdit ? { ...issueToEdit } : { ...issue, name: issue?.name, description: issue.description }}
|
||||||
prePopulateData={!issueToEdit && createUpdateIssueModal ? { ...issue } : {}}
|
|
||||||
data={issueToEdit}
|
|
||||||
/>
|
/>
|
||||||
<CustomMenu ellipsis>
|
<CustomMenu ellipsis>
|
||||||
<CustomMenu.MenuItem
|
<CustomMenu.MenuItem
|
||||||
|
@ -21,7 +21,7 @@ export const WorkspaceSidebarQuickAction = () => {
|
|||||||
<CreateUpdateDraftIssueModal
|
<CreateUpdateDraftIssueModal
|
||||||
isOpen={isDraftIssueModalOpen}
|
isOpen={isDraftIssueModalOpen}
|
||||||
handleClose={() => setIsDraftIssueModalOpen(false)}
|
handleClose={() => setIsDraftIssueModalOpen(false)}
|
||||||
prePopulateData={storedValue ? JSON.parse(storedValue) : {}}
|
initialData={storedValue ? JSON.parse(storedValue) : {}}
|
||||||
onSubmit={() => {
|
onSubmit={() => {
|
||||||
localStorage.removeItem("draftedIssue");
|
localStorage.removeItem("draftedIssue");
|
||||||
clearValue();
|
clearValue();
|
||||||
|
Loading…
Reference in New Issue
Block a user