controllers/dashboard/successful_builds_controller.js

  1. import { Controller } from "@hotwired/stimulus"
  2. import { subscription } from "~/javascripts/store/mixins/subscription"
  3. /**
  4. * @class Dashboard.SuccessfulBuildsController
  5. * @classdesc Stimulus controller that populates the number of successful builds per build context.
  6. * @extends Controller
  7. **/
  8. export default class extends Controller {
  9. static targets = [ "production", "deployPreview", "cms" ]
  10. static values = {
  11. loading: String,
  12. storeId: String
  13. }
  14. /**
  15. * Subscribe to the store.
  16. *
  17. * @instance
  18. * @memberof Dashboard.SuccessfulBuildsController
  19. **/
  20. connect() {
  21. subscription(this)
  22. this.subscribe()
  23. this.reconnect()
  24. }
  25. /**
  26. * Handles a repeated Turbo visit to the dashboard page.
  27. *
  28. * @instance
  29. * @memberof Dashboard.SuccessfulBuildsController
  30. **/
  31. reconnect() {
  32. if (this.store("selectedDataVizData")) {
  33. this.storeUpdated("selectedDataVizData", this.storeIdValue)
  34. }
  35. }
  36. /**
  37. * Sets the text for the number of builds
  38. *
  39. * @instance
  40. * @memberof Dashboard.SuccessfulBuildsController
  41. **/
  42. updateSuccessfulBuildsValue(target, text, context) {
  43. target.innerHTML = `${text} ${this.calculateNumberOfBuilds(context)}`
  44. }
  45. /**
  46. * Sets the text whilst waiting for the number of builds
  47. *
  48. * @instance
  49. * @memberof Dashboard.SuccessfulBuildsController
  50. **/
  51. loadingSuccessfulBuildsValue(target, text) {
  52. target.innerHTML = `${text} ${this.loadingValue}`
  53. }
  54. /**
  55. * @instance
  56. * @memberof Dashboard.SuccessfulBuildsController
  57. **/
  58. calculateNumberOfBuilds(context) {
  59. return this.store("selectedDataVizData").reduce((acc , data) => {
  60. return data.context === context ? acc + 1 : acc
  61. }, 0)
  62. }
  63. /**
  64. * Triggered by the store whenever any store data changes. Controls whether the number of builds should
  65. * be populated or display the loading indicator
  66. *
  67. * @instance
  68. * @memberof Dashboard.SuccessfulBuildsController
  69. **/
  70. storeUpdated(prop, storeId) {
  71. if (this.store("fetchingDataVizData")) {
  72. this.loadingSuccessfulBuildsValue(this.productionTarget, "Production")
  73. this.loadingSuccessfulBuildsValue(this.deployPreviewTarget, "Deploy preview")
  74. this.loadingSuccessfulBuildsValue(this.cmsTarget, "CMS")
  75. }
  76. if (prop === "selectedDataVizData" && storeId === this.storeIdValue) {
  77. this.updateSuccessfulBuildsValue(this.productionTarget, "Production ...", "production")
  78. this.updateSuccessfulBuildsValue(this.deployPreviewTarget, "Deploy preview ...", "deploy-preview")
  79. this.updateSuccessfulBuildsValue(this.cmsTarget, "CMS ...", "cms")
  80. }
  81. }
  82. /**
  83. * Unsubscribe from the store
  84. *
  85. * @instance
  86. * @memberof Dashboard.SuccessfulBuildsController
  87. **/
  88. disconnect() {
  89. this.unsubscribe()
  90. }
  91. }