plane/web/store/workspace/api-token.store.ts
rahulramesha b3ac9def8d
fix: issue property dropdown data flow (#3425)
* dev: workspace states and estimates

* refactor issue dropdown logic to help work properly with issues on global level

* fix: project labels response change

* fix label type

* change store computed actions to computed functions from mobx-utils

* fix: state response change

* chore: project and workspace state change

* fix state and label types

* chore: state and label serializer change

* modify state and label types

* fix dropdown reset on project id change

* fix label sort order

---------

Co-authored-by: pablohashescobar <nikhilschacko@gmail.com>
Co-authored-by: Rahul R <rahulr@Rahuls-MacBook-Pro.local>
Co-authored-by: NarayanBavisetti <narayan3119@gmail.com>
Co-authored-by: Rahul R <rahul.ramesha@plane.so>
2024-01-22 17:07:32 +05:30

114 lines
3.4 KiB
TypeScript

// mobx
import { action, observable, makeObservable, runInAction } from "mobx";
import { computedFn } from "mobx-utils";
import { APITokenService } from "services/api_token.service";
import { RootStore } from "../root.store";
// types
import { IApiToken } from "@plane/types";
export interface IApiTokenStore {
// observables
apiTokens: Record<string, IApiToken> | null;
// computed actions
getApiTokenById: (apiTokenId: string) => IApiToken | null;
// fetch actions
fetchApiTokens: (workspaceSlug: string) => Promise<IApiToken[]>;
fetchApiTokenDetails: (workspaceSlug: string, tokenId: string) => Promise<IApiToken>;
// crud actions
createApiToken: (workspaceSlug: string, data: Partial<IApiToken>) => Promise<IApiToken>;
deleteApiToken: (workspaceSlug: string, tokenId: string) => Promise<void>;
}
export class ApiTokenStore implements IApiTokenStore {
// observables
apiTokens: Record<string, IApiToken> | null = null;
// services
apiTokenService;
// root store
rootStore;
constructor(_rootStore: RootStore) {
makeObservable(this, {
// observables
apiTokens: observable,
// fetch actions
fetchApiTokens: action,
fetchApiTokenDetails: action,
// CRUD actions
createApiToken: action,
deleteApiToken: action,
});
// root store
this.rootStore = _rootStore;
// services
this.apiTokenService = new APITokenService();
}
/**
* get API token by id
* @param apiTokenId
*/
getApiTokenById = computedFn((apiTokenId: string) => {
if (!this.apiTokens) return null;
return this.apiTokens[apiTokenId] || null;
});
/**
* fetch all the API tokens for a workspace
* @param workspaceSlug
*/
fetchApiTokens = async (workspaceSlug: string) =>
await this.apiTokenService.getApiTokens(workspaceSlug).then((response) => {
const apiTokensObject: { [apiTokenId: string]: IApiToken } = response.reduce((accumulator, currentWebhook) => {
if (currentWebhook && currentWebhook.id) {
return { ...accumulator, [currentWebhook.id]: currentWebhook };
}
return accumulator;
}, {});
runInAction(() => {
this.apiTokens = apiTokensObject;
});
return response;
});
/**
* fetch API token details using token id
* @param workspaceSlug
* @param tokenId
*/
fetchApiTokenDetails = async (workspaceSlug: string, tokenId: string) =>
await this.apiTokenService.retrieveApiToken(workspaceSlug, tokenId).then((response) => {
runInAction(() => {
this.apiTokens = { ...this.apiTokens, [response.id]: response };
});
return response;
});
/**
* create API token using data
* @param workspaceSlug
* @param data
*/
createApiToken = async (workspaceSlug: string, data: Partial<IApiToken>) =>
await this.apiTokenService.createApiToken(workspaceSlug, data).then((response) => {
runInAction(() => {
this.apiTokens = { ...this.apiTokens, [response.id]: response };
});
return response;
});
/**
* delete API token using token id
* @param workspaceSlug
* @param tokenId
*/
deleteApiToken = async (workspaceSlug: string, tokenId: string) =>
await this.apiTokenService.deleteApiToken(workspaceSlug, tokenId).then(() => {
const updatedApiTokens = { ...this.apiTokens };
delete updatedApiTokens[tokenId];
runInAction(() => {
this.apiTokens = updatedApiTokens;
});
});
}