« get me outta code hell

listGroupsByLatestAlbum.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/listGroupsByLatestAlbum.js
blob: 48319314384e4b8ea16afe14ebf536aa4ad3327b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {compareDates, sortChronologically} from '#sort';
import {filterMultipleArrays, sortMultipleArrays, stitchArrays} from '#sugar';

export default {
  contentDependencies: [
    'generateListingPage',
    'linkAlbum',
    'linkGroup',
    'linkGroupGallery',
  ],

  extraDependencies: ['language', 'wikiData'],

  sprawl({groupData}) {
    return {groupData};
  },

  query({groupData}, spec) {
    const groups = sortChronologically(groupData.slice());

    const albums =
      groups
        .map(group =>
          sortChronologically(
            group.albums.filter(album => album.date),
            {latestFirst: true}))
        .map(albums => albums[0]);

    filterMultipleArrays(groups, albums, (group, album) => album);

    const dates = albums.map(album => album.date);

    // Note: After this sort, the groups/dates arrays are misaligned with
    // albums. That's OK only because we aren't doing anything further with
    // the albums array.
    sortMultipleArrays(groups, dates,
      (groupA, groupB, dateA, dateB) =>
        compareDates(dateA, dateB, {latestFirst: true}));

    return {spec, groups, dates};
  },

  relations(relation, query) {
    return {
      page: relation('generateListingPage', query.spec),

      groupLinks:
        query.groups
          .map(group => relation('linkGroup', group)),
    };
  },

  data(query) {
    return {
      dates: query.dates,
    };
  },

  generate(data, relations, {language}) {
    return relations.page.slots({
      type: 'rows',
      rows:
        stitchArrays({
          groupLink: relations.groupLinks,
          date: data.dates,
        }).map(({groupLink, date}) => ({
            group: groupLink,
            date: language.formatDate(date),
          })),
    });
  },
};