Skip to content

Commit

Permalink
wip: call clone endpoint from frontend
Browse files Browse the repository at this point in the history
Co-authored-by: Fael Bassetti <fael.bassetti@pix.fr>
Co-authored-by: Jérémie Jadé <jeremie.jade@pix.fr>
  • Loading branch information
3 people committed Aug 30, 2024
1 parent 24ac3a9 commit 88c57ec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
5 changes: 5 additions & 0 deletions pix-editor/app/adapters/skill.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import AirtableAdapter from './airtable';

export default class SkillAdapter extends AirtableAdapter {

urlForCreateRecord(model, snapshot) {
if (snapshot.adapterOptions?.clone) return '/api/skills/clone';
return super.urlForCreateRecord(model, snapshot);
}

pathForType() {
return 'Acquis';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,13 @@ export default class SingleController extends Controller {
(changelogValue) => this._duplicateToLocationCallback(changelogValue, competence, newTube, level));
}

async _duplicateToLocationCallback(changelogValue, competence, newTube, level) {
async _duplicateToLocationCallback(changelogValue, competence, tubeDestination, level) {
this.loader.start();

try {
const currentSkill = this.skill;
const newSkill = await currentSkill.clone();
const newSkill = await currentSkill.clone({ tubeDestination, level });

newSkill.tube = newTube;
newSkill.level = level;
newSkill.version = newTube.getNextSkillVersion(level);
await newSkill.save();
await this._duplicateLiveChallenges(newSkill);
await this._handleSkillChangelog(newSkill, changelogValue, this.changelogEntry.moveAction);

this.notify.message('Acquis et épreuves associées dupliqués');
Expand Down
47 changes: 18 additions & 29 deletions pix-editor/app/models/skill.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default class SkillModel extends Model {
tutoMore;

@service('store') myStore;
@service idGenerator;

@tracked _selected = false;

Expand Down Expand Up @@ -169,15 +168,12 @@ export default class SkillModel extends Model {
return this.save();
}

pinRelationships() {
const requests = [this.tutoSolution, this.tutoMore];
return Promise.all(requests)
.then((tutorials) => {
this._pinnedRelationships = {
tutoSolution: tutorials[0].toArray(),
tutoMore: tutorials[1].toArray(),
};
});
async pinRelationships() {
const tutorials = await Promise.all([this.tutoSolution, this.tutoMore]);
this._pinnedRelationships = {
tutoSolution: tutorials[0].toArray(),
tutoMore: tutorials[1].toArray(),
};
}

rollbackAttributes() {
Expand All @@ -188,27 +184,20 @@ export default class SkillModel extends Model {
this.tutoMore = tutoMore;
}

save() {
return super.save(...arguments)
.then((result) => {
this.pinRelationships();
return result;
});
async save(...args) {
const result = await super.save(...args);
await this.pinRelationships();
return result;
}

clone() {
const ignoredFields = ['pixId', 'competence', 'level', 'tube', 'createdAt', 'challenges', 'tutoSolution', 'tutoMore', 'version'];
const data = this._getJSON(ignoredFields);
data.status = 'en construction';
data.pixId = this.idGenerator.newId('skill');
const newSkill = this.myStore.createRecord(this.constructor.modelName, data);
const requests = [this.tutoSolution, this.tutoMore];
return Promise.all(requests)
.then((tutorials) => {
newSkill.tutoSolution = tutorials[0];
newSkill.tutoMore = tutorials[1];
return newSkill;
});
async clone({ tubeDestination, level }) {
const newSkill = this.myStore.createRecord(this.constructor.modelName, {});
return newSkill.save({ adapterOptions: {
clone: true,
skillIdToClone: this.pixId,
tubeDestinationId: tubeDestination.pixId,
level,
} });
}

_getJSON(fieldsToRemove) {
Expand Down
21 changes: 21 additions & 0 deletions pix-editor/app/serializers/skill.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,25 @@ export default class SkillSerializer extends AirtableSerializer {
payloadKeyFromModelName() {
return 'Acquis';
}

serialize(snapshot, options) {
if (snapshot.adapterOptions?.clone) {
return {
skillIdToClone: snapshot.adapterOptions.skillIdToClone,
tubeDestinationId: snapshot.adapterOptions.tubeDestinationId,
level: snapshot.adapterOptions.level,
};
}
return super.serialize(snapshot, options);
}

serializeIntoHash(hash, typeClass, snapshot, options) {
if (snapshot.adapterOptions?.clone) {
hash.data = {
attributes: this.serialize(snapshot, options),
};
return;
}
super.serializeIntoHash(hash, typeClass, snapshot, options);
}
}

0 comments on commit 88c57ec

Please sign in to comment.