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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. flatPacked.push({ slug: letter, title: letter, inbetween: true })
  46. seenTitles.push(letter)
  47. }
  48. postsList.forEach(post => {
  49. const lastWord = post.slug.split('-').filter(c => c).pop()
  50. const firstCharaOflastWord = lastWord[0]
  51. const firstCharaOfSortWord = post.sortname ? post.sortname[0] : 'z'
  52. let charaIndex = alphabet.indexOf(firstCharaOflastWord) < alphabet.indexOf(firstCharaOfSortWord) ? alphabet.indexOf(firstCharaOflastWord) : alphabet.indexOf(firstCharaOfSortWord)
  53. // Kamaka problem
  54. if(seenTitles.includes(letter)) {
  55. charaIndex = alphabet.indexOf(firstCharaOflastWord) > alphabet.indexOf(firstCharaOfSortWord) ? alphabet.indexOf(firstCharaOflastWord) : alphabet.indexOf(firstCharaOfSortWord)
  56. }
  57. storeTitle(alphabet[charaIndex])
  58. flatPacked.push(post)
  59. })
  60. return flatPacked
  61. }
  62. const _arrangeByEpisode = postsList => {
  63. const byEpisode = {}
  64. postsList.forEach(post => {
  65. const relatedEpisode = post.related_episode
  66. if(!byEpisode[relatedEpisode]) byEpisode[relatedEpisode] = []
  67. byEpisode[relatedEpisode].push(post)
  68. })
  69. const flatPacked = _insertInBetweens(byEpisode)
  70. return flatPacked
  71. }
  72. const repackBySort = (postsList, sortedBy, seenTitles) => {
  73. let repacked = postsList
  74. if(sortedBy == sortTypes.material) {
  75. repacked = _arrangeByMaterial(postsList)
  76. } else if(sortedBy == sortTypes.episode) {
  77. repacked = _arrangeByEpisode(postsList)
  78. } else if(sortedBy == sortTypes.subtype) {
  79. repacked = _arrangeByType(postsList)
  80. } else if(sortedBy == sortTypes.alpha) {
  81. repacked = _arrangeByAlpha(postsList, seenTitles)
  82. }
  83. return repacked
  84. }
  85. const allBySlug = state => {
  86. return Object.values(state.all).reduce((bySlug, post) => {
  87. bySlug[post.slug] = post
  88. return bySlug
  89. }, {})
  90. }
  91. export {
  92. allBySlug,
  93. repackBySort,
  94. _arrangeByMaterial,
  95. _arrangeByType,
  96. _arrangeByAlpha,
  97. _arrangeByEpisode
  98. }