Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update collections #784

Merged
merged 6 commits into from Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions client-v2/src/actions/Collections.ts
@@ -1,7 +1,6 @@
import { batchActions } from 'redux-batched-actions';
import {
getArticlesBatched,
updateCollection as updateCollectionFromApi,
discardDraftChangesToCollection as discardDraftChangesToCollectionApi,
fetchVisibleArticles,
fetchLastPressed as fetchLastPressedApi,
Expand Down Expand Up @@ -71,6 +70,7 @@ import { State } from 'types/State';
import { events } from 'services/GA';
import { collectionParamsSelector } from 'selectors/collectionSelectors';
import { fetchCollectionsStrategy } from 'strategies/fetch-collection';
import { updateCollectionStrategy } from 'strategies/update-collection';

const articlesInCollection = createAllArticlesInCollectionSelector();
const collectionsInOpenFrontsSelector = createCollectionsInOpenFrontsSelector();
Expand Down Expand Up @@ -262,7 +262,11 @@ function updateCollection(collection: Collection): ThunkResult<Promise<void>> {
getState(),
collection.id
);
await updateCollectionFromApi(collection.id, denormalisedCollection);
await updateCollectionStrategy(
getState(),
collection.id,
denormalisedCollection
);
dispatch(collectionActions.updateSuccess(collection.id));
const visibleArticles = await getVisibleArticles(
collection,
Expand Down
1 change: 1 addition & 0 deletions client-v2/src/actions/__tests__/Persistence.spec.ts
Expand Up @@ -39,6 +39,7 @@ const A4 = {

const init = () => {
const initState = {
path: '/v2/editorial',
shared: {
collections: {
loadingIds: [],
Expand Down
4 changes: 2 additions & 2 deletions client-v2/src/components/Editions/Issue.tsx
Expand Up @@ -32,11 +32,11 @@ const Issue = (props: IssueProps) => (
</tr>
<tr>
<td>Published:</td>
<td>{props.issue.lastPublished ? 'Yes' : 'No'}</td>
<td>{props.issue.launchedOn ? 'Yes' : 'No'}</td>
</tr>
<tr>
<td>Last published:</td>
<td>{props.issue.lastPublished}</td>
<td>{props.issue.launchedOn}</td>
</tr>
<tr>
<td>Creator:</td>
Expand Down
4 changes: 2 additions & 2 deletions client-v2/src/services/__tests__/faciaApi.spec.ts
Expand Up @@ -63,15 +63,15 @@ describe('faciaApi', () => {
})
});
expect.assertions(1);
return expect(updateCollection('exampleId', collection)).resolves.toEqual(
return expect(updateCollection('exampleId')(collection)).resolves.toEqual(
collection
);
});
it('should reject if the server gives a !2XX response', async () => {
fetchMock.once('/v2Edits', { status: 400 });
expect.assertions(1);
try {
await updateCollection('exampleId', collection);
await updateCollection('exampleId')(collection);
} catch (e) {
expect(e.message).toContain('exampleId');
}
Expand Down
22 changes: 15 additions & 7 deletions client-v2/src/services/faciaApi.ts
Expand Up @@ -20,7 +20,7 @@ import { CapiArticle } from 'types/Capi';
import chunk from 'lodash/chunk';
import { CAPISearchQueryResponse, checkIsResults } from './capiQuery';
import flatMap from 'lodash/flatMap';
import { EditionsIssue } from 'types/Edition';
import { EditionsIssue, EditionsCollection } from 'types/Edition';

function fetchEditionsIssueAsConfig(editionId: string): Promise<FrontsConfig> {
return pandaFetch(`/editions-api/${editionId}`, {
Expand Down Expand Up @@ -176,12 +176,11 @@ async function publishCollection(collectionId: string): Promise<void> {
}
}

async function updateCollection(
id: string,
collection: CollectionWithNestedArticles
): Promise<CollectionWithNestedArticles> {
const createUpdateCollection = <T>(path: string) => (id: string) => async (
collection: T
): Promise<void> => {
try {
const response = await pandaFetch(`/v2Edits`, {
const response = await pandaFetch(path, {
method: 'post',
headers: {
'Content-Type': 'application/json'
Expand All @@ -197,7 +196,15 @@ async function updateCollection(
}: ${response.body}`
);
}
}
};

const updateCollection = createUpdateCollection<CollectionWithNestedArticles>(
'/v2Edits'
);
const updateEditionsCollection = (collectionId: string) =>
createUpdateCollection<EditionsCollection>(
`/editions-api/collections/${collectionId}`
)(collectionId);

async function saveClipboard(
clipboardContent: NestedArticleFragment[]
Expand Down Expand Up @@ -421,6 +428,7 @@ export {
fetchLastPressed,
publishCollection,
updateCollection,
updateEditionsCollection,
saveClipboard,
saveOpenFrontIds,
saveFavouriteFrontIds,
Expand Down
3 changes: 3 additions & 0 deletions client-v2/src/shared/fixtures/shared.ts
Expand Up @@ -547,6 +547,7 @@ const collectionWithSupportingArticles = {
};

const stateWithCollection: any = {
path: '/v2/editorial',
fronts: {
frontsConfig: {
data: {
Expand Down Expand Up @@ -651,6 +652,7 @@ const stateWithCollection: any = {
};

const stateWithCollectionAndSupporting: any = {
path: '/v2/editorial',
shared: {
collections: {
data: {
Expand Down Expand Up @@ -712,6 +714,7 @@ const stateWithCollectionAndSupporting: any = {
};

const stateWithSnaplinksAndArticles: any = {
path: '/v2/editorial',
shared: {
articleFragments: {
'1269c42e-a341-4464-b206-a5731b92fa46': {
Expand Down
1 change: 1 addition & 0 deletions client-v2/src/shared/types/Collection.ts
Expand Up @@ -96,6 +96,7 @@ interface CollectionFromResponse {
live: NestedArticleFragment[];
previously?: NestedArticleFragment[];
draft?: NestedArticleFragment[];
isHidden?: boolean;
lastUpdated?: number;
updatedBy?: string;
updatedEmail?: string;
Expand Down
4 changes: 2 additions & 2 deletions client-v2/src/strategies/fetch-collection.ts
Expand Up @@ -15,9 +15,9 @@ const editionCollectionToCollection = (
return {
...restRes,
collection: {
...restCol,
draft: items,
live: [],
...restCol
live: []
},
storiesVisibleByStage: {
// TODO - remove me once we figure out what to do here!
Expand Down
31 changes: 31 additions & 0 deletions client-v2/src/strategies/update-collection.ts
@@ -0,0 +1,31 @@
import { State } from 'types/State';
import { updateCollection } from 'services/faciaApi';
import { updateEditionsCollection } from 'services/faciaApi';
import { runStrategy } from './run-strategy';
import { CollectionWithNestedArticles } from 'shared/types/Collection';
import { EditionsCollection } from 'types/Edition';

const collectionToEditionCollection = (
col: CollectionWithNestedArticles
): EditionsCollection => {
const { live, draft, isHidden, ...restCol } = col;
return {
...restCol,
isHidden: isHidden || false,
items: draft || []
};
};

const updateCollectionStrategy = (
state: State,
id: string,
collection: CollectionWithNestedArticles
) =>
runStrategy<void>(state, {
front: () => updateCollection(id)(collection),
edition: () =>
updateEditionsCollection(id)(collectionToEditionCollection(collection)),
none: () => null
});

export { updateCollectionStrategy };
12 changes: 6 additions & 6 deletions client-v2/src/types/Edition.ts
Expand Up @@ -5,11 +5,10 @@ interface EditionsCollection {
displayName: string;
prefill?: string;
isHidden: boolean;
lastUpdated?: string;
lastUpdated?: number;
updatedBy?: string;
updatedEmail?: string;
live: EditionsArticle[];
draft: EditionsArticle[];
items: EditionsArticle[];
}

interface EditionsFront {
Expand All @@ -25,9 +24,10 @@ interface EditionsFront {
interface EditionsIssue {
id: string;
displayName: string;
issueDate: string; // the date for which edition is made for, in format TimestampZ, eg 2016-06-22 19:10:25-07
lastPublished: string; // null if not published
launchedOn: string;
issueDate: number; // midnight on the expect publish date
createdOn: number;
createdBy: number;
launchedOn?: number;
launchedBy: string;
launchedEmail: string;
fronts: EditionsFront[];
Expand Down