Skip to content

Commit

Permalink
extract useLocalProjects
Browse files Browse the repository at this point in the history
  • Loading branch information
SillyFreak committed Jun 12, 2020
1 parent 0609348 commit 45b1082
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/components/projects/ProjectList/ProjectList.js
Expand Up @@ -43,6 +43,7 @@ import useCreateProjectDialog from './useCreateProjectDialog';
import useDeleteProjectDialog from './useDeleteProjectDialog';
import useRenameProjectDialog from './useRenameProjectDialog';
import useProjectIndex from './useProjectIndex';
import useLocalProjects from './useLocalProjects';

import { type Projects } from './__generated__/Projects';
import {
Expand Down Expand Up @@ -156,7 +157,7 @@ function ProjectList(_props: Props) {
const auth = useAuth();
const intl = useIntl();

const [projects, setProjects] = hooks.useAsyncState<Project[]>([]);
const [projects, refreshProjects] = useLocalProjects();

const remoteProjectsQuery = useProjectsQuery();
const remoteProjects = remoteProjectsQuery.data?.projects ?? [];
Expand All @@ -183,14 +184,6 @@ function ProjectList(_props: Props) {
});
}

function refreshProjects() {
setProjects(Project.getProjects());
}

React.useEffect(() => {
refreshProjects();
}, []);

async function confirmCreateProject(name: string): Promise<boolean> {
try {
await Project.createProject(name);
Expand Down
21 changes: 21 additions & 0 deletions src/components/projects/ProjectList/useLocalProjects.js
@@ -0,0 +1,21 @@
// @flow

import * as React from 'react';

import * as hooks from '../../misc/hooks';

import { Project } from '../../../core/store/projects';

export default function useLocalProjects(): [Project[], () => void] {
const [projects, setProjects] = hooks.useAsyncState<Project[]>([]);

function refreshProjects() {
setProjects(Project.getProjects());
}

React.useEffect(() => {
refreshProjects();
}, []);

return [projects, refreshProjects];
}

0 comments on commit 45b1082

Please sign in to comment.