Skip to content

Commit

Permalink
ref(stores): Make ProjectsStore useLegacyStore compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Oct 12, 2021
1 parent 5aedd9f commit db38e88
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
14 changes: 10 additions & 4 deletions static/app/stores/projectsStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ProjectActions from 'app/actions/projectActions';
import TeamActions from 'app/actions/teamActions';
import {Project, Team} from 'app/types';

import {CommonStoreInterface} from './types';

type State = {
projects: Project[];
loading: boolean;
Expand All @@ -19,7 +21,7 @@ type Internals = {
loading: boolean;
};

type ProjectsStoreInterface = {
type ProjectsStoreInterface = CommonStoreInterface<State> & {
init(): void;
reset(): void;
loadInitialData(projects: Project[]): void;
Expand All @@ -31,10 +33,10 @@ type ProjectsStoreInterface = {
onRemoveTeam(teamSlug: string, projectSlug: string): void;
onAddTeam(team: Team, projectSlug: string): void;
removeTeamFromProject(teamSlug: string, project: Project): void;
isLoading(): boolean;
getWithTeam(teamSlug: string): Project[];
getAll(): Project[];
getBySlugs(slug: string[]): Project[];
getState(slugs?: string[]): State;
getById(id?: string): Project | undefined;
getBySlug(slug?: string): Project | undefined;
};
Expand Down Expand Up @@ -193,6 +195,10 @@ const storeConfig: Reflux.StoreDefinition & Internals & ProjectsStoreInterface =
return this.getAll().filter(({teams}) => teams.find(({slug}) => slug === teamSlug));
},

isLoading() {
return this.loading;
},

getAll() {
return Object.values(this.itemsById).sort((a: Project, b: Project) => {
if (a.slug > b.slug) {
Expand All @@ -217,9 +223,9 @@ const storeConfig: Reflux.StoreDefinition & Internals & ProjectsStoreInterface =
return this.getAll().filter(project => slugs.includes(project.slug));
},

getState(slugs?: string[]): State {
getState() {
return {
projects: slugs ? this.getBySlugs(slugs) : this.getAll(),
projects: this.getAll(),
loading: this.loading,
};
},
Expand Down
11 changes: 9 additions & 2 deletions static/app/utils/withProjects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ function withProjects<P extends InjectedProjectsProps>(
> {
static displayName = `withProjects(${getDisplayName(WrappedComponent)})`;

state: State = ProjectsStore.getState();
state: State = {
projects: ProjectsStore.getAll(),
loading: ProjectsStore.isLoading(),
};

componentWillUnmount() {
this.unsubscribe();
}

unsubscribe = ProjectsStore.listen(
() => this.setState(ProjectsStore.getState()),
() =>
this.setState({
projects: ProjectsStore.getAll(),
loading: ProjectsStore.isLoading(),
}),
undefined
);

Expand Down
15 changes: 12 additions & 3 deletions static/app/utils/withProjectsSpecified.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,27 @@ function withProjectsSpecified<P extends InjectedProjectsProps>(
> {
static displayName = `withProjectsSpecified(${getDisplayName(WrappedComponent)})`;

state = ProjectsStore.getState(this.props.specificProjectSlugs);
state: State = {
projects: ProjectsStore.getBySlugs(this.props.specificProjectSlugs ?? []),
loading: ProjectsStore.isLoading(),
};

static getDerivedStateFromProps(nextProps: Readonly<Props>): State {
return ProjectsStore.getState(nextProps.specificProjectSlugs);
return {
projects: ProjectsStore.getBySlugs(nextProps.specificProjectSlugs ?? []),
loading: ProjectsStore.isLoading(),
};
}

componentWillUnmount() {
this.unsubscribe();
}

unsubscribe = ProjectsStore.listen(() => {
const storeState = ProjectsStore.getState(this.props.specificProjectSlugs);
const storeState: State = {
projects: ProjectsStore.getBySlugs(this.props.specificProjectSlugs ?? []),
loading: ProjectsStore.isLoading(),
};

if (!isEqual(this.state, storeState)) {
this.setState(storeState);
Expand Down

0 comments on commit db38e88

Please sign in to comment.