Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimistic update for instant operations #3221

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading