mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
chore: init webhook data store
This commit is contained in:
parent
1fffc90cea
commit
a43b23907d
@ -26,7 +26,7 @@ export class WebhookService extends APIService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async createWebhook(workspaceSlug: string, data: {}): Promise<IWebhook> {
|
async createWebhook(workspaceSlug: string, data: Partial<IWebhook>): Promise<IWebhook> {
|
||||||
return this.post(`/api/workspaces/${workspaceSlug}/webhooks/`, data)
|
return this.post(`/api/workspaces/${workspaceSlug}/webhooks/`, data)
|
||||||
.then((response) => response?.data)
|
.then((response) => response?.data)
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@ -34,7 +34,7 @@ export class WebhookService extends APIService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateWebhook(workspaceSlug: string, webhookId: string, data: {}): Promise<IWebhook> {
|
async updateWebhook(workspaceSlug: string, webhookId: string, data: Partial<IWebhook>): Promise<IWebhook> {
|
||||||
return this.patch(`/api/workspaces/${workspaceSlug}/webhooks/${webhookId}/`, data)
|
return this.patch(`/api/workspaces/${workspaceSlug}/webhooks/${webhookId}/`, data)
|
||||||
.then((response) => response?.data)
|
.then((response) => response?.data)
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
|
import { IWebhookData, WebhookData } from "./webhook.data.store";
|
||||||
import { IWorkspaceData, WorkspaceData } from "./workspace.data.store";
|
import { IWorkspaceData, WorkspaceData } from "./workspace.data.store";
|
||||||
|
|
||||||
export class DataStore {
|
export class DataStore {
|
||||||
workspace: IWorkspaceData;
|
workspace: IWorkspaceData;
|
||||||
|
webhook: IWebhookData;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.workspace = new WorkspaceData(this);
|
this.workspace = new WorkspaceData(this);
|
||||||
|
this.webhook = new WebhookData(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
resetOnSignout() {}
|
resetOnSignOut() {}
|
||||||
}
|
}
|
||||||
|
54
web/store/dataMaps/webhook.data.store.ts
Normal file
54
web/store/dataMaps/webhook.data.store.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import { action, makeObservable, observable } from "mobx";
|
||||||
|
import { computedFn } from "mobx-utils";
|
||||||
|
import { set } from "lodash";
|
||||||
|
// store
|
||||||
|
import { DataStore } from ".";
|
||||||
|
import { WebhookModel } from "store/webhook.store";
|
||||||
|
// types
|
||||||
|
import { IWebhook } from "@plane/types";
|
||||||
|
|
||||||
|
export interface IWebhookData {
|
||||||
|
// observables
|
||||||
|
webhookMap: Record<string, WebhookModel>;
|
||||||
|
// actions
|
||||||
|
addWebhook: (webhooks: IWebhook) => void;
|
||||||
|
deleteWebhook: (webhookId: string) => void;
|
||||||
|
getWebhookById: (webhookId: string) => WebhookModel | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WebhookData implements IWebhookData {
|
||||||
|
// observables
|
||||||
|
webhookMap: Record<string, WebhookModel> = {};
|
||||||
|
// data store
|
||||||
|
dataStore;
|
||||||
|
|
||||||
|
constructor(_dataStore: DataStore) {
|
||||||
|
makeObservable(this, {
|
||||||
|
// observables
|
||||||
|
webhookMap: observable,
|
||||||
|
// actions
|
||||||
|
addWebhook: action,
|
||||||
|
deleteWebhook: action,
|
||||||
|
});
|
||||||
|
this.dataStore = _dataStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description add a webhook to the webhook map
|
||||||
|
* @param {IWebhook} webhook
|
||||||
|
*/
|
||||||
|
addWebhook = (webhook: IWebhook) => set(this.webhookMap, [webhook.id], new WebhookModel(webhook, this.dataStore));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description delete a webhook from the webhook map
|
||||||
|
* @param {string} webhookId
|
||||||
|
*/
|
||||||
|
deleteWebhook = (webhookId: string) => delete this.webhookMap[webhookId];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description get a webhook model by its id
|
||||||
|
* @param {string} webhookId
|
||||||
|
* @returns {WebhookModel | undefined} webhook model
|
||||||
|
*/
|
||||||
|
getWebhookById = computedFn((webhookId: string) => this.webhookMap[webhookId]);
|
||||||
|
}
|
@ -1,37 +1,55 @@
|
|||||||
import { makeObservable, observable } from "mobx";
|
import { action, makeObservable, observable } from "mobx";
|
||||||
import { WorkspaceModel } from "store/workspace.store";
|
import { computedFn } from "mobx-utils";
|
||||||
import { DataStore } from ".";
|
|
||||||
import { IWorkspace } from "@plane/types";
|
|
||||||
import { set } from "lodash";
|
import { set } from "lodash";
|
||||||
|
// store
|
||||||
|
import { DataStore } from ".";
|
||||||
|
import { WorkspaceModel } from "store/workspace.store";
|
||||||
|
// types
|
||||||
|
import { IWorkspace } from "@plane/types";
|
||||||
|
|
||||||
export interface IWorkspaceData {
|
export interface IWorkspaceData {
|
||||||
|
// observables
|
||||||
workspaceMap: Record<string, WorkspaceModel>;
|
workspaceMap: Record<string, WorkspaceModel>;
|
||||||
|
// actions
|
||||||
addWorkspace: (workspaces: IWorkspace) => void;
|
addWorkspace: (workspaces: IWorkspace) => void;
|
||||||
deleteWorkspace: (workspaceId: string) => void;
|
deleteWorkspace: (workspaceId: string) => void;
|
||||||
getWorkspacebyId: (workspaceId: string) => WorkspaceModel | undefined;
|
getWorkspaceById: (workspaceId: string) => WorkspaceModel | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WorkspaceData implements IWorkspaceData {
|
export class WorkspaceData implements IWorkspaceData {
|
||||||
|
// observables
|
||||||
workspaceMap: Record<string, WorkspaceModel> = {};
|
workspaceMap: Record<string, WorkspaceModel> = {};
|
||||||
|
|
||||||
// data store
|
// data store
|
||||||
dataStore;
|
dataStore;
|
||||||
|
|
||||||
constructor(_dataStore: DataStore) {
|
constructor(_dataStore: DataStore) {
|
||||||
makeObservable(this, {
|
makeObservable(this, {
|
||||||
|
// observables
|
||||||
workspaceMap: observable,
|
workspaceMap: observable,
|
||||||
|
// actions
|
||||||
|
addWorkspace: action,
|
||||||
|
deleteWorkspace: action,
|
||||||
});
|
});
|
||||||
this.dataStore = _dataStore;
|
this.dataStore = _dataStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
addWorkspace = (workspace: IWorkspace) => {
|
/**
|
||||||
|
* @description add a workspace to the workspace map
|
||||||
|
* @param {IWorkspace} workspace
|
||||||
|
*/
|
||||||
|
addWorkspace = (workspace: IWorkspace) =>
|
||||||
set(this.workspaceMap, [workspace.id], new WorkspaceModel(workspace, this.dataStore));
|
set(this.workspaceMap, [workspace.id], new WorkspaceModel(workspace, this.dataStore));
|
||||||
};
|
|
||||||
|
|
||||||
deleteWorkspace = (workspaceId: string) => {
|
/**
|
||||||
delete this.workspaceMap[workspaceId];
|
* @description delete a workspace from the workspace map
|
||||||
};
|
* @param {string} workspaceId
|
||||||
|
*/
|
||||||
|
deleteWorkspace = (workspaceId: string) => delete this.workspaceMap[workspaceId];
|
||||||
|
|
||||||
getWorkspacebyId = (workspaceId: string) => this.workspaceMap[workspaceId];
|
/**
|
||||||
|
* @description get a workspace model by its id
|
||||||
|
* @param {string} workspaceId
|
||||||
|
* @returns {WorkspaceModel | undefined} workspace model
|
||||||
|
*/
|
||||||
|
getWorkspaceById = computedFn((workspaceId: string) => this.workspaceMap[workspaceId]);
|
||||||
}
|
}
|
||||||
|
@ -12,5 +12,5 @@ export class RootStore {
|
|||||||
this.user = new UserModel(this.data);
|
this.user = new UserModel(this.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
resetOnSignout() {}
|
resetOnSignOut() {}
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,14 @@ export class UserModel implements IUserModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get workspace info from the array of workspaces in the store using workspace slug
|
* @description get workspace info from the array of workspaces in the store using workspace slug
|
||||||
* @param workspaceSlug
|
* @param {string} workspaceSlug
|
||||||
*/
|
*/
|
||||||
getWorkspaceBySlug = (workspaceSlug: string) =>
|
getWorkspaceBySlug = (workspaceSlug: string) =>
|
||||||
Object.values(this.workspaces ?? {})?.find((w) => w.slug == workspaceSlug) || null;
|
Object.values(this.workspaces ?? {})?.find((w) => w.slug == workspaceSlug) || null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetch user workspaces from API
|
* @description fetch user workspaces from API
|
||||||
*/
|
*/
|
||||||
fetchWorkspaces = async () => {
|
fetchWorkspaces = async () => {
|
||||||
const workspaceResponse = await this.workspaceService.userWorkspaces();
|
const workspaceResponse = await this.workspaceService.userWorkspaces();
|
||||||
@ -48,8 +48,8 @@ export class UserModel implements IUserModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create workspace using the workspace data
|
* @description create workspace using the workspace data
|
||||||
* @param data
|
* @param {Partial<IWorkspace>} data
|
||||||
*/
|
*/
|
||||||
createWorkspace = async (data: Partial<IWorkspace>) =>
|
createWorkspace = async (data: Partial<IWorkspace>) =>
|
||||||
await this.workspaceService.createWorkspace(data).then((response) => {
|
await this.workspaceService.createWorkspace(data).then((response) => {
|
||||||
@ -61,8 +61,8 @@ export class UserModel implements IUserModel {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* delete workspace using the workspace slug
|
* @description delete workspace using the workspace slug
|
||||||
* @param workspaceSlug
|
* @param {string} workspaceSlug
|
||||||
*/
|
*/
|
||||||
deleteWorkspace = async (workspaceSlug: string) =>
|
deleteWorkspace = async (workspaceSlug: string) =>
|
||||||
await this.workspaceService.deleteWorkspace(workspaceSlug).then(() => {
|
await this.workspaceService.deleteWorkspace(workspaceSlug).then(() => {
|
||||||
|
@ -97,11 +97,11 @@ export class WebhookModel implements IWebhookModel {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description update a webhook using the data
|
* @description update a webhook using the data
|
||||||
* @param workspaceSlug
|
* @param {string} workspaceSlug
|
||||||
* @param data
|
* @param {Partial<IWebhook>} data
|
||||||
*/
|
*/
|
||||||
updateWebhook = async (workspaceSlug: string, data: Partial<IWebhook>) => {
|
updateWebhook = async (workspaceSlug: string, data: Partial<IWebhook>) => {
|
||||||
const originalData = { ...this };
|
const originalData = this.asJSON;
|
||||||
// optimistically update the store
|
// optimistically update the store
|
||||||
runInAction(() => {
|
runInAction(() => {
|
||||||
Object.entries(data).forEach(([key, value]) => {
|
Object.entries(data).forEach(([key, value]) => {
|
||||||
|
@ -3,8 +3,10 @@ import set from "lodash/set";
|
|||||||
import { DataStore } from "./dataMaps";
|
import { DataStore } from "./dataMaps";
|
||||||
// services
|
// services
|
||||||
import { WorkspaceService } from "services/workspace.service";
|
import { WorkspaceService } from "services/workspace.service";
|
||||||
|
import { WebhookService } from "services/webhook.service";
|
||||||
// types
|
// types
|
||||||
import { IUser, IWorkspace } from "@plane/types";
|
import { IUser, IWebhook, IWorkspace } from "@plane/types";
|
||||||
|
import { IWebhookModel } from "./webhook.store";
|
||||||
|
|
||||||
export interface IWorkspaceModel {
|
export interface IWorkspaceModel {
|
||||||
// model observables
|
// model observables
|
||||||
@ -21,13 +23,20 @@ export interface IWorkspaceModel {
|
|||||||
url: string;
|
url: string;
|
||||||
updated_at: Date;
|
updated_at: Date;
|
||||||
updated_by: string;
|
updated_by: string;
|
||||||
|
// data maps
|
||||||
|
webhooks: Record<string, IWebhookModel>;
|
||||||
// computed
|
// computed
|
||||||
toJSON: IWorkspace;
|
asJSON: IWorkspace;
|
||||||
// actions
|
// workspace actions
|
||||||
updateWorkspace: (data: Partial<IWorkspace>) => Promise<IWorkspace>;
|
updateWorkspace: (data: Partial<IWorkspace>) => Promise<IWorkspace>;
|
||||||
|
// webhook actions
|
||||||
|
fetchWebhooks: () => Promise<IWebhook[]>;
|
||||||
|
createWebhook: (data: Partial<IWebhook>) => Promise<IWebhook>;
|
||||||
|
deleteWebhook: (webhookId: string) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WorkspaceModel implements IWorkspaceModel {
|
export class WorkspaceModel implements IWorkspaceModel {
|
||||||
|
// model observables
|
||||||
created_at: Date;
|
created_at: Date;
|
||||||
created_by: string;
|
created_by: string;
|
||||||
id: string;
|
id: string;
|
||||||
@ -41,10 +50,13 @@ export class WorkspaceModel implements IWorkspaceModel {
|
|||||||
updated_at: Date;
|
updated_at: Date;
|
||||||
updated_by: string;
|
updated_by: string;
|
||||||
url: string;
|
url: string;
|
||||||
|
// data maps
|
||||||
|
webhooks: Record<string, IWebhookModel> = {};
|
||||||
// root store
|
// root store
|
||||||
dataStore;
|
dataStore;
|
||||||
// services
|
// services
|
||||||
workspaceService;
|
workspaceService;
|
||||||
|
webhookService;
|
||||||
|
|
||||||
constructor(workspace: IWorkspace, _dataStore: DataStore) {
|
constructor(workspace: IWorkspace, _dataStore: DataStore) {
|
||||||
makeObservable(this, {
|
makeObservable(this, {
|
||||||
@ -62,14 +74,21 @@ export class WorkspaceModel implements IWorkspaceModel {
|
|||||||
updated_at: observable.ref,
|
updated_at: observable.ref,
|
||||||
updated_by: observable.ref,
|
updated_by: observable.ref,
|
||||||
url: observable.ref,
|
url: observable.ref,
|
||||||
|
// data maps
|
||||||
|
webhooks: observable,
|
||||||
// computed
|
// computed
|
||||||
toJSON: computed,
|
asJSON: computed,
|
||||||
// actions
|
// workspace actions
|
||||||
updateWorkspace: action,
|
updateWorkspace: action,
|
||||||
|
// webhook actions
|
||||||
|
fetchWebhooks: action,
|
||||||
|
createWebhook: action,
|
||||||
|
deleteWebhook: action,
|
||||||
});
|
});
|
||||||
this.dataStore = _dataStore;
|
this.dataStore = _dataStore;
|
||||||
// services
|
// services
|
||||||
this.workspaceService = new WorkspaceService();
|
this.workspaceService = new WorkspaceService();
|
||||||
|
this.webhookService = new WebhookService();
|
||||||
|
|
||||||
this.created_at = workspace.created_at;
|
this.created_at = workspace.created_at;
|
||||||
this.created_by = workspace.created_by;
|
this.created_by = workspace.created_by;
|
||||||
@ -89,7 +108,7 @@ export class WorkspaceModel implements IWorkspaceModel {
|
|||||||
/**
|
/**
|
||||||
* @description returns the workspace data in JSON format
|
* @description returns the workspace data in JSON format
|
||||||
*/
|
*/
|
||||||
get toJSON() {
|
get asJSON() {
|
||||||
return {
|
return {
|
||||||
created_at: this.created_at,
|
created_at: this.created_at,
|
||||||
created_by: this.created_by,
|
created_by: this.created_by,
|
||||||
@ -109,10 +128,10 @@ export class WorkspaceModel implements IWorkspaceModel {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description update workspace using the new workspace data
|
* @description update workspace using the new workspace data
|
||||||
* @param data
|
* @param {Partial<IWorkspace>} data
|
||||||
*/
|
*/
|
||||||
updateWorkspace = async (data: Partial<IWorkspace>) => {
|
updateWorkspace = async (data: Partial<IWorkspace>) => {
|
||||||
const originalData = { ...this };
|
const originalData = this.asJSON;
|
||||||
// optimistically update the store
|
// optimistically update the store
|
||||||
runInAction(() => {
|
runInAction(() => {
|
||||||
Object.entries(data).forEach(([key, value]) => {
|
Object.entries(data).forEach(([key, value]) => {
|
||||||
@ -133,4 +152,47 @@ export class WorkspaceModel implements IWorkspaceModel {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// webhook actions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description fetch all the webhooks of the workspace
|
||||||
|
*/
|
||||||
|
fetchWebhooks = async () => {
|
||||||
|
const webhooksResponse = await this.webhookService.fetchWebhooksList(this.slug);
|
||||||
|
|
||||||
|
runInAction(() => {
|
||||||
|
webhooksResponse.forEach((webhook) => {
|
||||||
|
this.dataStore.webhook.addWebhook(webhook);
|
||||||
|
set(this.webhooks, [webhook.id], this.dataStore.webhook.webhookMap[webhook.id]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return webhooksResponse;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description create webhook using the webhook data
|
||||||
|
* @param {Partial<IWebhook>} data
|
||||||
|
*/
|
||||||
|
createWebhook = async (data: Partial<IWebhook>) =>
|
||||||
|
await this.webhookService.createWebhook(this.slug, data).then((response) => {
|
||||||
|
runInAction(() => {
|
||||||
|
this.dataStore.webhook.addWebhook(response);
|
||||||
|
set(this.webhooks, [response.id], this.dataStore.webhook.webhookMap[response.id]);
|
||||||
|
});
|
||||||
|
return response;
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description delete a webhook using webhook id
|
||||||
|
* @param {string} webhookId
|
||||||
|
*/
|
||||||
|
deleteWebhook = async (webhookId: string) =>
|
||||||
|
await this.webhookService.deleteWebhook(this.slug, webhookId).then(() => {
|
||||||
|
this.dataStore.webhook.deleteWebhook(webhookId);
|
||||||
|
runInAction(() => {
|
||||||
|
delete this.webhooks[webhookId];
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user