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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. let charaIndex = null
  54. let otherIndex = null
  55. const lastWordAlphaIndex = alphabet.indexOf(firstCharaOflastWord)
  56. const altWordAlphaIndex = alphabet.indexOf(firstCharaOfSortWord)
  57. if(lastWordAlphaIndex < altWordAlphaIndex) {
  58. charaIndex = lastWordAlphaIndex
  59. otherIndex = altWordAlphaIndex
  60. } else if (lastWordAlphaIndex > altWordAlphaIndex) {
  61. otherIndex = lastWordAlphaIndex
  62. charaIndex = altWordAlphaIndex
  63. }
  64. if(seenTitles.includes(alphabet[charaIndex])) {
  65. charaIndex = otherIndex
  66. }
  67. storeTitle(alphabet[charaIndex])
  68. flatPacked.push(post)
  69. })
  70. return flatPacked
  71. }
  72. const _arrangeByEpisode = postsList => {
  73. const byEpisode = {}
  74. postsList.forEach(post => {
  75. const relatedEpisode = post.related_episode
  76. if(!byEpisode[relatedEpisode]) byEpisode[relatedEpisode] = []
  77. byEpisode[relatedEpisode].push(post)
  78. })
  79. const flatPacked = _insertInBetweens(byEpisode)
  80. return flatPacked
  81. }
  82. const repackBySort = (postsList, sortedBy, seenTitles) => {
  83. let repacked = postsList
  84. if(sortedBy == sortTypes.material) {
  85. repacked = _arrangeByMaterial(postsList)
  86. } else if(sortedBy == sortTypes.episode) {
  87. repacked = _arrangeByEpisode(postsList)
  88. } else if(sortedBy == sortTypes.subtype) {
  89. repacked = _arrangeByType(postsList)
  90. } else if(sortedBy == sortTypes.alpha) {
  91. repacked = _arrangeByAlpha(postsList, seenTitles)
  92. }
  93. return repacked
  94. }
  95. const allBySlug = state => {
  96. return Object.values(state.all).reduce((bySlug, post) => {
  97. bySlug[post.slug] = post
  98. return bySlug
  99. }, {})
  100. }
  101. export {
  102. allBySlug,
  103. repackBySort,
  104. _arrangeByMaterial,
  105. _arrangeByType,
  106. _arrangeByAlpha,
  107. _arrangeByEpisode
  108. }