@streamlayer/feature-gamification 0.18.3 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ import '@streamlayer/sdk-web-core/store';
5
5
  import type { PlainMessage } from '@bufbuild/protobuf';
6
6
  import { WritableAtom } from 'nanostores';
7
7
  import * as queries from './queries';
8
+ import { leaderboard } from './leaderboard';
8
9
  /**
9
10
  * Required: in-app should be displayed and questions not available
10
11
  * Optional: in-app should be displayed but questions are available
@@ -37,7 +38,7 @@ export declare class Gamification extends AbstractFeature<'games', PlainMessage<
37
38
  /** pinned leaderboard id */
38
39
  leaderboardId: WritableAtom<string | undefined>;
39
40
  /** leaderboard list */
40
- leaderboardList: ApiStore<GetApiResponseType<typeof queries.$leaderboardList>>;
41
+ leaderboardList: ReturnType<typeof leaderboard>;
41
42
  /** onboarding status */
42
43
  onboardingStatus: WritableAtom<OnboardingStatus | undefined>;
43
44
  /** opened question */
@@ -5,6 +5,7 @@ import '@streamlayer/sdk-web-core/store';
5
5
  import * as queries from './queries';
6
6
  import * as actions from './queries/actions';
7
7
  import { GamificationStorage } from './storage';
8
+ import { leaderboard } from './leaderboard';
8
9
  import { gamificationBackground } from './';
9
10
  const GamificationQuestionTypes = new Set([QuestionType.POLL, QuestionType.PREDICTION, QuestionType.TRIVIA]);
10
11
  /**
@@ -71,7 +72,7 @@ export class Gamification extends AbstractFeature {
71
72
  }
72
73
  return { loading: openedQuestion.loading, data: undefined };
73
74
  });
74
- this.leaderboardList = new ApiStore(queries.$leaderboardList(this.background.slStreamId, this.leaderboardId, instance.transport), 'gamification:leaderboardList');
75
+ this.leaderboardList = leaderboard(this.transport, this.background.slStreamId);
75
76
  this.onboardingStatus.subscribe((onboardingStatus) => {
76
77
  if (onboardingStatus === OnboardingStatus.Optional || OnboardingStatus.Required) {
77
78
  this.showOnboardingInApp();
@@ -0,0 +1,17 @@
1
+ import type { Transport } from '@streamlayer/sdk-web-api';
2
+ import { ReadableAtom } from 'nanostores';
3
+ import { type LeaderboardItem } from './queries/leaderboard';
4
+ type LeaderboardOptions = {
5
+ pageSize?: number;
6
+ };
7
+ type LeaderboardStore = {
8
+ data?: LeaderboardItem[];
9
+ loading?: boolean;
10
+ error?: string;
11
+ };
12
+ export declare const leaderboard: (transport: Transport, $eventId: ReadableAtom<string | undefined>, options?: LeaderboardOptions) => {
13
+ $store: import("nanostores").MapStore<LeaderboardStore>;
14
+ fetchMore: () => void;
15
+ invalidate: () => void;
16
+ };
17
+ export {};
@@ -0,0 +1,48 @@
1
+ import { createSingleStore, createMapStore } from '@streamlayer/sdk-web-interfaces';
2
+ import { createLeaderboardListFetch } from './queries/leaderboard';
3
+ const defaultOptions = {
4
+ pageSize: 10,
5
+ };
6
+ export const leaderboard = (transport, $eventId, options) => {
7
+ const $pagination = createSingleStore({ pageSize: options?.pageSize || defaultOptions.pageSize, page: 0 });
8
+ const $store = createMapStore({ data: [], loading: undefined, error: undefined });
9
+ const fetch = createLeaderboardListFetch(transport);
10
+ const refetch = async (eventId) => {
11
+ if (eventId) {
12
+ $store.setKey('loading', true);
13
+ const request = {
14
+ eventId: eventId,
15
+ pagination: { page: 0, pageSize: options?.pageSize || defaultOptions.pageSize },
16
+ };
17
+ const newData = await fetch(request);
18
+ $store.set({ data: newData.data.map((item) => item.attributes), loading: false });
19
+ $pagination.set(request.pagination);
20
+ }
21
+ };
22
+ $eventId.listen(refetch);
23
+ const invalidate = () => {
24
+ void refetch($eventId.get());
25
+ };
26
+ $pagination.listen(async (pagination) => {
27
+ const eventId = $eventId.get();
28
+ const prevDataLength = $store.get().data?.length || 0;
29
+ if (pagination.page > 0 && eventId && prevDataLength >= pagination.page * pagination.pageSize) {
30
+ $store.setKey('loading', true);
31
+ const request = {
32
+ eventId: eventId,
33
+ pagination,
34
+ };
35
+ const newData = await fetch(request);
36
+ const prevData = $store.get().data || [];
37
+ $store.set({
38
+ data: [...prevData, ...newData.data.map((item) => item.attributes)],
39
+ loading: false,
40
+ });
41
+ }
42
+ });
43
+ const fetchMore = () => {
44
+ const pagination = $pagination.get();
45
+ $pagination.set({ ...pagination, page: pagination.page + 1 });
46
+ };
47
+ return { $store, fetchMore, invalidate };
48
+ };
@@ -1,4 +1,8 @@
1
1
  import type { Transport } from '@streamlayer/sdk-web-api';
2
2
  import { ReadableAtom } from 'nanostores';
3
+ import { ListRequest } from '@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_pb';
4
+ import { PartialMessage } from '@bufbuild/protobuf';
5
+ export { LeaderboardItem } from '@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_pb';
3
6
  export declare const $userSummary: ($eventId: ReadableAtom<string | undefined>, $userId: ReadableAtom<string | undefined>, transport: Transport) => import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_pb").LeaderboardSummaryItem | undefined, any>;
4
- export declare const $leaderboardList: ($eventId: ReadableAtom<string | undefined>, $leaderboardId: ReadableAtom<string | undefined>, transport: Transport) => import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_pb").ListResponse_ListResponseData[], any>;
7
+ export declare const $leaderboardList: ($eventId: ReadableAtom<string | undefined>, _: ReadableAtom<string | undefined>, transport: Transport) => import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_pb").ListResponse_ListResponseData[], any>;
8
+ export declare const createLeaderboardListFetch: (transport: Transport) => (params: PartialMessage<ListRequest>) => Promise<import("@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_pb").ListResponse>;
@@ -1,4 +1,5 @@
1
1
  import { Leaderboard } from '@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_connect';
2
+ export { LeaderboardItem } from '@streamlayer/sl-eslib/interactive/leaderboard/interactive.leaderboard_pb';
2
3
  export const $userSummary = ($eventId, $userId, transport) => {
3
4
  const { client, queryKey } = transport.createPromiseClient(Leaderboard, {
4
5
  method: 'summary',
@@ -14,22 +15,22 @@ export const $userSummary = ($eventId, $userId, transport) => {
14
15
  },
15
16
  });
16
17
  };
17
- export const $leaderboardList = ($eventId, $leaderboardId, transport) => {
18
+ export const $leaderboardList = ($eventId, _, transport) => {
18
19
  const { client, queryKey } = transport.createPromiseClient(Leaderboard, {
19
20
  method: 'list',
20
- params: [$eventId, $leaderboardId],
21
+ params: [$eventId],
21
22
  });
22
23
  return transport.nanoquery.createFetcherStore(queryKey, {
23
- fetcher: async (_, __, eventId, leaderboardId) => {
24
- const params = { usersIds: [] };
25
- if (eventId) {
26
- params.eventId = eventId;
24
+ fetcher: async (_, __, eventId) => {
25
+ if (!eventId) {
26
+ return [];
27
27
  }
28
- if (leaderboardId) {
29
- params.leaderboardId = leaderboardId;
30
- }
31
- const res = await client.list(params);
28
+ const res = await client.list({ eventId: eventId });
32
29
  return res.data;
33
30
  },
34
31
  });
35
32
  };
33
+ export const createLeaderboardListFetch = (transport) => {
34
+ const { client } = transport.createPromiseClient(Leaderboard, { method: 'list' });
35
+ return (params) => client.list(params);
36
+ };
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@streamlayer/feature-gamification",
3
- "version": "0.18.3",
3
+ "version": "0.19.0",
4
4
  "peerDependencies": {
5
5
  "@bufbuild/protobuf": "^1.4.2",
6
6
  "@streamlayer/sl-eslib": "^5.52.0",
7
7
  "nanostores": "^0.9.5",
8
- "@streamlayer/sdk-web-interfaces": "^0.18.5",
9
- "@streamlayer/sdk-web-core": "^0.16.0",
8
+ "@streamlayer/sdk-web-interfaces": "^0.18.7",
9
+ "@streamlayer/sdk-web-core": "^0.16.2",
10
10
  "@streamlayer/sdk-web-api": "^0.1.0",
11
- "@streamlayer/sdk-web-types": "^0.16.5",
12
- "@streamlayer/sdk-web-storage": "^0.3.7",
13
- "@streamlayer/sdk-web-logger": "^0.5.7",
14
- "@streamlayer/sdk-web-notifications": "^0.10.4"
11
+ "@streamlayer/sdk-web-types": "^0.16.7",
12
+ "@streamlayer/sdk-web-storage": "^0.3.9",
13
+ "@streamlayer/sdk-web-logger": "^0.5.9",
14
+ "@streamlayer/sdk-web-notifications": "^0.10.6"
15
15
  },
16
16
  "devDependencies": {
17
17
  "tslib": "^2.6.2"