Skip to content

Commit

Permalink
fix: handling of relational data (PollOptions) in the form and saving…
Browse files Browse the repository at this point in the history
… process
  • Loading branch information
novacuum committed Feb 28, 2024
1 parent bca66b7 commit f5c8b5f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
16 changes: 7 additions & 9 deletions js/src/forum/addComposerItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import CreatePollModal from './components/CreatePollModal';
function toPoll(data) {
if (data) {
const poll = app.store.createRecord('polls');
poll.pushAttributes(data);
poll.pushData({
relationships: {
options: data.options.map((option) => {
const pollOption = app.store.createRecord('poll_options');
pollOption.pushAttributes(option);
return pollOption;
}),
},

poll.tempOptions = data.options.map((option) => {
const pollOption = app.store.createRecord('poll_options');
pollOption.pushAttributes(option);
return pollOption;
});
poll.pushAttributes(data);

return poll;
}
return data;
Expand Down
2 changes: 1 addition & 1 deletion js/src/forum/components/PollForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class PollForm extends Component<PollFormAttrs, PollFormState> {
// state handles poll initialization
const poll = this.state.poll;

this.options = poll.options() as PollOption[];
this.options = (poll.tempOptions ?? poll.options()) as PollOption[];
this.optionAnswers = this.options.map((o) => Stream(o.answer()));
this.optionImageUrls = this.options.map((o) => Stream(o.imageUrl()));

Expand Down
2 changes: 2 additions & 0 deletions js/src/forum/models/Poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import PollVote from './PollVote';
import computed from 'flarum/common/utils/computed';

export default class Poll extends Model {
public tempOptions: PollOption[] | undefined;

question() {
return Model.attribute<string>('question').call(this);
}
Expand Down
12 changes: 11 additions & 1 deletion js/src/forum/states/PollFormState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import app from 'flarum/forum/app';
import Poll from '../models/Poll';
import PollOption from '../models/PollOption';

export default class PollFormState {
poll: Poll;
Expand All @@ -9,6 +10,7 @@ export default class PollFormState {

static createNewPoll() {
const poll = app.store.createRecord<Poll>('polls');

poll.pushAttributes({
question: '',
endDate: '',
Expand All @@ -18,7 +20,9 @@ export default class PollFormState {
allowChangeVote: false,
maxVotes: 0,
});
poll.pushData({ relationships: { options: [] } });

poll.tempOptions = [app.store.createRecord<PollOption>('poll_options'), app.store.createRecord<PollOption>('poll_options')];

return poll;
}

Expand Down Expand Up @@ -48,6 +52,12 @@ export default class PollFormState {

try {
this.poll = await this.poll.save(data);
/**
* Cleanup attributes:
* For the saving process, we add the options directly to the attributes.
* As we currently cannot add new PollOptions as relationships.
*/
delete this.poll!.data!.attributes!.options;
} finally {
this.loading = false;
m.redraw();
Expand Down

0 comments on commit f5c8b5f

Please sign in to comment.