Skip to content

Commit

Permalink
chor: move both Modal to tsx format
Browse files Browse the repository at this point in the history
  • Loading branch information
novacuum committed Feb 27, 2024
1 parent e2bbb21 commit 29ea10a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
20 changes: 19 additions & 1 deletion js/src/forum/addComposerItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@ import ReplyComposer from 'flarum/forum/components/ReplyComposer';

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;
}),
},
});
return poll;
}
return data;
}

export const addToComposer = (composer) => {
composer.prototype.addPoll = function () {
app.modal.show(CreatePollModal, {
poll: this.composer.fields.poll,
poll: toPoll(this.composer.fields.poll),
onsubmit: (poll) => (this.composer.fields.poll = poll),
});
};
Expand Down
26 changes: 0 additions & 26 deletions js/src/forum/components/CreatePollModal.js

This file was deleted.

34 changes: 34 additions & 0 deletions js/src/forum/components/CreatePollModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type Mithril from 'mithril';
import app from 'flarum/forum/app';
import Modal, { IInternalModalAttrs } from 'flarum/common/components/Modal';
import PollForm from './PollForm';
import PollModel from '../models/Poll';
import PollFormState from '../states/PollFormState';

interface CreatePollModalAttrs extends IInternalModalAttrs {
poll: PollModel;
onsubmit: (data: object) => Promise<void>;
}

export default class CreatePollModal extends Modal<CreatePollModalAttrs> {
title(): Mithril.Children {
return app.translator.trans('fof-polls.forum.modal.add_title');
}

className(): string {
return 'PollDiscussionModal Modal--medium';
}

content(): Mithril.Children {
return [
<div className="Modal-body">
<PollForm poll={this.attrs.poll} onsubmit={this.onFormSubmit.bind(this)}></PollForm>
</div>,
];
}

async onFormSubmit(data: object, state: PollFormState): Promise<void> {
this.hide();
await this.attrs.onsubmit(data);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type Mithril from 'mithril';
import app from 'flarum/forum/app';
import CreatePollModal from './CreatePollModal';
import PollFormState from '../states/PollFormState';

export default class EditPollModal extends CreatePollModal {
title() {
title(): Mithril.Children {
return app.translator.trans('fof-polls.forum.modal.edit_title');
}

async onsubmit(data, state) {
async onFormSubmit(data: object, state: PollFormState): Promise<void> {
await state.save(data);

// Show success alert
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 @@ -274,7 +274,7 @@ export default class PollForm extends Component<PollFormAttrs, PollFormState> {
}
}

removeOption(i:number):void {
removeOption(i: number): void {
this.options.splice(i, 1);
this.optionAnswers.splice(i, 1);
this.optionImageUrls.splice(i, 1);
Expand Down

0 comments on commit 29ea10a

Please sign in to comment.