Просмотр исходного кода

Use audit log hook as template and define search filter

arvid-e 6 месяцев назад
Родитель
Сommit
e30c374076
2 измененных файлов с 36 добавлено и 0 удалено
  1. 16 0
      apps/app/src/interfaces/activity.ts
  2. 20 0
      apps/app/src/stores/recent-activity.ts

+ 16 - 0
apps/app/src/interfaces/activity.ts

@@ -668,3 +668,19 @@ export type ISearchFilter = {
   dates?: { startDate: string | null; endDate: string | null };
   dates?: { startDate: string | null; endDate: string | null };
   actions?: SupportedActionType[];
   actions?: SupportedActionType[];
 };
 };
+
+
+// FIX: change to API supported types when API is done
+type SupportedRecentActivityAction = typeof ActivityLogActions[keyof typeof ActivityLogActions];
+
+export type IRecentActivitySearchFilter = {
+  actions?: SupportedRecentActivityAction[];
+  usernames?: string[];
+  userIds?: string[];
+  dates?: {
+    startDate?: string;
+    endDate?: string;
+  };
+
+  targetModels?: string[];
+};

+ 20 - 0
apps/app/src/stores/recent-activity.ts

@@ -0,0 +1,20 @@
+import type { SWRResponse } from 'swr';
+import useSWRImmutable from 'swr/immutable';
+
+import { apiv3Get } from '~/client/util/apiv3-client';
+import type { IActivityHasId, IRecentActivitySearchFilter } from '~/interfaces/activity';
+import type { PaginateResult } from '~/interfaces/mongoose-utils';
+
+export const useSWRxRecentActivity = (limit?: number, offset?: number, searchFilter?: IRecentActivitySearchFilter):
+  SWRResponse<PaginateResult<IActivityHasId>, Error> => {
+
+  const stringifiedSearchFilter = JSON.stringify(searchFilter);
+
+  const key = ['/user-activities', limit, offset, stringifiedSearchFilter];
+
+  const fetcher = ([endpoint, limit, offset, stringifiedSearchFilter]) => apiv3Get(endpoint, { limit, offset, searchFilter: stringifiedSearchFilter })
+    .then(result => result.data.serializedPaginationResult);
+
+
+  return useSWRImmutable(key, fetcher);
+};