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

Switch to new SDK #110

Merged
merged 3 commits into from
Jan 12, 2024
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@metamask/detect-provider": "2.0.0",
"@ngneat/until-destroy": "9.2.3",
"@nguniversal/express-engine": "15.1.0",
"@build-5/interfaces": "2.1.27",
"@build-5/lib": "latest",
"@build-5/interfaces": "latest",
"@build-5/sdk": "latest",
"@tailwindcss/line-clamp": "0.4.2",
"algoliasearch": "4.14.3",
"angular-instantsearch": "4.4.0",
Expand Down
78 changes: 48 additions & 30 deletions src/app/@api/award.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {
Award,
AwardApproveParticipantRequest,
AwardCreateRequest,
AwardFundRequest,
AwardParticpateRequest,
AwardRejectRequest,
Build5Request,
Dataset,
Member,
PublicCollections,
Subset,
Timestamp,
WEN_FUNC,
WenRequest,
} from '@build-5/interfaces';
import {
AwardFilter,
AwardOwnerRepository,
AwardParticipantRepository,
AwardRepository,
} from '@build-5/lib';

import { map, Observable, of } from 'rxjs';
import { BaseApi, SOON_ENV } from './base.api';
import { AwardFilter, BaseApi } from './base.api';

export interface AwardParticipantWithMember extends Member {
comment?: string;
Expand All @@ -28,31 +27,45 @@ export interface AwardParticipantWithMember extends Member {
providedIn: 'root',
})
export class AwardApi extends BaseApi<Award> {
protected awardRepo = new AwardRepository(SOON_ENV);
protected awardOwnerRepo = new AwardOwnerRepository(SOON_ENV);
protected awardParticipantRepo = new AwardParticipantRepository(SOON_ENV);
private awardDataset = this.project.dataset(Dataset.AWARD);

constructor(protected httpClient: HttpClient) {
super(PublicCollections.AWARD, httpClient);
super(Dataset.AWARD, httpClient);
}

public listenSpace = (space: string, filter = AwardFilter.ALL) =>
this.awardRepo.getBySpaceAndFilterLive(space, filter);
public listenSpace = (space: string, filter = AwardFilter.ALL, lastValue?: string) => {
switch (filter) {
case AwardFilter.ALL:
return this.awardDataset.getBySpaceLive(space, lastValue);
case AwardFilter.ACTIVE:
return this.awardDataset.getActiveLive(space, lastValue);
case AwardFilter.COMPLETED:
return this.awardDataset.getCompletedLive(space, lastValue);
case AwardFilter.DRAFT:
return this.awardDataset.getDraftLive(space, lastValue);
case AwardFilter.REJECTED:
return this.awardDataset.getRejectedLive(space, lastValue);
}
};

public listenOwners = (award: string, lastValue?: string) =>
this.awardOwnerRepo.getAllLive(award, lastValue);
this.awardDataset.id(award).subset(Subset.OWNERS).getAllLive(lastValue);

public lastActive = (lastValue?: string) => this.awardRepo.getLastActiveLive(lastValue);
public lastActive = (lastValue?: string) => this.awardDataset.getLastActiveLive(lastValue);

public listenPendingParticipants = (award: string, lastValue?: string, searchIds?: string[]) =>
this.awardParticipantRepo
this.awardDataset
.id(award)
.subset(Subset.PARTICIPANTS)
.getParticipantsLive(award, false, searchIds, lastValue)
.pipe(
map((participants) => participants.map((p) => ({ ...p, participatedOn: p.createdOn }))),
);

public listenIssuedParticipants = (award: string, lastValue?: string, searchIds?: string[]) =>
this.awardParticipantRepo
this.awardDataset
.id(award)
.subset(Subset.PARTICIPANTS)
.getParticipantsLive(award, true, searchIds, lastValue)
.pipe(
map((participants) => participants.map((p) => ({ ...p, participatedOn: p.createdOn }))),
Expand All @@ -62,26 +75,31 @@ export class AwardApi extends BaseApi<Award> {
if (!awardId || !memberId) {
return of(false);
}
return this.awardParticipantRepo
.getByIdLive(awardId, memberId)
return this.awardDataset
.id(awardId)
.subset(Subset.PARTICIPANTS)
.subsetId(memberId)
.getLive()
.pipe(map((awardMember) => !!awardMember));
}

public create = (req: WenRequest): Observable<Award | undefined> =>
public create = (req: Build5Request<AwardCreateRequest>): Observable<Award | undefined> =>
this.request(WEN_FUNC.createAward, req);

public participate = (req: WenRequest): Observable<Award | undefined> =>
this.request(WEN_FUNC.participateAward, req);
public participate = (
req: Build5Request<AwardParticpateRequest>,
): Observable<Award | undefined> => this.request(WEN_FUNC.participateAward, req);

public approveParticipant = (req: WenRequest): Observable<Award | undefined> =>
this.request(WEN_FUNC.approveParticipantAward, req);
public approveParticipant = (
req: Build5Request<AwardApproveParticipantRequest>,
): Observable<Award | undefined> => this.request(WEN_FUNC.approveParticipantAward, req);

public approve = (req: WenRequest): Observable<Award | undefined> =>
public approve = (req: Build5Request<AwardFundRequest>): Observable<Award | undefined> =>
this.request(WEN_FUNC.fundAward, req);

public reject = (req: WenRequest): Observable<Award | undefined> =>
public reject = (req: Build5Request<AwardRejectRequest>): Observable<Award | undefined> =>
this.request(WEN_FUNC.rejectAward, req);

public fundAndMint = (req: WenRequest): Observable<Award | undefined> =>
public fundAndMint = (req: Build5Request<AwardFundRequest>): Observable<Award | undefined> =>
this.request(WEN_FUNC.fundAward, req);
}
39 changes: 25 additions & 14 deletions src/app/@api/base.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { environment } from '@env/environment';
import {
NetworkAddress,
PublicCollections,
BUILD5_PROD_ADDRESS_API,
BUILD5_TEST_ADDRESS_API,
WEN_FUNC,
WenRequest,
Build5Request,
Dataset,
} from '@build-5/interfaces';
import { Build5Env, CrudRepository } from '@build-5/lib';
import { Observable, map, of } from 'rxjs';
import { https, Build5, SoonaverseApiKey } from '@build-5/sdk';
import { Observable, of } from 'rxjs';

export const DEFAULT_LIST_SIZE = 50;
export const WHERE_IN_BATCH = 10;
Expand All @@ -18,24 +18,35 @@
export const FULL_TODO_CHANGE_TO_PAGING = FULL_LIST;
export const FULL_TODO_MOVE_TO_PROTOCOL = FULL_LIST;

export const SOON_ENV = environment.production ? Build5Env.PROD : Build5Env.TEST;
export const ORIGIN = environment.production ? Build5.PROD : Build5.TEST;
export const API_KEY = SoonaverseApiKey[ORIGIN];

export enum AwardFilter {
ALL = 'all',
DRAFT = 'draft',
ACTIVE = 'active',
COMPLETED = 'completed',
REJECTED = 'rejected',
}

export class BaseApi<T> {
protected repo: CrudRepository<T>;
protected project = https(ORIGIN).project(API_KEY);

constructor(public readonly collection: PublicCollections, protected httpClient: HttpClient) {
this.repo = new CrudRepository(SOON_ENV, this.collection);
}
constructor(public readonly dataset: Dataset, protected httpClient: HttpClient) {}

public listen = (id: string) => this.repo.getByIdLive(id);
public listen = (id: string): Observable<T> =>
this.project.dataset(this.dataset).id(id).getLive() as Observable<T>;

public listenMultiple = (ids: NetworkAddress[]) =>
ids.length ? this.repo.getManyByIdLive(ids) : of([]);
public listenMultiple = (ids: NetworkAddress[]): Observable<T[]> =>
ids.length
? (this.project.dataset(this.dataset).getManyByIdLive(ids) as Observable<T[]>)
: of([]);

public top = (lastValue?: string, limit?: number) => this.repo.getTopLive(lastValue, limit);
public top = (lastValue?: string, limit?: number): Observable<T[]> =>
this.project.dataset(this.dataset).getTopLive(lastValue, limit) as Observable<T[]>;

protected request<T>(func: WEN_FUNC, req: WenRequest): Observable<T | undefined> {
protected request<T>(func: WEN_FUNC, req: Build5Request<any>): Observable<T | undefined> {

Check warning on line 48 in src/app/@api/base.api.ts

View workflow job for this annotation

GitHub Actions / front_end_es_lint

Unexpected any. Specify a different type
const origin = environment.production ? BUILD5_PROD_ADDRESS_API : BUILD5_TEST_ADDRESS_API;
return <any>this.httpClient.post(origin + func, req);

Check warning on line 50 in src/app/@api/base.api.ts

View workflow job for this annotation

GitHub Actions / front_end_es_lint

Unexpected any. Specify a different type
}
}
57 changes: 36 additions & 21 deletions src/app/@api/collection.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {
Collection,
PublicCollections,
Dataset,
Transaction,
WEN_FUNC,
WenRequest,
Build5Request,
CollectionMintRequest,
VoteRequest,
RankRequest,
CreateCollectionRequest,
UpdateCollectionRequest,
RejectCollectionRequest,
Subset,
} from '@build-5/interfaces';
import { CollectionRepository, CollectionStatsRepository } from '@build-5/lib';
import { Observable, of } from 'rxjs';
import { BaseApi, SOON_ENV } from './base.api';
import { BaseApi } from './base.api';

export enum CollectionFilter {
ALL = 'all',
Expand All @@ -22,40 +28,49 @@ export enum CollectionFilter {
providedIn: 'root',
})
export class CollectionApi extends BaseApi<Collection> {
protected colRepo = new CollectionRepository(SOON_ENV);
protected colStatRepo = new CollectionStatsRepository(SOON_ENV);
private collectionDataset = this.project.dataset(Dataset.COLLECTION);

constructor(protected httpClient: HttpClient) {
super(PublicCollections.COLLECTION, httpClient);
super(Dataset.COLLECTION, httpClient);
}

public mintCollection = (req: WenRequest): Observable<Transaction | undefined> =>
this.request(WEN_FUNC.mintCollection, req);
public mintCollection = (
req: Build5Request<CollectionMintRequest>,
): Observable<Transaction | undefined> => this.request(WEN_FUNC.mintCollection, req);

public vote = (req: WenRequest): Observable<Transaction | undefined> =>
public vote = (req: Build5Request<VoteRequest>): Observable<Transaction | undefined> =>
this.request(WEN_FUNC.voteController, req);

public rank = (req: WenRequest): Observable<Transaction | undefined> =>
public rank = (req: Build5Request<RankRequest>): Observable<Transaction | undefined> =>
this.request(WEN_FUNC.rankController, req);

public stats = (collectionId: string) =>
collectionId ? this.colStatRepo.getByIdLive(collectionId, collectionId) : of(undefined);
collectionId
? this.collectionDataset
.id(collectionId)
.subset(Subset.STATS)
.subsetId(collectionId)
.getLive()
: of(undefined);

public allPendingSpace = (space: string, lastValue?: string) =>
this.colRepo.getAllPendingLive(space, lastValue);
this.collectionDataset.getAllPendingLive(space, lastValue);

public allAvailableSpace = (space: string, lastValue?: string) =>
this.colRepo.getAllAvailableLive(space, lastValue);
this.collectionDataset.getAllAvailableLive(space, lastValue);

public allRejectedSpace = (space: string, lastValue?: string) =>
this.colRepo.getAllRejectedLive(space, lastValue);
this.collectionDataset.getAllRejectedLive(space, lastValue);

public create = (req: WenRequest): Observable<Collection | undefined> =>
this.request(WEN_FUNC.createCollection, req);
public create = (
req: Build5Request<CreateCollectionRequest>,
): Observable<Collection | undefined> => this.request(WEN_FUNC.createCollection, req);

public update = (req: WenRequest): Observable<Collection | undefined> =>
this.request(WEN_FUNC.updateCollection, req);
public update = (
req: Build5Request<UpdateCollectionRequest>,
): Observable<Collection | undefined> => this.request(WEN_FUNC.updateCollection, req);

public reject = (req: WenRequest): Observable<Collection | undefined> =>
this.request(WEN_FUNC.rejectCollection, req);
public reject = (
req: Build5Request<RejectCollectionRequest>,
): Observable<Collection | undefined> => this.request(WEN_FUNC.rejectCollection, req);
}
Loading
Loading