Skip to content

Commit

Permalink
Merge pull request #2209 from daostack/feature/CW-2183-commons-sorting
Browse files Browse the repository at this point in the history
Sort commons by lastActivity in the dropdowns #2183
  • Loading branch information
pvm-code authored Oct 20, 2023
2 parents ebf506b + 8b89853 commit e84980a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/shared/models/Common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Discussion } from "./Discussion";
import { DiscussionMessage } from "./DiscussionMessage";
import { PaymentAmount } from "./Payment";
import { Proposal } from "./Proposals";
import { Timestamp } from "./Timestamp";
import { User } from "./User";
import {
AllowedActions,
Expand Down Expand Up @@ -119,6 +120,8 @@ export interface Common extends BaseEntity {
hasPublicItems: boolean;

rootCommonId?: string;

lastActivity?: Timestamp;
}

export interface Project extends Common {
Expand Down
15 changes: 15 additions & 0 deletions src/shared/utils/compareCommonsByLastActivity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Common } from "@/shared/models";

export const compareCommonsByLastActivity = (
prevCommon: Common,
nextCommon: Common,
): number => {
if (!nextCommon.lastActivity) {
return -1;
}
if (!prevCommon.lastActivity) {
return 1;
}

return nextCommon.lastActivity.seconds - prevCommon.lastActivity.seconds;
};
1 change: 1 addition & 0 deletions src/shared/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from "./parseLinksForSubmission";
export * from "./proposals";
export * from "./queryParams";
export { default as request } from "./request";
export * from "./compareCommonsByLastActivity";
export * from "./convertDatesToFirestoreTimestamps";
export * from "./convertLinkToUploadFile";
export * from "./timeAgo";
Expand Down
12 changes: 7 additions & 5 deletions src/store/states/commonLayout/saga/getCommons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { selectUser } from "@/pages/Auth/store/selectors";
import { CommonService, GovernanceService, ProjectService } from "@/services";
import { Awaited } from "@/shared/interfaces";
import { User } from "@/shared/models";
import { isError } from "@/shared/utils";
import { compareCommonsByLastActivity, isError } from "@/shared/utils";
import { ProjectsStateItem } from "../../projects";
import * as actions from "../actions";
import { getPermissionsDataByAllUserCommonMemberInfo } from "./utils";
Expand Down Expand Up @@ -80,17 +80,19 @@ export function* getCommons(
commonId,
userId,
)) as Awaited<ReturnType<typeof getProjectsInfo>>;
const projectsData: ProjectsStateItem[] = data.map(
({ common, hasMembership, hasPermissionToAddProject }) => ({
const projectsData: ProjectsStateItem[] = [...data]
.sort((prevItem, nextItem) =>
compareCommonsByLastActivity(prevItem.common, nextItem.common),
)
.map(({ common, hasMembership, hasPermissionToAddProject }) => ({
commonId: common.id,
image: common.image,
name: common.name,
directParent: common.directParent,
hasMembership,
hasPermissionToAddProject,
notificationsAmount: 0,
}),
);
}));

yield put(
actions.getCommons.success({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CommonService, GovernanceService, ProjectService } from "@/services";
import { InboxItemType } from "@/shared/constants";
import { Awaited } from "@/shared/interfaces";
import { Common, User } from "@/shared/models";
import { compareCommonsByLastActivity } from "@/shared/utils";
import { getPermissionsDataByAllUserCommonMemberInfo } from "../../commonLayout/saga/utils";
import * as actions from "../actions";
import { selectMultipleSpacesLayoutBreadcrumbs } from "../selectors";
Expand Down Expand Up @@ -102,16 +103,18 @@ export function* fetchBreadcrumbsItemsByCommonId(
commonId,
user?.uid,
)) as Awaited<ReturnType<typeof fetchProjectsInfoByActiveCommonId>>;
const projectsData: ProjectsStateItem[] = projectsInfo.map(
({ common, hasMembership, hasPermissionToAddProject }) => ({
const projectsData: ProjectsStateItem[] = [...projectsInfo]
.sort((prevItem, nextItem) =>
compareCommonsByLastActivity(prevItem.common, nextItem.common),
)
.map(({ common, hasMembership, hasPermissionToAddProject }) => ({
commonId: common.id,
image: common.image,
name: common.name,
directParent: common.directParent,
hasMembership,
hasPermissionToAddProject,
}),
);
}));

const currentBreadcrumbs = (yield select(
selectMultipleSpacesLayoutBreadcrumbs,
Expand Down

0 comments on commit e84980a

Please sign in to comment.