FIX: Do not reload post if raw is present (#13335)

Editing a post that was just posted caused it to be reloaded and made a
request to the server. This had an additional side effect where the
model instances used by post stream and composer would be different and
changes did not propagate correctly.
This commit is contained in:
Bianca Nenciu
2021-06-11 04:00:41 +03:00
committed by GitHub
parent fa02775095
commit ef906fa1da

View File

@@ -722,12 +722,14 @@ const Composer = RestModel.extend({
if (!opts) { if (!opts) {
opts = {}; opts = {};
} }
this.set("loading", true); this.set("loading", true);
const replyBlank = isEmpty(this.reply); if (
!isEmpty(this.reply) &&
const composer = this; (opts.reply || isEdit(opts.action)) &&
if (!replyBlank && (opts.reply || isEdit(opts.action)) && this.replyDirty) { this.replyDirty
) {
return promise; return promise;
} }
@@ -770,6 +772,15 @@ const Composer = RestModel.extend({
if (!this.topic) { if (!this.topic) {
this.set("topic", opts.post.topic); this.set("topic", opts.post.topic);
} }
} else if (opts.postId) {
promise = promise.then(() =>
this.store.find("post", opts.postId).then((post) => {
this.set("post", post);
if (post) {
this.set("topic", post.topic);
}
})
);
} else { } else {
this.set("post", null); this.set("post", null);
} }
@@ -794,19 +805,8 @@ const Composer = RestModel.extend({
(c) => c.topic_template (c) => c.topic_template
); );
if (opts.postId) {
promise = promise.then(() =>
this.store.find("post", opts.postId).then((post) => {
composer.set("post", post);
if (post) {
composer.set("topic", post.topic);
}
})
);
}
// If we are editing a post, load it. // If we are editing a post, load it.
if (isEdit(opts.action) && opts.post) { if (isEdit(opts.action) && this.post) {
const topicProps = this.serialize(_edit_topic_serializer); const topicProps = this.serialize(_edit_topic_serializer);
topicProps.loading = true; topicProps.loading = true;
@@ -816,30 +816,40 @@ const Composer = RestModel.extend({
} }
this.setProperties(topicProps); this.setProperties(topicProps);
promise = promise.then(() => promise = promise.then(() => {
this.store.find("post", opts.post.id).then((post) => { let rawPromise = Promise.resolve();
composer.setProperties({
if (!this.post.raw) {
rawPromise = this.store.find("post", opts.post.id).then((post) => {
this.setProperties({
post,
reply: post.raw, reply: post.raw,
originalText: post.raw, originalText: post.raw,
post: post,
}); });
});
} else {
this.setProperties({
reply: this.post.raw,
originalText: this.post.raw,
});
}
promise = Promise.resolve();
// edge case ... make a post then edit right away // edge case ... make a post then edit right away
// store does not have topic for the post // store does not have topic for the post
if (composer.topic && composer.topic.id === post.topic_id) { if (this.topic && this.topic.id === this.post.topic_id) {
// nothing to do ... we have the right topic // nothing to do ... we have the right topic
} else { } else {
promise = this.store.find("topic", post.topic_id).then((topic) => { rawPromise = this.store
.find("topic", this.post.topic_id)
.then((topic) => {
this.set("topic", topic); this.set("topic", topic);
}); });
} }
return promise.then(() => { return rawPromise.then(() => {
composer.appEvents.trigger("composer:reply-reloaded", composer); this.appEvents.trigger("composer:reply-reloaded", this);
});
}); });
})
);
} else if (opts.action === REPLY && opts.quote) { } else if (opts.action === REPLY && opts.quote) {
this.setProperties({ this.setProperties({
reply: opts.quote, reply: opts.quote,
@@ -862,7 +872,7 @@ const Composer = RestModel.extend({
if (!isEdit(opts.action) || !opts.post) { if (!isEdit(opts.action) || !opts.post) {
promise = promise.then(() => promise = promise.then(() =>
composer.appEvents.trigger("composer:reply-reloaded", composer) this.appEvents.trigger("composer:reply-reloaded", this)
); );
} }