mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
select newly created project/org
This commit is contained in:
@@ -45,7 +45,7 @@ function Index() {
|
|||||||
const [issues, setIssues] = useState<IssueResponse[]>([]);
|
const [issues, setIssues] = useState<IssueResponse[]>([]);
|
||||||
const [selectedIssue, setSelectedIssue] = useState<IssueResponse | null>(null);
|
const [selectedIssue, setSelectedIssue] = useState<IssueResponse | null>(null);
|
||||||
|
|
||||||
const refetchOrganisations = async () => {
|
const refetchOrganisations = async (options?: { selectOrganisationId?: number }) => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${serverURL}/organisation/by-user?userId=${user.id}`, {
|
const res = await fetch(`${serverURL}/organisation/by-user?userId=${user.id}`, {
|
||||||
headers: getAuthHeaders(),
|
headers: getAuthHeaders(),
|
||||||
@@ -54,7 +54,16 @@ function Index() {
|
|||||||
|
|
||||||
setOrganisations(data);
|
setOrganisations(data);
|
||||||
|
|
||||||
// attempt to retain selected organisation (avoid re-fetching projects and issues)
|
// select newly created organisation
|
||||||
|
if (options?.selectOrganisationId) {
|
||||||
|
const created = data.find((o) => o.Organisation.id === options.selectOrganisationId);
|
||||||
|
if (created) {
|
||||||
|
setSelectedOrganisation(created);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// preserve previously selected organisation
|
||||||
setSelectedOrganisation((prev) => {
|
setSelectedOrganisation((prev) => {
|
||||||
if (!prev) return data[0] || null;
|
if (!prev) return data[0] || null;
|
||||||
const stillExists = data.find((o) => o.Organisation.id === prev.Organisation.id);
|
const stillExists = data.find((o) => o.Organisation.id === prev.Organisation.id);
|
||||||
@@ -71,7 +80,7 @@ function Index() {
|
|||||||
void refetchOrganisations();
|
void refetchOrganisations();
|
||||||
}, [user.id]);
|
}, [user.id]);
|
||||||
|
|
||||||
const refetchProjects = async (organisationId: number) => {
|
const refetchProjects = async (organisationId: number, options?: { selectProjectId?: number }) => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
`${serverURL}/projects/by-organisation?organisationId=${organisationId}`,
|
`${serverURL}/projects/by-organisation?organisationId=${organisationId}`,
|
||||||
@@ -83,6 +92,16 @@ function Index() {
|
|||||||
const data = (await res.json()) as ProjectResponse[];
|
const data = (await res.json()) as ProjectResponse[];
|
||||||
setProjects(data);
|
setProjects(data);
|
||||||
|
|
||||||
|
// select newly created project
|
||||||
|
if (options?.selectProjectId) {
|
||||||
|
const created = data.find((p) => p.Project.id === options.selectProjectId);
|
||||||
|
if (created) {
|
||||||
|
setSelectedProject(created);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// preserve previously selected project
|
||||||
setSelectedProject((prev) => {
|
setSelectedProject((prev) => {
|
||||||
if (!prev) return data[0] || null;
|
if (!prev) return data[0] || null;
|
||||||
const stillExists = data.find((p) => p.Project.id === prev.Project.id);
|
const stillExists = data.find((p) => p.Project.id === prev.Project.id);
|
||||||
@@ -175,7 +194,12 @@ function Index() {
|
|||||||
Create Organisation
|
Create Organisation
|
||||||
</Button>
|
</Button>
|
||||||
}
|
}
|
||||||
completeAction={refetchOrganisations}
|
completeAction={async (organisationId) => {
|
||||||
|
if (!selectedOrganisation) return;
|
||||||
|
await refetchOrganisations({
|
||||||
|
selectOrganisationId: organisationId,
|
||||||
|
});
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
@@ -229,9 +253,11 @@ function Index() {
|
|||||||
Create Project
|
Create Project
|
||||||
</Button>
|
</Button>
|
||||||
}
|
}
|
||||||
completeAction={async () => {
|
completeAction={async (projectId) => {
|
||||||
if (!selectedOrganisation) return;
|
if (!selectedOrganisation) return;
|
||||||
await refetchProjects(selectedOrganisation.Organisation.id);
|
await refetchProjects(selectedOrganisation.Organisation.id, {
|
||||||
|
selectProjectId: projectId,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export function CreateOrganisation({
|
|||||||
completeAction,
|
completeAction,
|
||||||
}: {
|
}: {
|
||||||
trigger?: React.ReactNode;
|
trigger?: React.ReactNode;
|
||||||
completeAction?: () => void | Promise<void>;
|
completeAction?: (organisationId: number) => void | Promise<void>;
|
||||||
}) {
|
}) {
|
||||||
const serverURL = import.meta.env.VITE_SERVER_URL?.trim() || "http://localhost:3000";
|
const serverURL = import.meta.env.VITE_SERVER_URL?.trim() || "http://localhost:3000";
|
||||||
|
|
||||||
@@ -109,10 +109,17 @@ export function CreateOrganisation({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const organisation = (await res.json()) as { id?: number };
|
||||||
|
if (!organisation.id) {
|
||||||
|
setError("failed to create organisation");
|
||||||
|
setSubmitting(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
reset();
|
reset();
|
||||||
try {
|
try {
|
||||||
await completeAction?.();
|
await completeAction?.(organisation.id);
|
||||||
} catch (actionErr) {
|
} catch (actionErr) {
|
||||||
console.error(actionErr);
|
console.error(actionErr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export function CreateProject({
|
|||||||
}: {
|
}: {
|
||||||
organisationId?: number;
|
organisationId?: number;
|
||||||
trigger?: React.ReactNode;
|
trigger?: React.ReactNode;
|
||||||
completeAction?: () => void | Promise<void>;
|
completeAction?: (projectId: number) => void | Promise<void>;
|
||||||
}) {
|
}) {
|
||||||
const serverURL = import.meta.env.VITE_SERVER_URL?.trim() || "http://localhost:3000";
|
const serverURL = import.meta.env.VITE_SERVER_URL?.trim() || "http://localhost:3000";
|
||||||
|
|
||||||
@@ -113,10 +113,17 @@ export function CreateProject({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const project = (await res.json()) as { id?: number };
|
||||||
|
if (!project.id) {
|
||||||
|
setError("failed to create project");
|
||||||
|
setSubmitting(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
reset();
|
reset();
|
||||||
try {
|
try {
|
||||||
await completeAction?.();
|
await completeAction?.(project.id);
|
||||||
} catch (actionErr) {
|
} catch (actionErr) {
|
||||||
console.error(actionErr);
|
console.error(actionErr);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user