Skip to content

Commit

Permalink
after creating a project, go to its page
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazuh committed Jan 30, 2024
1 parent 96efd26 commit 16c79e5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { ProjectListingItem } from "./projects-management-entities";

export interface ProjectsManagementContextValue {
items: ProjectListingItem[];
create: (creating: Pick<ProjectListingItem, "name">) => Promise<void>;
create: (
creating: Pick<ProjectListingItem, "name">
) => Promise<ProjectListingItem | null>;
update: (updating: ProjectListingItem) => Promise<void>;
remove: (removing: ProjectListingItem) => Promise<void>;
isLoading: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ export function ProjectsManagementContextProvider({
async ({ name }) => {
setError(null);
setIsLoading(true);
persistNewProjectListingItem(name)
.catch((error) => setError(error))
.finally(() => setIsLoading(false))
.then(() => pullProjectsListing());
return persistNewProjectListingItem(name)
.then((created: ProjectListingItem) => {
pullProjectsListing();
return created;
})
.catch((error) => {
setError(error);
return null;
})
.finally(() => setIsLoading(false));
},
[pullProjectsListing]
);
Expand Down
20 changes: 17 additions & 3 deletions src/features/projects-management/ProjectsManagementPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ProjectListingItem } from "./projects-management-entities";
import { cn } from "@/lib/utils";
import { useProjectsManagement } from "./ProjectsManagementContext";
import { ProjectsManagementContextProvider } from "./ProjectsManagementContextProvider";
import { useLocation } from "wouter";

export function ProjectsManagementPage() {
return (
Expand Down Expand Up @@ -134,6 +135,7 @@ interface ProjectModalByButtonProps {
}

function ProjectModalByButton(props: ProjectModalByButtonProps) {
const [, setLocation] = useLocation();
const { create, update } = useProjectsManagement();

const [isOpen, setIsOpen] = useState(false);
Expand All @@ -153,12 +155,24 @@ function ProjectModalByButton(props: ProjectModalByButtonProps) {
},
});

const handleSubmit = (values: z.infer<typeof projectFormSchema>) => {
const handleSubmit = async (values: z.infer<typeof projectFormSchema>) => {
if (props.project) {
return update({ ...values, uuid: props.project.uuid }).then(close);
return update({ ...values, uuid: props.project.uuid })
.then(close)
.catch(() =>
form.setError("name", { message: "Something went wrong" })
);
}

return create(values).then(close);
return create(values).then((created) => {
if (!created) {
form.setError("name", { message: "Something went wrong" });
return;
}

close();
setLocation(`/project/${created.uuid}`);
});
};

return (
Expand Down

0 comments on commit 16c79e5

Please sign in to comment.