Skip to content

Commit

Permalink
fix: optimistic update for instant operations (#3221)
Browse files Browse the repository at this point in the history
* fix: update functions failed case

* fix: typo
  • Loading branch information
1akhanBaheti authored Dec 22, 2023
1 parent 5c9cb05 commit ffda154
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
53 changes: 40 additions & 13 deletions web/store/page.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,39 @@ export class PageStore implements IPageStore {
* @param projectId
* @param pageId
*/
addToFavorites = async (workspaceSlug: string, projectId: string, pageId: string) =>
await this.pageService.addPageToFavorites(workspaceSlug, projectId, pageId).then(() => {
addToFavorites = async (workspaceSlug: string, projectId: string, pageId: string) => {
try {
runInAction(() => {
set(this.pages, [pageId, "is_favorite"], true);
});
});
await this.pageService.addPageToFavorites(workspaceSlug, projectId, pageId);
} catch (error) {
runInAction(() => {
set(this.pages, [pageId, "is_favorite"], false);
});
throw error;
}
};

/**
* Remove page from the users favorites list
* @param workspaceSlug
* @param projectId
* @param pageId
*/
removeFromFavorites = async (workspaceSlug: string, projectId: string, pageId: string) =>
await this.pageService.removePageFromFavorites(workspaceSlug, projectId, pageId).then(() => {
removeFromFavorites = async (workspaceSlug: string, projectId: string, pageId: string) => {
try {
runInAction(() => {
set(this.pages, [pageId, "is_favorite"], false);
});
});

await this.pageService.removePageFromFavorites(workspaceSlug, projectId, pageId);
} catch (error) {
runInAction(() => {
set(this.pages, [pageId, "is_favorite"], true);
});
throw error;
}
};
/**
* Creates a new page using the api and updated the local state in store
* @param workspaceSlug
Expand Down Expand Up @@ -287,12 +300,19 @@ export class PageStore implements IPageStore {
* @param pageId
* @returns
*/
makePublic = async (workspaceSlug: string, projectId: string, pageId: string) =>
await this.pageService.patchPage(workspaceSlug, projectId, pageId, { access: 0 }).then(() => {
makePublic = async (workspaceSlug: string, projectId: string, pageId: string) => {
try {
runInAction(() => {
set(this.pages, [pageId, "access"], 0);
});
});
await this.pageService.patchPage(workspaceSlug, projectId, pageId, { access: 0 });
} catch (error) {
runInAction(() => {
set(this.pages, [pageId, "access"], 1);
});
throw error;
}
};

/**
* Make a page private
Expand All @@ -301,12 +321,19 @@ export class PageStore implements IPageStore {
* @param pageId
* @returns
*/
makePrivate = async (workspaceSlug: string, projectId: string, pageId: string) =>
await this.pageService.patchPage(workspaceSlug, projectId, pageId, { access: 1 }).then(() => {
makePrivate = async (workspaceSlug: string, projectId: string, pageId: string) => {
try {
runInAction(() => {
set(this.pages, [pageId, "access"], 1);
});
});
await this.pageService.patchPage(workspaceSlug, projectId, pageId, { access: 1 });
} catch (error) {
runInAction(() => {
set(this.pages, [pageId, "access"], 0);
});
throw error;
}
};

/**
* Mark a page archived
Expand Down
4 changes: 3 additions & 1 deletion web/store/state.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ export class StateStore implements IStateStore {
groupedProjectStates: computed,
// computed actions
getProjectStates: action,
// actions
// fetch action
fetchProjectStates: action,
// CRUD actions
createState: action,
updateState: action,
deleteState: action,
// state actions
markStateAsDefault: action,
moveStatePosition: action,
});
Expand Down
9 changes: 4 additions & 5 deletions web/store/workspace/api-token.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ export class ApiTokenStore implements IApiTokenStore {
apiTokens: observable,
// computed actions
getApiTokenById: action,
// actions
// fetch actions
fetchApiTokens: action,
fetchApiTokenDetails: action,
// CRUD actions
createApiToken: action,
deleteApiToken: action,
});

// services
this.apiTokenService = new APITokenService();
// root store
this.rootStore = _rootStore;
// services
this.apiTokenService = new APITokenService();
}

/**
Expand Down Expand Up @@ -107,7 +107,6 @@ export class ApiTokenStore implements IApiTokenStore {
await this.apiTokenService.deleteApiToken(workspaceSlug, tokenId).then(() => {
const updatedApiTokens = { ...this.apiTokens };
delete updatedApiTokens[tokenId];

runInAction(() => {
this.apiTokens = updatedApiTokens;
});
Expand Down
2 changes: 0 additions & 2 deletions web/store/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ export class WorkspaceRootStore implements IWorkspaceRootStore {
await this.workspaceService.deleteWorkspace(workspaceSlug).then(() => {
const updatedWorkspacesList = this.workspaces;
const workspaceId = this.getWorkspaceBySlug(workspaceSlug)?.id;

delete updatedWorkspacesList[`${workspaceId}`];

runInAction(() => {
this.workspaces = updatedWorkspacesList;
});
Expand Down
5 changes: 4 additions & 1 deletion web/store/workspace/webhook.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface IWebhookStore {
) => Promise<{ webHook: IWebhook; secretKey: string | null }>;
updateWebhook: (workspaceSlug: string, webhookId: string, data: Partial<IWebhook>) => Promise<IWebhook>;
removeWebhook: (workspaceSlug: string, webhookId: string) => Promise<void>;
// secret key actions
regenerateSecretKey: (
workspaceSlug: string,
webhookId: string
Expand All @@ -47,12 +48,14 @@ export class WebhookStore implements IWebhookStore {
currentWebhook: computed,
// computed actions
getWebhookById: action,
// actions
// fetch actions
fetchWebhooks: action,
fetchWebhookById: action,
// CRUD actions
createWebhook: action,
updateWebhook: action,
removeWebhook: action,
// secret key actions
regenerateSecretKey: action,
clearSecretKey: action,
});
Expand Down

0 comments on commit ffda154

Please sign in to comment.