Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/95/archive project #99

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions app/core/components/ArchiveProject/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Archive } from "@mui/icons-material";
import {
Button,
CircularProgress,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
IconButton,
Tooltip,
} from "@mui/material";
import { Form, useTransition } from "@remix-run/react";
import { useEffect, useState } from "react";

export const ArchiveProject = ({ projectId }: { projectId?: string }) => {
const [open, setOpen] = useState(false);
const [isButtonDisabled, setisButtonDisabled] = useState(true);

const handleClickOpen = () => {
setOpen(true);

setTimeout(() => setisButtonDisabled(false), 5000);
};

const handleClose = () => {
setisButtonDisabled(true);
setOpen(false);
};

const transition = useTransition();
useEffect(() => {
if (transition.type == "actionRedirect") {
setOpen(false);
}
}, [transition]);

return (
<>
<Tooltip title="Archive project">
<IconButton
onClick={handleClickOpen}
aria-label="Archive-project-button"
>
<Archive />
</IconButton>
</Tooltip>

<Dialog onClose={handleClose} open={open}>
<DialogTitle>
Are you sure you want to archive this proposal?
</DialogTitle>
<Form action={`/projects/archive`} method="post">
<DialogContent>
You can unarchive the project later.
<input type="hidden" name="projectId" value={projectId} />
</DialogContent>
<DialogActions>
<Button className="primary" onClick={handleClose}>
Cancel
</Button>

<Button
disabled={isButtonDisabled}
type="submit"
variant="contained"
color="warning"
>
Yes, archive it
</Button>
{isButtonDisabled && (
<CircularProgress
size={24}
sx={{
position: "absolute",
left: "80%",
marginTop: "-1px",
marginLeft: "-1px",
}}
/>
)}
</DialogActions>
</Form>
</Dialog>
</>
);
};
5 changes: 4 additions & 1 deletion app/core/components/ProposalCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ interface IProps {
votesCount?: number | null;
skills?: { name: string }[];
isOwner?: boolean;
isArchived?: boolean;
tierName: String;
projectMembers?: number | null;
}

export const ProposalCard = (props: IProps) => {
const stopEvent = (event: React.MouseEvent<HTMLElement>) =>
event.stopPropagation();

return (
<>
<Card>
<Card variant={props?.isArchived ? "outlined" : "elevation"}>
<CardActionArea sx={{ height: "100%" }} href={`/projects/${props.id}`}>
<CardContent>
<ProposalCardWrap>
Expand All @@ -48,6 +50,7 @@ export const ProposalCard = (props: IProps) => {
<div className="ProposalCard__head__description">
<div className="ProposalCard__head__description--title">
{props.title}
{props.isArchived && <p>(Archived)</p>}
</div>
<div className="ProposalCard__head__description--date">
{props.date}
Expand Down
85 changes: 85 additions & 0 deletions app/core/components/UnarchiveProject/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Unarchive } from "@mui/icons-material";
import {
Button,
CircularProgress,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
IconButton,
Tooltip,
} from "@mui/material";
import { Form, useTransition } from "@remix-run/react";
import { useEffect, useState } from "react";

export const UnarchiveProject = ({ projectId }: { projectId?: string }) => {
const [open, setOpen] = useState(false);
const [isButtonDisabled, setisButtonDisabled] = useState(true);

const handleClickOpen = () => {
setOpen(true);

setTimeout(() => setisButtonDisabled(false), 5000);
};

const handleClose = () => {
setisButtonDisabled(true);
setOpen(false);
};

const transition = useTransition();
useEffect(() => {
if (transition.type == "actionRedirect") {
setOpen(false);
}
}, [transition]);

return (
<>
<Tooltip title="Unarchive project">
<IconButton
onClick={handleClickOpen}
aria-label="Unarchive-project-button"
>
<Unarchive color="secondary" />
</IconButton>
</Tooltip>

<Dialog onClose={handleClose} open={open}>
<DialogTitle>
Are you sure you want to unarchive this proposal?
</DialogTitle>
<Form action={`/projects/unarchive`} method="post">
<DialogContent>
This action will unarchive the project and will be available again.
<input type="hidden" name="projectId" value={projectId} />
</DialogContent>
<DialogActions>
<Button className="primary" onClick={handleClose}>
Cancel
</Button>
<Button
disabled={isButtonDisabled}
type="submit"
variant="contained"
color="warning"
>
Unarchive it
</Button>
{isButtonDisabled && (
<CircularProgress
size={24}
sx={{
position: "absolute",
left: "85%",
marginTop: "-1px",
marginLeft: "-1px",
}}
/>
)}
</DialogActions>
</Form>
</Dialog>
</>
);
};
100 changes: 96 additions & 4 deletions app/models/project.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CompressOutlined } from "@mui/icons-material";
import type { Profiles, Projects } from "@prisma/client";
import { Prisma } from "@prisma/client";
import { defaultStatus } from "~/constants";

import { joinCondition, prisma as db } from "~/db.server";
import { log } from "console";

interface SearchProjectsInput {
profileId: Profiles["id"];
Expand Down Expand Up @@ -36,6 +38,7 @@ interface SearchProjectsOutput {
projectMembers: number;
owner: string;
tierName: string;
isArchived: boolean;
}

interface ProjectWhereInput {
Expand Down Expand Up @@ -520,13 +523,18 @@ export async function searchProjects({
skip = 0,
take = 50,
}: SearchProjectsInput) {
let where = Prisma.sql`WHERE p.id IS NOT NULL`;
let where = Prisma.sql`WHERE p.id IS NOT NULL AND p."isArchived" = false`;
let having = Prisma.empty;
if (search && search !== "") {
search === "myProposals"
? (where = Prisma.sql`WHERE pm."profileId" = ${profileId}`)
: (where = Prisma.sql`WHERE "tsColumn" @@ websearch_to_tsquery('english', ${search})`);
if (search === "myProposals") {
where = Prisma.sql`WHERE pm."profileId" = ${profileId}`;
} else if (search === "archivedProjects") {
where = Prisma.sql`WHERE p."isArchived" = true`;
} else {
where = Prisma.sql`WHERE "tsColumn" @@ websearch_to_tsquery('english', ${search})`;
}
}
console.log(where);

if (status.length > 0) {
where = Prisma.sql`${where} AND p.status IN (${Prisma.join(status)})`;
Expand Down Expand Up @@ -644,6 +652,7 @@ export async function searchProjects({
p."updatedAt",
p."ownerId",
p."tierName",
p."isArchived",
COUNT(DISTINCT pm."profileId") as "projectMembers"
FROM "Projects" p
INNER JOIN "ProjectStatus" s on s.name = p.status
Expand Down Expand Up @@ -771,3 +780,86 @@ export async function deleteProject(projectId: string, isAdmin: boolean) {
}
return true;
}

export async function archiveProject(
projectId: string,
profileId: string,
isAdmin: boolean
) {
const currentProject = await db.projects.findUniqueOrThrow({
where: { id: projectId },
select: {
ownerId: true,
},
});

const projectMembers = await db.projectMembers.findMany({
where: { projectId },
select: {
profileId: true,
},
});

if (!isAdmin)
validateIsTeamMember(profileId, projectMembers, currentProject.ownerId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we should move these validation functions out of the *.server.ts files, and instead do them inside the action functions. That way the database layer will be cleaner, and authorization logic will live close to the view/controller layer.

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an example of this, look at the app/routes/projects/$projectId/updateRelatedProjects.ts file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, makes sense for me. Do you want me to start it on this ticket? 👀

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but only for the validations you added. To move them to the app/routes/projects/archive.tsx action.


const project = await db.projects.update({
where: { id: projectId },
data: {
updatedAt: new Date(),
isArchived: true,
},
include: {
projectStatus: true,
},
});

return project;
}

export async function unarchiveProject(
projectId: string,
profileId: string,
isAdmin: boolean
) {
const currentProject = await db.projects.findUniqueOrThrow({
where: { id: projectId },
select: {
ownerId: true,
},
});

const projectMembers = await db.projectMembers.findMany({
where: { projectId },
select: {
profileId: true,
},
});

if (!isAdmin)
validateIsTeamMember(profileId, projectMembers, currentProject.ownerId);

const project = await db.projects.update({
where: { id: projectId },
data: {
updatedAt: new Date(),
isArchived: false,
},
include: {
projectStatus: true,
},
});

return project;
}

export async function existArchivedProjects() {
const archiveProjects = await db.projects.findMany({
where: { isArchived: true },
});
if (archiveProjects.length > 0) {
return true;
} else {
return false;
}
}
4 changes: 0 additions & 4 deletions app/routes/manager/filter-tags/test/labels.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ describe("Labels test", () => {
});

test("Path loader", async () => {
let request = new Request(
"http://localhost:3000/manager/filter-tags/labels"
);

const response = await loader();

expect(response).toBeInstanceOf(Response);
Expand Down
Loading