refactor: services

This commit is contained in:
Aaryan Khandelwal 2023-10-03 18:03:53 +05:30
parent 639a30abb5
commit 22d659cd4c
5 changed files with 40 additions and 29 deletions

View File

@ -1,6 +1,6 @@
import { useEffect } from "react";
import { useRouter } from "next/router";
import { observer } from "mobx-react-lite";
import useSWR from "swr";
// mobx store
import { useMobxStore } from "lib/mobx/store-provider";
@ -10,6 +10,8 @@ import { SingleObject } from "components/custom-attributes";
import { EmptyState, Loader } from "components/ui";
// assets
import emptyCustomObjects from "public/empty-state/custom-objects.svg";
// fetch-keys
import { CUSTOM_OBJECTS_LIST } from "constants/fetch-keys";
type Props = {
projectId: string;
@ -19,20 +21,22 @@ export const ObjectsList: React.FC<Props> = observer(({ projectId }) => {
const router = useRouter();
const { workspaceSlug } = router.query;
const { customAttributes } = useMobxStore();
const { customAttributes: customAttributesStore } = useMobxStore();
useEffect(() => {
if (!workspaceSlug) return;
if (!customAttributes.objects)
customAttributes.fetchObjects(workspaceSlug.toString(), projectId);
}, [customAttributes, projectId, workspaceSlug]);
useSWR(
workspaceSlug && projectId ? CUSTOM_OBJECTS_LIST(projectId.toString()) : null,
workspaceSlug && projectId
? () => customAttributesStore.fetchObjects(workspaceSlug.toString(), projectId.toString())
: null
);
return (
<div className="divide-y divide-custom-border-100">
{customAttributes.objects ? (
customAttributes.objects.length > 0 ? (
customAttributes.objects.map((object) => <SingleObject key={object.id} object={object} />)
{customAttributesStore.objects ? (
customAttributesStore.objects.length > 0 ? (
customAttributesStore.objects.map((object) => (
<SingleObject key={object.id} object={object} />
))
) : (
<div className="bg-custom-background-90 border border-custom-border-100 rounded max-w-3xl mt-10 mx-auto">
<EmptyState

View File

@ -386,3 +386,6 @@ export const COMMENT_REACTION_LIST = (
commendId: string
) =>
`COMMENT_REACTION_LIST_${workspaceSlug.toUpperCase()}_${projectId.toUpperCase()}_${commendId.toUpperCase()}`;
export const CUSTOM_OBJECTS_LIST = (projectId: string) =>
`CUSTOM_OBJECTS_LIST_${projectId.toUpperCase()}`;

View File

@ -23,7 +23,7 @@ class CustomAttributesService extends APIService {
});
}
async getPropertyDetails(workspaceSlug: string, propertyId: string): Promise<ICustomAttribute> {
async getAttributeDetails(workspaceSlug: string, propertyId: string): Promise<ICustomAttribute> {
return this.get(`/api/workspaces/${workspaceSlug}/properties/${propertyId}/`)
.then((response) => response?.data)
.catch((error) => {
@ -31,7 +31,7 @@ class CustomAttributesService extends APIService {
});
}
async createProperty(
async createAttribute(
workspaceSlug: string,
data: Partial<ICustomAttribute>
): Promise<ICustomAttribute> {
@ -42,7 +42,7 @@ class CustomAttributesService extends APIService {
});
}
async patchProperty(
async patchAttribute(
workspaceSlug: string,
propertyId: string,
data: Partial<ICustomAttribute>
@ -54,7 +54,7 @@ class CustomAttributesService extends APIService {
});
}
async deleteProperty(workspaceSlug: string, propertyId: string): Promise<any> {
async deleteAttribute(workspaceSlug: string, propertyId: string): Promise<any> {
return this.delete(`/api/workspaces/${workspaceSlug}/properties/${propertyId}/`)
.then((response) => response?.data)
.catch((error) => {
@ -76,7 +76,7 @@ class CustomAttributesService extends APIService {
});
}
async createPropertyValues(
async createAttributeValues(
workspaceSlug: string,
projectId: string,
issueId: string,
@ -92,7 +92,7 @@ class CustomAttributesService extends APIService {
});
}
async deletePropertyValue(
async deleteAttributeValue(
workspaceSlug: string,
projectId: string,
issueId: string,

View File

@ -83,7 +83,7 @@ class CustomAttributeValuesStore {
const date = new Date();
const unixEpochTimeInSeconds = Math.floor(date.getTime() / 1000);
const response = await customAttributesService.createPropertyValues(
const response = await customAttributesService.createAttributeValues(
workspaceSlug,
projectId,
issueId,
@ -122,7 +122,7 @@ class CustomAttributeValuesStore {
};
});
await customAttributesService.deletePropertyValue(
await customAttributesService.deleteAttributeValue(
workspaceSlug,
projectId,
issueId,

View File

@ -70,7 +70,7 @@ class CustomAttributesStore {
this.fetchObjectDetailsLoader = true;
});
const response = await customAttributesService.getPropertyDetails(workspaceSlug, objectId);
const response = await customAttributesService.getAttributeDetails(workspaceSlug, objectId);
const objectChildren: { [key: string]: ICustomAttribute } = response.children.reduce(
(acc, child) => ({
@ -99,7 +99,7 @@ class CustomAttributesStore {
createObject = async (workspaceSlug: string, data: Partial<ICustomAttribute>) => {
try {
const response = await customAttributesService.createProperty(workspaceSlug, data);
const response = await customAttributesService.createAttribute(workspaceSlug, data);
runInAction(() => {
this.objects = [...(this.objects ?? []), response];
@ -119,7 +119,7 @@ class CustomAttributesStore {
data: Partial<ICustomAttribute>
) => {
try {
const response = await customAttributesService.patchProperty(workspaceSlug, objectId, data);
const response = await customAttributesService.patchAttribute(workspaceSlug, objectId, data);
const newObjects = [...(this.objects ?? [])].map((object) =>
object.id === objectId ? { ...object, ...response } : object
@ -139,7 +139,7 @@ class CustomAttributesStore {
deleteObject = async (workspaceSlug: string, objectId: string) => {
try {
await customAttributesService.deleteProperty(workspaceSlug, objectId);
await customAttributesService.deleteAttribute(workspaceSlug, objectId);
const newObjects = this.objects?.filter((object) => object.id !== objectId);
@ -162,7 +162,7 @@ class CustomAttributesStore {
this.createObjectAttributeLoader = true;
});
const response = await customAttributesService.createProperty(workspaceSlug, data);
const response = await customAttributesService.createAttribute(workspaceSlug, data);
runInAction(() => {
this.objectAttributes = {
@ -191,7 +191,7 @@ class CustomAttributesStore {
data: Partial<ICustomAttribute>
) => {
try {
await customAttributesService.patchProperty(workspaceSlug, propertyId, data);
await customAttributesService.patchAttribute(workspaceSlug, propertyId, data);
const newObjects = this.objectAttributes[parentId];
newObjects[propertyId] = {
@ -214,7 +214,7 @@ class CustomAttributesStore {
deleteObjectAttribute = async (workspaceSlug: string, parentId: string, propertyId: string) => {
try {
await customAttributesService.deleteProperty(workspaceSlug, propertyId);
await customAttributesService.deleteAttribute(workspaceSlug, propertyId);
const newObjects = this.objectAttributes[parentId];
delete newObjects[propertyId];
@ -242,7 +242,7 @@ class CustomAttributesStore {
this.createAttributeOptionLoader = true;
});
const response = await customAttributesService.createProperty(workspaceSlug, data);
const response = await customAttributesService.createAttribute(workspaceSlug, data);
runInAction(() => {
this.objectAttributes = {
@ -275,7 +275,11 @@ class CustomAttributesStore {
data: Partial<ICustomAttribute>
) => {
try {
const response = await customAttributesService.patchProperty(workspaceSlug, propertyId, data);
const response = await customAttributesService.patchAttribute(
workspaceSlug,
propertyId,
data
);
const newOptions = this.objectAttributes[objectId][parentId].children.map((option) => ({
...option,
@ -327,7 +331,7 @@ class CustomAttributesStore {
};
});
await customAttributesService.deleteProperty(workspaceSlug, propertyId);
await customAttributesService.deleteAttribute(workspaceSlug, propertyId);
} catch (error) {
runInAction(() => {
this.error = error;