NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arrangements.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { sortTypes } from '../../utils/helpers'
  2. const _insertInBetweens = bySort => {
  3. const postListWithInBetweens = []
  4. Object.keys(bySort).forEach(term => {
  5. if(term) {
  6. postListWithInBetweens.push({
  7. slug: term,
  8. title: term,
  9. inbetween: true
  10. })
  11. }
  12. bySort[term].forEach(post => postListWithInBetweens.push(post))
  13. })
  14. return postListWithInBetweens
  15. }
  16. const _arrangeByMaterial = postsList => {
  17. const byMaterial = {
  18. clay: []
  19. }
  20. postsList.forEach(post => {
  21. post.materials.forEach(mat => {
  22. if(!byMaterial[mat]) byMaterial[mat] = []
  23. byMaterial[mat].push(post)
  24. })
  25. })
  26. const flatPacked = _insertInBetweens(byMaterial)
  27. return flatPacked
  28. }
  29. const _arrangeByType = postsList => {
  30. const byType = {}
  31. postsList.forEach(post => {
  32. const subtypes = post.subtypes
  33. subtypes.forEach(type => {
  34. if(!byType[type]) byType[type] = []
  35. byType[type].push(post)
  36. })
  37. })
  38. const flatPacked = _insertInBetweens(byType)
  39. return flatPacked
  40. }
  41. const _arrangeByAlpha = (postsList, seenTitles) => {
  42. const alphabet = [...'9abcdefghijklmnopqrstuvwxyz']
  43. const flatPacked = []
  44. const storeTitle = letter => {
  45. if(seenTitles.includes(letter)) return
  46. flatPacked.push({ slug: letter, title: letter, inbetween: true })
  47. seenTitles.push(letter)
  48. }
  49. postsList.forEach(post => {
  50. const lastWord = post.slug.split('-').filter(c => c).pop()
  51. const firstCharaOflastWord = lastWord[0]
  52. const firstCharaOfSortWord = post.sortname ? post.sortname[0] : 'z'
  53. const charaIndex = alphabet.indexOf(firstCharaOflastWord) < alphabet.indexOf(firstCharaOfSortWord) ? alphabet.indexOf(firstCharaOflastWord) : alphabet.indexOf(firstCharaOfSortWord)
  54. storeTitle(alphabet[charaIndex])
  55. flatPacked.push(post)
  56. })
  57. return flatPacked
  58. }
  59. const _arrangeByEpisode = postsList => {
  60. const byEpisode = {}
  61. postsList.forEach(post => {
  62. const relatedEpisode = post.related_episode
  63. if(!byEpisode[relatedEpisode]) byEpisode[relatedEpisode] = []
  64. byEpisode[relatedEpisode].push(post)
  65. })
  66. // TODO: Delete any undefined for now. Should figure out how these get returned in the firstplace.
  67. delete byEpisode['undefined']
  68. const flatPacked = _insertInBetweens(byEpisode)
  69. return flatPacked
  70. }
  71. const repackBySort = (postsList, sortedBy, seenTitles) => {
  72. let repacked = postsList
  73. if(sortedBy == sortTypes.material) {
  74. repacked = _arrangeByMaterial(postsList)
  75. } else if(sortedBy == sortTypes.episode) {
  76. repacked = _arrangeByEpisode(postsList)
  77. } else if(sortedBy == sortTypes.subtype) {
  78. repacked = _arrangeByType(postsList)
  79. } else if(sortedBy == sortTypes.alpha) {
  80. repacked = _arrangeByAlpha(postsList, seenTitles)
  81. }
  82. return repacked
  83. }
  84. const allBySlug = state => {
  85. return Object.values(state.all).reduce((bySlug, post) => {
  86. bySlug[post.slug] = post
  87. return bySlug
  88. }, {})
  89. }
  90. export {
  91. allBySlug,
  92. repackBySort,
  93. _arrangeByMaterial,
  94. _arrangeByType,
  95. _arrangeByAlpha,
  96. _arrangeByEpisode
  97. }