Skip to content

Commit e4f0d12

Browse files
committed
use card-resource to create new instance
1 parent 8f0a636 commit e4f0d12

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

packages/host/app/components/operator-mode/code-submode/playground-panel.gts

+14-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { action } from '@ember/object';
44
import type Owner from '@ember/owner';
55
import { service } from '@ember/service';
66
import Component from '@glimmer/component';
7+
import { tracked } from '@glimmer/tracking';
78

89
import Folder from '@cardstack/boxel-icons/folder';
910
import { task } from 'ember-concurrency';
@@ -94,7 +95,7 @@ const SelectedItem: TemplateOnlyComponent<{ Args: { title?: string } }> =
9495
</style>
9596
</template>;
9697

97-
const BeforeOptions: TemplateOnlyComponent<{ Args: {} }> = <template>
98+
const BeforeOptions: TemplateOnlyComponent = <template>
9899
<div class='before-options'>
99100
<span class='title'>
100101
Recent
@@ -378,6 +379,7 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
378379
@service private declare realm: RealmService;
379380
@service private declare realmServer: RealmServerService;
380381
@service declare recentFilesService: RecentFilesService;
382+
@tracked newCardJSON: LooseSingleCardDocument | undefined;
381383
private playgroundSelections: Record<
382384
string, // moduleId
383385
{ cardId: string; format: Format }
@@ -430,7 +432,8 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
430432

431433
private cardResource = getCard(
432434
this,
433-
() => this.playgroundSelections[this.args.moduleId]?.cardId,
435+
() =>
436+
this.newCardJSON ?? this.playgroundSelections[this.args.moduleId]?.cardId,
434437
{ isAutoSave: () => true },
435438
);
436439

@@ -477,6 +480,7 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
477480
}
478481

479482
private persistSelections = (cardId: string, format = this.format) => {
483+
this.newCardJSON = undefined;
480484
this.playgroundSelections[this.args.moduleId] = { cardId, format };
481485
window.localStorage.setItem(
482486
PlaygroundSelections,
@@ -507,29 +511,20 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
507511
}
508512
});
509513

514+
// TODO: convert this to @action once we no longer need to await below
510515
private createNew = task(async () => {
511-
let ref = this.args.codeRef;
512-
let realmURL = this.operatorModeStateService.realmURL.href; // creates in current workspace
513-
let doc: LooseSingleCardDocument = {
516+
this.newCardJSON = {
514517
data: {
515518
meta: {
516-
adoptsFrom: ref,
517-
realmURL,
519+
adoptsFrom: this.args.codeRef,
520+
realmURL: this.operatorModeStateService.realmURL.href,
518521
},
519522
},
520523
};
521-
try {
522-
let card = await this.cardService.createFromSerialized(doc.data, doc);
523-
if (!card) {
524-
throw new Error(
525-
`Failed to create card from ref "${ref.name}" from "${ref.module}"`,
526-
);
527-
}
528-
await this.cardService.saveModel(card);
529-
this.recentFilesService.addRecentFileUrl(`${card.id}.json`);
530-
this.persistSelections(card.id, 'edit'); // open new instance in playground in edit format
531-
} catch (e: any) {
532-
console.log('Error saving', e);
524+
await this.cardResource.loaded; // TODO: remove await when card-resource is refactored
525+
if (this.card) {
526+
this.recentFilesService.addRecentFileUrl(`${this.card.id}.json`);
527+
this.persistSelections(this.card.id, 'edit'); // open new instance in playground in edit format
533528
}
534529
});
535530

0 commit comments

Comments
 (0)