mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
refactor: typed context and using observer hoc
This commit is contained in:
parent
380ad340a6
commit
b9975dfa24
@ -4,8 +4,13 @@ import { useRouter } from "next/router";
|
|||||||
|
|
||||||
import { mutate } from "swr";
|
import { mutate } from "swr";
|
||||||
|
|
||||||
|
// mobx
|
||||||
|
import { useObserver } from "mobx-react-lite";
|
||||||
|
|
||||||
// react-hook-form
|
// react-hook-form
|
||||||
import { Controller, SubmitHandler, useForm } from "react-hook-form";
|
import { Controller, SubmitHandler, useForm } from "react-hook-form";
|
||||||
|
// store
|
||||||
|
import labelStore from "store/labels";
|
||||||
// hooks
|
// hooks
|
||||||
import useUserAuth from "hooks/use-user-auth";
|
import useUserAuth from "hooks/use-user-auth";
|
||||||
// react-color
|
// react-color
|
||||||
@ -58,17 +63,14 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
|
|||||||
defaultValues,
|
defaultValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => {
|
const { createLabel } = useObserver(() => ({
|
||||||
if (!workspaceSlug || !projectId || isSubmitting) return;
|
createLabel: labelStore.createLabel,
|
||||||
|
}));
|
||||||
|
|
||||||
await issuesService
|
const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => {
|
||||||
.createIssueLabel(workspaceSlug as string, projectId as string, formData, user)
|
if (!workspaceSlug || !projectId || isSubmitting || !user) return;
|
||||||
.then((res) => {
|
|
||||||
mutate<IIssueLabels[]>(
|
await createLabel(workspaceSlug.toString(), projectId.toString(), formData, user).then(() => {
|
||||||
PROJECT_ISSUE_LABELS(projectId as string),
|
|
||||||
(prevData) => [res, ...(prevData ?? [])],
|
|
||||||
false
|
|
||||||
);
|
|
||||||
reset(defaultValues);
|
reset(defaultValues);
|
||||||
setLabelForm(false);
|
setLabelForm(false);
|
||||||
});
|
});
|
||||||
|
@ -4,7 +4,7 @@ import { RootStore } from "store/root";
|
|||||||
|
|
||||||
let rootStore: any = null;
|
let rootStore: any = null;
|
||||||
|
|
||||||
export const MobxStoreContext = createContext(null);
|
export const MobxStoreContext = createContext<RootStore>({} as RootStore);
|
||||||
|
|
||||||
const initializeStore = () => {
|
const initializeStore = () => {
|
||||||
const _rootStore = rootStore ?? new RootStore();
|
const _rootStore = rootStore ?? new RootStore();
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
import React, { useState, useRef } from "react";
|
import React, { useState, useRef, useEffect } from "react";
|
||||||
|
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
|
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
|
|
||||||
|
// mobx
|
||||||
|
import { observer } from "mobx-react-lite";
|
||||||
|
|
||||||
// hooks
|
// hooks
|
||||||
import useUserAuth from "hooks/use-user-auth";
|
import useUserAuth from "hooks/use-user-auth";
|
||||||
|
import { useMobxStore } from "lib/mobx/store-provider";
|
||||||
// services
|
// services
|
||||||
import projectService from "services/project.service";
|
import projectService from "services/project.service";
|
||||||
import issuesService from "services/issues.service";
|
|
||||||
// layouts
|
// layouts
|
||||||
import { ProjectAuthorizationWrapper } from "layouts/auth-layout";
|
import { ProjectAuthorizationWrapper } from "layouts/auth-layout";
|
||||||
// components
|
// components
|
||||||
@ -31,7 +34,7 @@ import emptyLabel from "public/empty-state/label.svg";
|
|||||||
import { IIssueLabels } from "types";
|
import { IIssueLabels } from "types";
|
||||||
import type { NextPage } from "next";
|
import type { NextPage } from "next";
|
||||||
// fetch-keys
|
// fetch-keys
|
||||||
import { PROJECT_DETAILS, PROJECT_ISSUE_LABELS } from "constants/fetch-keys";
|
import { PROJECT_DETAILS } from "constants/fetch-keys";
|
||||||
// helper
|
// helper
|
||||||
import { truncateText } from "helpers/string.helper";
|
import { truncateText } from "helpers/string.helper";
|
||||||
|
|
||||||
@ -57,6 +60,9 @@ const LabelsSettings: NextPage = () => {
|
|||||||
|
|
||||||
const scrollToRef = useRef<HTMLDivElement>(null);
|
const scrollToRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
const { labelStore } = useMobxStore();
|
||||||
|
const { isLabelsLoading: isLoading, labels } = labelStore;
|
||||||
|
|
||||||
const { data: projectDetails } = useSWR(
|
const { data: projectDetails } = useSWR(
|
||||||
workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null,
|
workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null,
|
||||||
workspaceSlug && projectId
|
workspaceSlug && projectId
|
||||||
@ -64,13 +70,6 @@ const LabelsSettings: NextPage = () => {
|
|||||||
: null
|
: null
|
||||||
);
|
);
|
||||||
|
|
||||||
const { data: issueLabels } = useSWR(
|
|
||||||
workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId as string) : null,
|
|
||||||
workspaceSlug && projectId
|
|
||||||
? () => issuesService.getIssueLabels(workspaceSlug as string, projectId as string)
|
|
||||||
: null
|
|
||||||
);
|
|
||||||
|
|
||||||
const newLabel = () => {
|
const newLabel = () => {
|
||||||
setIsUpdating(false);
|
setIsUpdating(false);
|
||||||
setLabelForm(true);
|
setLabelForm(true);
|
||||||
@ -87,6 +86,11 @@ const LabelsSettings: NextPage = () => {
|
|||||||
setLabelToUpdate(label);
|
setLabelToUpdate(label);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (workspaceSlug && projectId)
|
||||||
|
labelStore.loadLabels(workspaceSlug.toString(), projectId.toString());
|
||||||
|
}, [workspaceSlug, projectId, labelStore]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<LabelsListModal
|
<LabelsListModal
|
||||||
@ -137,10 +141,10 @@ const LabelsSettings: NextPage = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<>
|
<>
|
||||||
{issueLabels ? (
|
{!isLoading ? (
|
||||||
issueLabels.length > 0 ? (
|
labels.length > 0 ? (
|
||||||
issueLabels.map((label) => {
|
labels.map((label) => {
|
||||||
const children = issueLabels?.filter((l) => l.parent === label.id);
|
const children = labels?.filter((l) => l.parent === label.id);
|
||||||
|
|
||||||
if (children && children.length === 0) {
|
if (children && children.length === 0) {
|
||||||
if (!label.parent)
|
if (!label.parent)
|
||||||
@ -148,7 +152,17 @@ const LabelsSettings: NextPage = () => {
|
|||||||
<SingleLabel
|
<SingleLabel
|
||||||
key={label.id}
|
key={label.id}
|
||||||
label={label}
|
label={label}
|
||||||
addLabelToGroup={() => addLabelToGroup(label)}
|
addLabelToGroup={() => {
|
||||||
|
addLabelToGroup(label);
|
||||||
|
// if (!workspaceSlug || !projectId || !user) return;
|
||||||
|
// labelStore.updateLabel(
|
||||||
|
// workspaceSlug.toString(),
|
||||||
|
// projectId.toString(),
|
||||||
|
// label.id,
|
||||||
|
// { parent: label.id },
|
||||||
|
// user
|
||||||
|
// );
|
||||||
|
}}
|
||||||
editLabel={(label) => {
|
editLabel={(label) => {
|
||||||
editLabel(label);
|
editLabel(label);
|
||||||
scrollToRef.current?.scrollIntoView({
|
scrollToRef.current?.scrollIntoView({
|
||||||
@ -205,4 +219,4 @@ const LabelsSettings: NextPage = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default LabelsSettings;
|
export default observer(LabelsSettings);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// mobx
|
// mobx
|
||||||
import { action, observable, runInAction, makeObservable } from "mobx";
|
import { action, observable, runInAction, makeAutoObservable } from "mobx";
|
||||||
|
|
||||||
// services
|
// services
|
||||||
import issueService from "services/issues.service";
|
import issueService from "services/issues.service";
|
||||||
@ -9,12 +9,14 @@ import type { IIssueLabels, LabelForm, ICurrentUserResponse } from "types";
|
|||||||
|
|
||||||
class LabelStore {
|
class LabelStore {
|
||||||
labels: IIssueLabels[] = [];
|
labels: IIssueLabels[] = [];
|
||||||
|
isLabelsLoading: boolean = false;
|
||||||
rootStore: any | null = null;
|
rootStore: any | null = null;
|
||||||
|
|
||||||
constructor(_rootStore: any | null = null) {
|
constructor(_rootStore: any | null = null) {
|
||||||
makeObservable(this, {
|
makeAutoObservable(this, {
|
||||||
labels: observable.ref,
|
labels: observable.ref,
|
||||||
loadLabels: action,
|
loadLabels: action,
|
||||||
|
isLabelsLoading: observable,
|
||||||
createLabel: action,
|
createLabel: action,
|
||||||
updateLabel: action,
|
updateLabel: action,
|
||||||
deleteLabel: action,
|
deleteLabel: action,
|
||||||
@ -24,6 +26,7 @@ class LabelStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadLabels = async (workspaceSlug: string, projectId: string) => {
|
loadLabels = async (workspaceSlug: string, projectId: string) => {
|
||||||
|
this.isLabelsLoading = true;
|
||||||
try {
|
try {
|
||||||
const labelsResponse: IIssueLabels[] = await issueService.getIssueLabels(
|
const labelsResponse: IIssueLabels[] = await issueService.getIssueLabels(
|
||||||
workspaceSlug,
|
workspaceSlug,
|
||||||
@ -31,8 +34,10 @@ class LabelStore {
|
|||||||
);
|
);
|
||||||
runInAction(() => {
|
runInAction(() => {
|
||||||
this.labels = labelsResponse;
|
this.labels = labelsResponse;
|
||||||
|
this.isLabelsLoading = false;
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.isLabelsLoading = false;
|
||||||
console.error("Fetching labels error", error);
|
console.error("Fetching labels error", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -62,7 +67,7 @@ class LabelStore {
|
|||||||
workspaceSlug: string,
|
workspaceSlug: string,
|
||||||
projectId: string,
|
projectId: string,
|
||||||
labelId: string,
|
labelId: string,
|
||||||
labelForm: LabelForm,
|
labelForm: Partial<LabelForm>,
|
||||||
user: ICurrentUserResponse
|
user: ICurrentUserResponse
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
|
@ -10,11 +10,11 @@ enableStaticRendering(typeof window === "undefined");
|
|||||||
export class RootStore {
|
export class RootStore {
|
||||||
user;
|
user;
|
||||||
theme;
|
theme;
|
||||||
labels;
|
labelStore: LabelStore;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.user = new UserStore(this);
|
this.user = new UserStore(this);
|
||||||
this.theme = new ThemeStore(this);
|
this.theme = new ThemeStore(this);
|
||||||
this.labels = new LabelStore(this);
|
this.labelStore = new LabelStore(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user