Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

Commit 161cf50

Browse files
authored
Merge pull request #435 from 30-seconds/collection-parents
Resolves #377
2 parents 3607ef3 + 2c32b8f commit 161cf50

File tree

5 files changed

+56
-36
lines changed

5 files changed

+56
-36
lines changed

src/blocks/application.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -420,30 +420,20 @@ export class Application {
420420
lastUpdated: new Date(lastUpdated),
421421
});
422422
});
423-
// Populate collections and link to snippets
424-
collections.forEach(collection => {
425-
const { snippetIds, typeMatcher, ...rest } = collection;
426-
const collectionRec = Collection.createRecord(rest);
427-
if (snippetIds && snippetIds.length) collectionRec.snippets = snippetIds;
428-
if (typeMatcher)
429-
collectionRec.snippets = Snippet.records
430-
.where(snippet => snippet.type === typeMatcher)
431-
.flatPluck('id');
432-
});
433423
// Populate listings and create relationships
434424
Repository.records.forEach(repo => {
435425
const type = repo.isBlog ? 'blog' : 'language';
436426
const slugPrefix = `/${repo.slug}`;
437427
const repoListingId = `${type}${slugPrefix}`;
438-
const repoListing = Listing.createRecord({
428+
Listing.createRecord({
439429
id: repoListingId,
440430
relatedRecordId: repo.id,
441431
type,
442432
slugPrefix,
443433
featuredIndex: featuredListings.indexOf(repoListingId),
444434
});
445435
// Populate tag listings from repositories
446-
const tagListingIds = repo.tags.flatMap(tag => {
436+
repo.tags.forEach(tag => {
447437
const tagSlugPrefix = tag.slugPrefix;
448438
const tagId = `tag${tagSlugPrefix}`;
449439
Listing.createRecord({
@@ -452,10 +442,29 @@ export class Application {
452442
type: 'tag',
453443
slugPrefix: tagSlugPrefix,
454444
featuredIndex: featuredListings.indexOf(tagId),
445+
parent: repoListingId,
455446
});
456-
return tagId;
457447
});
458-
repoListing.children = tagListingIds;
448+
});
449+
// Populate collections, collection listings and link to snippets and parent listings
450+
collections.forEach(collection => {
451+
const { snippetIds, typeMatcher, parent, ...rest } = collection;
452+
const collectionRec = Collection.createRecord(rest);
453+
if (snippetIds && snippetIds.length) collectionRec.snippets = snippetIds;
454+
if (typeMatcher)
455+
collectionRec.snippets = Snippet.records
456+
.where(snippet => snippet.type === typeMatcher)
457+
.flatPluck('id');
458+
const slugPrefix = `/${collection.slug}`;
459+
const listingId = `collection${slugPrefix}`;
460+
Listing.createRecord({
461+
id: listingId,
462+
relatedRecordId: collection.id,
463+
type: 'collection',
464+
slugPrefix,
465+
featuredIndex: featuredListings.indexOf(listingId),
466+
parent,
467+
});
459468
});
460469
// Populate the main listing
461470
Listing.createRecord({
@@ -473,18 +482,6 @@ export class Application {
473482
featuredIndex: -1,
474483
...collectionListingConfig,
475484
});
476-
// Populate listings for custom collections
477-
Collection.records.forEach(collection => {
478-
const slugPrefix = `/${collection.slug}`;
479-
const listingId = `collection${slugPrefix}`;
480-
Listing.createRecord({
481-
id: listingId,
482-
relatedRecordId: collection.id,
483-
type: 'collection',
484-
slugPrefix,
485-
featuredIndex: featuredListings.indexOf(listingId),
486-
});
487-
});
488485
// Populate snipet pages
489486
Snippet.records.forEach(snippet => {
490487
const { id } = snippet;

src/blocks/models/collection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const collection = {
22
name: 'Collection',
33
fields: [
44
{ name: 'name', type: 'stringRequired' },
5+
{ name: 'shortName', type: 'stringRequired' },
56
{ name: 'slug', type: 'stringRequired' },
67
{ name: 'featured', type: 'booleanRequired' },
78
{ name: 'icon', type: 'stringRequired' },

src/blocks/models/listing.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export const listing = {
187187
return [];
188188
},
189189
sublinks: ({ models: { Listing } }) => listing => {
190-
if (listing.isCollection) return [];
190+
if (listing.isCollection && !listing.parent) return [];
191191
if (listing.isCollections) return [];
192192
if (listing.isMain) {
193193
return [
@@ -212,11 +212,22 @@ export const listing = {
212212
url: `${listing.rootUrl}/p/1`,
213213
selected: listing.isParent,
214214
},
215-
...links.flatMap(link => ({
216-
name: literals.tag(link.data.shortId),
217-
url: `${link.data.slugPrefix}/p/1`,
218-
selected: listing.isTag && listing.data.shortId === link.data.shortId,
219-
})),
215+
...links
216+
.flatMap(link =>
217+
link.isCollection
218+
? {
219+
name: link.data.shortName,
220+
url: `/${link.data.slug}/p/1`,
221+
selected: listing.isCollection && link.id === listing.id,
222+
}
223+
: {
224+
name: literals.tag(link.data.shortId),
225+
url: `${link.data.slugPrefix}/p/1`,
226+
selected:
227+
listing.isTag && listing.data.shortId === link.data.shortId,
228+
}
229+
)
230+
.sort((a, b) => a.name.localeCompare(b.name)),
220231
];
221232
},
222233
},

src/blocks/models/snippet.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ export const snippet = {
152152
}/t/${snippet.primaryTag.toLowerCase()}/p/1`,
153153
name: literals.tag(snippet.primaryTag),
154154
};
155+
} else if (
156+
snippet.hasCollection &&
157+
snippet.collections.first.listing.parent
158+
) {
159+
// TODO: Make this smarter to account for multiple collections
160+
tagCrumb = {
161+
url: `/${snippet.collections.first.slug}/p/1`,
162+
name: snippet.collections.first.shortName,
163+
};
155164
} else if (
156165
snippet.language &&
157166
snippet.truePrimaryTag &&
@@ -220,7 +229,9 @@ export const snippet = {
220229
hasCollection: snippet =>
221230
Boolean(snippet.collections && snippet.collections.length),
222231
recommendedCollection: snippet =>
223-
snippet.hasCollection ? snippet.collections.first : null,
232+
snippet.hasCollection && !snippet.collections.first.listing.parent
233+
? snippet.collections.first
234+
: null,
224235
},
225236
lazyProperties: {
226237
icon: ({ models: { Language } }) => snippet => {

src/blocks/schema.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export const schema = {
3232
type: 'manyToOne',
3333
},
3434
{
35-
from: { model: 'Listing', name: 'children' },
36-
to: { model: 'Listing', name: 'parent' },
37-
type: 'oneToMany',
35+
from: { model: 'Listing', name: 'parent' },
36+
to: { model: 'Listing', name: 'children' },
37+
type: 'manyToOne',
3838
},
3939
{
4040
from: { model: 'Page', name: 'snippets' },

0 commit comments

Comments
 (0)