projectsStatsStore.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Reflux from 'reflux';
  2. import ProjectActions from 'sentry/actions/projectActions';
  3. import {Project} from 'sentry/types';
  4. type ProjectsStatsStoreInterface = {
  5. itemsBySlug: Record<string, Project>;
  6. getInitialState(): ProjectsStatsStoreInterface['itemsBySlug'];
  7. reset(): void;
  8. getBySlug(slug: string): Project;
  9. getAll(): ProjectsStatsStoreInterface['itemsBySlug'];
  10. };
  11. /**
  12. * This is a store specifically used by the dashboard, so that we can
  13. * clear the store when the Dashboard unmounts
  14. * (as to not disrupt ProjectsStore which a lot more components use)
  15. */
  16. const storeConfig: Reflux.StoreDefinition & ProjectsStatsStoreInterface = {
  17. itemsBySlug: {},
  18. init() {
  19. this.reset();
  20. this.listenTo(ProjectActions.loadStatsForProjectSuccess, this.onStatsLoadSuccess);
  21. this.listenTo(ProjectActions.update, this.onUpdate);
  22. this.listenTo(ProjectActions.updateError, this.onUpdateError);
  23. },
  24. getInitialState() {
  25. return this.itemsBySlug;
  26. },
  27. reset() {
  28. this.itemsBySlug = {};
  29. this.updatingItems = new Map();
  30. },
  31. onStatsLoadSuccess(projects: Project[]) {
  32. projects.forEach(project => {
  33. this.itemsBySlug[project.slug] = project;
  34. });
  35. this.trigger(this.itemsBySlug);
  36. },
  37. /**
  38. * Optimistic updates
  39. * @param projectSlug Project slug
  40. * @param data Project data
  41. */
  42. onUpdate(projectSlug: string, data: Project) {
  43. const project = this.getBySlug(projectSlug);
  44. this.updatingItems.set(projectSlug, project);
  45. if (!project) {
  46. return;
  47. }
  48. const newProject: Project = {
  49. ...project,
  50. ...data,
  51. };
  52. this.itemsBySlug = {
  53. ...this.itemsBySlug,
  54. [project.slug]: newProject,
  55. };
  56. this.trigger(this.itemsBySlug);
  57. },
  58. onUpdateSuccess(data: Project) {
  59. // Remove project from updating map
  60. this.updatingItems.delete(data.slug);
  61. },
  62. /**
  63. * Revert project data when there was an error updating project details
  64. * @param err Error object
  65. * @param data Previous project data
  66. */
  67. onUpdateError(_err: Error, projectSlug: string) {
  68. const project = this.updatingItems.get(projectSlug);
  69. if (!project) {
  70. return;
  71. }
  72. this.updatingItems.delete(projectSlug);
  73. // Restore old project
  74. this.itemsBySlug = {
  75. ...this.itemsBySlug,
  76. [project.slug]: {...project},
  77. };
  78. this.trigger(this.itemsBySlug);
  79. },
  80. getAll() {
  81. return this.itemsBySlug;
  82. },
  83. getBySlug(slug) {
  84. return this.itemsBySlug[slug];
  85. },
  86. };
  87. const ProjectsStatsStore = Reflux.createStore(storeConfig) as Reflux.Store &
  88. ProjectsStatsStoreInterface;
  89. export default ProjectsStatsStore;