Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
feat(link): allow linking to shared projects (fixes #128)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Jun 12, 2018
1 parent ac8dc9e commit 44fe669
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ <h1 mat-dialog-title>Link your Project</h1>

<mat-list *ngIf="games | async">
<mat-list-item *ngFor="let game of (games | async)">
<h4 matLine>{{ game.name }}</h4>
<h4 matLine>
{{ game.name }}
<span class="shared" *ngIf="game.isShared" title="Shared with you">Shared</span>
</h4>
<p matLine>Updated: {{ game.updatedAt | amTimeAgo }}</p>
<button mat-raised-button color="primary" *ngIf="(linkedGame | async)?.id !== game.id "
(click)="link(game)">Link</button>
Expand Down
13 changes: 13 additions & 0 deletions src/app/editor/project/link-dialog/link-dialog.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@import '../../../assets/styles/declarations';

.shared {
$font-size: 0.7em;
$vertical-margin: 1em * (1em / $font-size - 1) / 2;

font-size: $font-size;
display: inline-block;
padding: $vertical-margin 0.3em;
background: $color-primary2;
border-radius: 2px;
margin: $vertical-margin 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as fromProject from '../project.reducer';
selector: 'link-dialog',
templateUrl: './link-dialog.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrls: ['./link-dialog.component.scss'],
})
export class LinkProjectDialogComponent {
/**
Expand Down
1 change: 1 addition & 0 deletions src/app/editor/project/project.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface IInteractiveVersion {
export interface IInteractiveGame {
id: number;
name: string;
isShared: boolean;
versions: IInteractiveVersion[];
}

Expand Down
43 changes: 27 additions & 16 deletions src/server/project-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IFullInteractiveVersion, IInteractiveGame } from '../app/editor/project
import { IWorld } from '../app/editor/schema/schema.actions';
import { UnexpectedHttpError } from './errors';
import { Project } from './project';
import { Fetcher } from './util';
import { Fetcher, IRequester } from './util';

const storageKey = 'linkedInteractiveGame';

Expand All @@ -20,22 +20,14 @@ export class ProjectLinker {
const user = await this.project.profile.user();
const fetcher = new Fetcher().with(await this.project.profile.tokens());

let output: IInteractiveGame[] = [];
for (let page = 0; true; page++) {
// tslint:disable-line
const next = await fetcher.json(
'get',
`/interactive/games/owned?user=${user.id}&page=${page}`,
);
const contents = await next.json();
if (!contents.length) {
break;
}
const [owned, shared] = await Promise.all([
this.enumerateUponEndpoint(fetcher, `/interactive/games/owned?user=${user.id}`),
this.enumerateUponEndpoint(fetcher, `/interactive/games/editor?user=${user.id}`),
]);

output = output.concat(contents);
}

return output;
return owned
.map(g => ({ ...g, isShared: false }))
.concat(shared.map(g => ({ ...g, isShared: true })));
}

/**
Expand Down Expand Up @@ -99,4 +91,23 @@ export class ProjectLinker {
throw new UnexpectedHttpError(response, await response.text());
}
}

private async enumerateUponEndpoint(
fetcher: IRequester,
endpoint: string,
): Promise<IInteractiveGame[]> {
let output: IInteractiveGame[] = [];
for (let page = 0; true; page++) {
// tslint:disable-line
const next = await fetcher.json('get', `${endpoint}&page=${page}`);
const contents = await next.json();
if (!contents.length) {
break;
}

output = output.concat(contents);
}

return output;
}
}

0 comments on commit 44fe669

Please sign in to comment.