123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import {createStore, StoreDefinition} from 'reflux';
- import {Project} from 'sentry/types';
- interface ProjectsStatsStoreDefinition extends StoreDefinition {
- getAll(): ProjectsStatsStoreDefinition['itemsBySlug'];
- getBySlug(slug: string): Project;
- getInitialState(): ProjectsStatsStoreDefinition['itemsBySlug'];
- itemsBySlug: Record<string, Project>;
- onStatsLoadSuccess(projects: Project[]): void;
- onUpdate(projectSlug: string, data: Partial<Project>): void;
- onUpdateError(err: Error, projectSlug: string): void;
- reset(): void;
- }
- /**
- * This is a store specifically used by the dashboard, so that we can
- * clear the store when the Dashboard unmounts
- * (as to not disrupt ProjectsStore which a lot more components use)
- */
- const storeConfig: ProjectsStatsStoreDefinition = {
- itemsBySlug: {},
- init() {
- this.reset();
- },
- getInitialState() {
- return this.itemsBySlug;
- },
- reset() {
- this.itemsBySlug = {};
- this.updatingItems = new Map();
- },
- onStatsLoadSuccess(projects) {
- projects.forEach(project => {
- this.itemsBySlug[project.slug] = project;
- });
- this.trigger(this.itemsBySlug);
- },
- /**
- * Optimistic updates
- * @param projectSlug Project slug
- * @param data Project data
- */
- onUpdate(projectSlug, data) {
- const project = this.getBySlug(projectSlug);
- this.updatingItems.set(projectSlug, project);
- if (!project) {
- return;
- }
- const newProject: Project = {
- ...project,
- ...data,
- };
- this.itemsBySlug = {
- ...this.itemsBySlug,
- [project.slug]: newProject,
- };
- this.trigger(this.itemsBySlug);
- },
- onUpdateSuccess(data: Project) {
- // Remove project from updating map
- this.updatingItems.delete(data.slug);
- },
- /**
- * Revert project data when there was an error updating project details
- * @param err Error object
- * @param data Previous project data
- */
- onUpdateError(_err, projectSlug) {
- const project = this.updatingItems.get(projectSlug);
- if (!project) {
- return;
- }
- this.updatingItems.delete(projectSlug);
- // Restore old project
- this.itemsBySlug = {
- ...this.itemsBySlug,
- [project.slug]: {...project},
- };
- this.trigger(this.itemsBySlug);
- },
- getAll() {
- return this.itemsBySlug;
- },
- getBySlug(slug) {
- return this.itemsBySlug[slug];
- },
- };
- const ProjectsStatsStore = createStore(storeConfig);
- export default ProjectsStatsStore;
|