2022-12-02 11:47:13 +00:00
|
|
|
import { useState, useEffect, useCallback } from "react";
|
2022-11-19 14:21:26 +00:00
|
|
|
// swr
|
|
|
|
import useSWR from "swr";
|
|
|
|
// api routes
|
|
|
|
import { ISSUE_PROPERTIES_ENDPOINT } from "constants/api-routes";
|
|
|
|
// services
|
2022-12-12 04:49:52 +00:00
|
|
|
import issueServices from "lib/services/issues.service";
|
2022-11-19 14:21:26 +00:00
|
|
|
// hooks
|
|
|
|
import useUser from "./useUser";
|
|
|
|
// types
|
|
|
|
import { IssuePriorities, Properties } from "types";
|
|
|
|
|
|
|
|
const initialValues: Properties = {
|
|
|
|
key: true,
|
|
|
|
state: true,
|
|
|
|
assignee: true,
|
|
|
|
priority: false,
|
|
|
|
start_date: false,
|
2022-12-07 23:27:10 +00:00
|
|
|
target_date: false,
|
2022-11-19 14:21:26 +00:00
|
|
|
cycle: false,
|
2022-12-19 14:43:43 +00:00
|
|
|
children_count: false,
|
2022-11-19 14:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const useIssuesProperties = (workspaceSlug?: string, projectId?: string) => {
|
|
|
|
const [properties, setProperties] = useState<Properties>(initialValues);
|
|
|
|
|
|
|
|
const { user } = useUser();
|
|
|
|
|
|
|
|
const { data: issueProperties } = useSWR<IssuePriorities>(
|
|
|
|
workspaceSlug && projectId ? ISSUE_PROPERTIES_ENDPOINT(workspaceSlug, projectId) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => issueServices.getIssueProperties(workspaceSlug, projectId)
|
|
|
|
: null,
|
|
|
|
{
|
|
|
|
shouldRetryOnError: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!issueProperties || !workspaceSlug || !projectId || !user) return;
|
|
|
|
setProperties({ ...initialValues, ...issueProperties.properties });
|
|
|
|
|
|
|
|
if (Object.keys(issueProperties).length === 0)
|
|
|
|
issueServices.createIssueProperties(workspaceSlug, projectId, {
|
|
|
|
properties: { ...initialValues },
|
|
|
|
user: user.id,
|
|
|
|
});
|
|
|
|
else if (Object.keys(issueProperties?.properties).length === 0)
|
|
|
|
issueServices.patchIssueProperties(workspaceSlug, projectId, issueProperties.id, {
|
|
|
|
properties: { ...initialValues },
|
|
|
|
user: user.id,
|
|
|
|
});
|
|
|
|
}, [issueProperties, workspaceSlug, projectId, user]);
|
|
|
|
|
|
|
|
const updateIssueProperties = useCallback(
|
|
|
|
(key: keyof Properties) => {
|
|
|
|
if (!workspaceSlug || !projectId || !issueProperties || !user) return;
|
|
|
|
setProperties((prev) => ({ ...prev, [key]: !prev[key] }));
|
|
|
|
if (Object.keys(issueProperties).length > 0) {
|
|
|
|
issueServices.patchIssueProperties(workspaceSlug, projectId, issueProperties.id, {
|
|
|
|
properties: {
|
|
|
|
...issueProperties.properties,
|
|
|
|
[key]: !issueProperties.properties[key],
|
|
|
|
},
|
|
|
|
user: user.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
issueServices.createIssueProperties(workspaceSlug, projectId, {
|
|
|
|
properties: { ...initialValues },
|
|
|
|
user: user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[workspaceSlug, projectId, issueProperties, user]
|
|
|
|
);
|
|
|
|
|
2022-12-02 11:47:13 +00:00
|
|
|
const newProperties = Object.keys(properties).reduce((obj: any, key) => {
|
|
|
|
if (
|
|
|
|
key !== "children" &&
|
|
|
|
key !== "name" &&
|
|
|
|
key !== "parent" &&
|
|
|
|
key !== "project" &&
|
|
|
|
key !== "description" &&
|
|
|
|
key !== "attachments" &&
|
|
|
|
key !== "sequence_id"
|
|
|
|
) {
|
|
|
|
obj[key] = properties[key as keyof Properties];
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return [newProperties, updateIssueProperties] as const;
|
2022-11-19 14:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default useIssuesProperties;
|