Skip to content

Commit

Permalink
chore: added apis
Browse files Browse the repository at this point in the history
  • Loading branch information
gakshita committed Jan 6, 2025
1 parent 314f714 commit afa330e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 49 deletions.
7 changes: 7 additions & 0 deletions packages/types/src/home.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ export type TLinkMap = {
export type TLinkIdMap = {
[workspace_slug: string]: string[];
};

export type TWidgetEntityData = {
key: string;
name: string;
is_enabled: boolean;
sort_order: number;
};
4 changes: 2 additions & 2 deletions web/core/components/home/home-dashboard-widgets.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { observer } from "mobx-react";
import { useParams } from "next/navigation";
// types
import { THomeWidgetKeys, THomeWidgetProps, TWidgetKeys } from "@plane/types";
import { THomeWidgetKeys, THomeWidgetProps } from "@plane/types";
// hooks
import { useHome } from "@/hooks/store/use-home";
// components
import { HomePageHeader } from "@/plane-web/components/home/header";
import { StickiesWidget } from "@/plane-web/components/stickies";
import { RecentActivityWidget } from "./widgets";
import { DashboardQuickLinks } from "./widgets/links";
import { ManageWidgetsModal } from "./widgets/manage";
import { StickiesWidget } from "@/plane-web/components/stickies";

const WIDGETS_LIST: {
[key in THomeWidgetKeys]: { component: React.FC<THomeWidgetProps>; fullWidth: boolean };
Expand Down
6 changes: 3 additions & 3 deletions web/core/components/home/user-greetings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export const UserGreetingsView: FC<IUserGreetingsView> = (props) => {
{weekDay}, {date} {timeString}
</div>
</h6>
</div>{" "}
<button
</div>
{/* <button
onClick={handleWidgetModal}
className="flex items-center gap-2 font-medium text-custom-text-300 justify-center border border-custom-border-200 rounded p-2 my-auto mb-0"
>
<Shapes size={16} />
<div className="text-xs font-medium">Manage widgets</div>
</button>
</button> */}
</div>
);
};
24 changes: 23 additions & 1 deletion web/core/services/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TLink,
TSearchResponse,
TSearchEntityRequestPayload,
TWidgetEntityData,
} from "@plane/types";
import { APIService } from "@/services/api.service";
// helpers
Expand Down Expand Up @@ -306,7 +307,7 @@ export class WorkspaceService extends APIService {
});
}

async deleteWorkspaceLink(workspaceSlug: string, linkId: string): Promise<any> {
async deleteWorkspaceLink(workspaceSlug: string, linkId: string): Promise<void> {
return this.delete(`/api/workspaces/${workspaceSlug}/quick-links/${linkId}/`)
.then((response) => response?.data)
.catch((error) => {
Expand Down Expand Up @@ -339,4 +340,25 @@ export class WorkspaceService extends APIService {
throw error?.response;
});
}

// widgets
async fetchWorkspaceWidgets(workspaceSlug: string): Promise<TWidgetEntityData[]> {
return this.get(`/api/workspaces/${workspaceSlug}/home-preferences/`)
.then((response) => response?.data)
.catch((error) => {
throw error?.response;
});
}

async updateWorkspaceWidget(
workspaceSlug: string,
widgetKey: string,
data: Partial<TWidgetEntityData>
): Promise<TWidgetEntityData> {
return this.patch(`/api/workspaces/${workspaceSlug}/home-preferences/${widgetKey}/`, data)
.then((response) => response?.data)
.catch((error) => {
throw error?.response;
});
}
}
115 changes: 72 additions & 43 deletions web/core/store/workspace/home.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import orderBy from "lodash/orderBy";
import set from "lodash/set";
import { action, makeObservable, observable, runInAction } from "mobx";
import { IWorkspaceLinkStore, WorkspaceLinkStore } from "./link.store";
import { TWidgetEntityData } from "@plane/types";
import { WorkspaceService } from "@/plane-web/services";
import { IWorkspaceLinkStore, WorkspaceLinkStore } from "./link.store";

export interface IHomeStore {
// observables
showWidgetSettings: boolean;
widgetsMap: Record<string, any>;
widgetsMap: Record<string, TWidgetEntityData>;
//stores
quickLinks: IWorkspaceLinkStore;
// actions
toggleWidgetSettings: (value?: boolean) => void;
reorderWidgets: (workspaceSlug: string, widgetId: string, destinationId: string, edge: string | undefined) => void;
fetchWidgets: (workspaceSlug: string) => Promise<void>;
reorderWidget: (workspaceSlug: string, widgetKey: string, destinationId: string, edge: string | undefined) => void;
toggleWidget: (workspaceSlug: string, widgetKey: string, is_enabled: boolean) => void;
}

export class HomeStore implements IHomeStore {
// observables
showWidgetSettings = false;
widgetsMap: Record<string, any> = {};
widgetsMap: Record<string, TWidgetEntityData> = {};
widgets: string[] = [];
// stores
quickLinks: IWorkspaceLinkStore;
// services
Expand All @@ -27,9 +33,12 @@ export class HomeStore implements IHomeStore {
// observables
showWidgetSettings: observable,
widgetsMap: observable,
widgets: observable,
// actions
toggleWidgetSettings: action,
reorderWidgets: action,
fetchWidgets: action,
reorderWidget: action,
toggleWidget: action,
});
// services
this.workspaceService = new WorkspaceService();
Expand All @@ -42,44 +51,64 @@ export class HomeStore implements IHomeStore {
this.showWidgetSettings = value !== undefined ? value : !this.showWidgetSettings;
};

reorderWidgets = async (workspaceSlug: string, widgetId: string, destinationId: string, edge: string | undefined) => {
// try {
// let resultSequence = 10000;
// if (edge) {
// const sortedIds = orderBy(Object.values(this.favoriteMap), "sequence", "desc").map((fav) => fav.id);
// const destinationSequence = this.favoriteMap[destinationId]?.sequence || undefined;
// if (destinationSequence) {
// const destinationIndex = sortedIds.findIndex((id) => id === destinationId);
// if (edge === "reorder-above") {
// const prevSequence = this.favoriteMap[sortedIds[destinationIndex - 1]]?.sequence || undefined;
// if (prevSequence) {
// resultSequence = (destinationSequence + prevSequence) / 2;
// } else {
// resultSequence = destinationSequence + resultSequence;
// }
// } else {
// resultSequence = destinationSequence - resultSequence;
// }
// }
// }
// await this.dashboardService.updateDashboardWidget(workspaceSlug, dashboardId, widgetId, {
// sequence: resultSequence,
// });
// runInAction(() => {
// set(this.favoriteMap, [favoriteId, "sequence"], resultSequence);
// });
// } catch (error) {
// console.error("Failed to move favorite folder");
// throw error;
// }
fetchWidgets = async (workspaceSlug: string) => {
try {
const widgets = await this.workspaceService.fetchWorkspaceWidgets(workspaceSlug);
runInAction(() => {
this.widgets = widgets.map((widget) => widget.key);
widgets.forEach((widget) => {
this.widgetsMap[widget.key] = widget;
});
});
} catch (error) {
console.error("Failed to fetch widgets");
throw error;
}
};

// fetchRecentActivity = async (workspaceSlug: string) => {
// try {
// const response = await this.workspaceService.fetchWorkspaceRecents(workspaceSlug);
// } catch (error) {
// console.error("Failed to fetch recent activity");
// throw error;
// }
// };
toggleWidget = async (workspaceSlug: string, widgetKey: string, is_enabled: boolean) => {
try {
await this.workspaceService.updateWorkspaceWidget(workspaceSlug, widgetKey, {
is_enabled,
});
runInAction(() => {
this.widgetsMap[widgetKey].is_enabled = is_enabled;
});
} catch (error) {
console.error("Failed to toggle widget");
throw error;
}
};

reorderWidget = async (workspaceSlug: string, widgetKey: string, destinationId: string, edge: string | undefined) => {
try {
let resultSequence = 10000;
if (edge) {
const sortedIds = orderBy(Object.values(this.widgetsMap), "sort_order", "desc").map((widget) => widget.key);
const destinationSequence = this.widgetsMap[destinationId]?.sort_order || undefined;
if (destinationSequence) {
const destinationIndex = sortedIds.findIndex((id) => id === destinationId);
if (edge === "reorder-above") {
const prevSequence = this.widgetsMap[sortedIds[destinationIndex - 1]]?.sort_order || undefined;
if (prevSequence) {
resultSequence = (destinationSequence + prevSequence) / 2;
} else {
resultSequence = destinationSequence + resultSequence;
}
} else {
resultSequence = destinationSequence - resultSequence;
}
}
}
await this.workspaceService.updateWorkspaceWidget(workspaceSlug, widgetKey, {
sort_order: resultSequence,
});
runInAction(() => {
set(this.widgetsMap, [widgetKey, "sort_order"], resultSequence);
});
} catch (error) {
console.error("Failed to move widget");
throw error;
}
};
}

0 comments on commit afa330e

Please sign in to comment.