mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 22:02:11 +03:00
e5c26aac89
closes https://github.com/TryGhost/Team/issues/1479 - updated post adapter to append `?newsletter_id=xyz` when passed a `newsletterId` adapterOption - updated editor save task to pass `options.newsletterId` through as `adapterOptions.newsletterId` - set up `post.newsletter` relationship ready for handling embedded newsletter association from the API - explicitly deleted when serializing back to the API as it doesn't yet ignore the attribute - updated `<GhPublishmenu>` for newsletter support - fetches newsletters on first render so they are available in the dropdown - sets "default" (first in the ordered list) newsletter as the initially selected newsletter - adds newsletter dropdown to draft publish menu - passes `newsletterId` option to editor save task when it's set This is a minimal implementation for testing. Not included: - correct free/paid member counts based on selected newsletter - correct member count in confirmation modal - indication of selected newsletter for scheduled post
94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
import moment from 'moment';
|
|
import {Response} from 'miragejs';
|
|
import {dasherize} from '@ember/string';
|
|
import {extractFilterParam, paginateModelCollection} from '../utils';
|
|
import {isBlank, isEmpty} from '@ember/utils';
|
|
|
|
// NOTE: mirage requires Model objects when saving relationships, however the
|
|
// `attrs` on POST/PUT requests will contain POJOs for authors and tags so we
|
|
// need to replace them
|
|
function extractAuthors(postAttrs, users) {
|
|
return postAttrs.authors.map(author => users.find(author.id));
|
|
}
|
|
|
|
function extractTags(postAttrs, tags) {
|
|
return postAttrs.tags.map((requestTag) => {
|
|
let tag = tags.find(requestTag.id);
|
|
|
|
if (!tag) {
|
|
tag = tag.create(requestTag);
|
|
}
|
|
|
|
return tag;
|
|
});
|
|
}
|
|
|
|
export default function mockPosts(server) {
|
|
server.post('/posts', function ({posts, users, tags}) {
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
attrs.authors = extractAuthors(attrs, users);
|
|
attrs.tags = extractTags(attrs, tags);
|
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
|
|
attrs.slug = dasherize(attrs.title);
|
|
}
|
|
|
|
return posts.create(attrs);
|
|
});
|
|
|
|
// TODO: handle authors filter
|
|
server.get('/posts/', function ({posts}, {queryParams}) {
|
|
let {filter, page, limit} = queryParams;
|
|
|
|
page = +page || 1;
|
|
limit = +limit || 15;
|
|
|
|
let statusFilter = extractFilterParam('status', filter);
|
|
|
|
let collection = posts.all().filter((post) => {
|
|
let matchesStatus = true;
|
|
|
|
if (!isEmpty(statusFilter)) {
|
|
matchesStatus = statusFilter.includes(post.status);
|
|
}
|
|
|
|
return matchesStatus;
|
|
});
|
|
|
|
return paginateModelCollection('posts', collection, page, limit);
|
|
});
|
|
|
|
server.get('/posts/:id/', function ({posts}, {params}) {
|
|
let {id} = params;
|
|
let post = posts.find(id);
|
|
|
|
return post || new Response(404, {}, {
|
|
errors: [{
|
|
type: 'NotFoundError',
|
|
message: 'Post not found.'
|
|
}]
|
|
});
|
|
});
|
|
|
|
server.put('/posts/:id/', function ({newsletters, posts, users, tags}, {params}) {
|
|
const attrs = this.normalizedRequestAttrs();
|
|
const post = posts.find(params.id);
|
|
|
|
attrs.authors = extractAuthors(attrs, users);
|
|
attrs.tags = extractTags(attrs, tags);
|
|
|
|
attrs.updatedAt = moment.utc().toDate();
|
|
|
|
if (params.newsletter_id) {
|
|
const newsletter = newsletters.find(params.newsletter_id);
|
|
post.newsletter = newsletter;
|
|
post.save();
|
|
}
|
|
|
|
return post.update(attrs);
|
|
});
|
|
|
|
server.del('/posts/:id/');
|
|
}
|