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

(FE) Retrieve Document List within Workspace #80

Merged
merged 7 commits into from
Jan 22, 2024
Merged
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
4 changes: 3 additions & 1 deletion backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export class UsersService {

const duplicatedWorkspaceList = await this.prismaService.workspace.findMany({
where: {
slug,
slug: {
startsWith: slug,
},
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class WorkspaceDocumentsService {

const duplicatedDocumentList = await this.prismaService.document.findMany({
where: {
slug,
slug: {
startsWith: slug,
},
},
});

Expand Down
4 changes: 3 additions & 1 deletion backend/src/workspaces/workspaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export class WorkspacesService {

const duplicatedWorkspaceList = await this.prismaService.workspace.findMany({
where: {
slug,
slug: {
startsWith: slug,
},
},
});

Expand Down
9 changes: 9 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"codemirror-toolbar": "^0.0.3",
"color": "^4.2.3",
"lib0": "^0.2.88",
"moment": "^2.30.1",
"randomcolor": "^0.6.2",
"react": "^17.0.0 || ^18.0.0",
"react-dom": "^17.0.0 || ^18.0.0",
Expand Down
37 changes: 37 additions & 0 deletions frontend/src/components/cards/DocumentCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import moment from "moment";
import { Card, CardActionArea, CardContent, Stack, Typography } from "@mui/material";
import AccessTimeIcon from "@mui/icons-material/AccessTime";
import { Document } from "../../hooks/api/types/document.d";

interface DocumentCardProps {
document: Document;
}

function DocumentCard(props: DocumentCardProps) {
const { document } = props;

return (
<Card sx={{ width: "100%" }}>
<CardActionArea>
<CardContent>
<Typography variant="h5" component="div" noWrap>
{document.title}
</Typography>
<Stack direction="row" alignItems="center" gap={1}>
<AccessTimeIcon
fontSize="small"
sx={{
color: "text.secondary",
}}
/>
<Typography variant="body2" color="text.secondary" noWrap>
Changed {moment(document.updatedAt).fromNow()}
</Typography>
</Stack>
</CardContent>
</CardActionArea>
</Card>
);
}

export default DocumentCard;
2 changes: 1 addition & 1 deletion frontend/src/components/drawers/WorkspaceDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function WorkspaceDrawer() {
boxSizing: "border-box",
},
}}
variant="persistent"
variant="permanent"
anchor="left"
open
>
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/components/popovers/WorkspaceListPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ function WorkspaceListPopover(props: WorkspaceListPopoverProps) {
}, [] as Array<Workspace>);
}, [workspacePageList?.pages]);

const handleMoveToSelectedWorkspace = (workspaceId: string) => {
if (params.workspaceId === workspaceId) return;
const handleMoveToSelectedWorkspace = (workspaceSlug: string) => {
if (params.workspaceSlug === workspaceSlug) return;

navigate(`/workspace/${workspaceId}`);
navigate(`/workspace/${workspaceSlug}`);
};

return (
Expand All @@ -50,7 +50,7 @@ function WorkspaceListPopover(props: WorkspaceListPopoverProps) {
>
<Box
style={{
height: 300,
maxHeight: 300,
overflow: "auto",
}}
>
Expand All @@ -69,7 +69,7 @@ function WorkspaceListPopover(props: WorkspaceListPopoverProps) {
{workspaceList?.map((workspace) => (
<MenuItem
key={workspace.id}
onClick={() => handleMoveToSelectedWorkspace(workspace.id)}
onClick={() => handleMoveToSelectedWorkspace(workspace.slug)}
>
<ListItemText
primaryTypographyProps={{
Expand All @@ -79,7 +79,7 @@ function WorkspaceListPopover(props: WorkspaceListPopoverProps) {
>
{workspace.title}
</ListItemText>
{params.workspaceId === workspace.id && (
{params.workspaceSlug === workspace.slug && (
<ListItemSecondaryAction>
<CheckIcon fontSize="small" />
</ListItemSecondaryAction>
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/hooks/api/types/document.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export class Document {
id: string;
yorkieDocumentId: string;
title: string;
slug: string;
content?: string;
createdAt: Date;
updatedAt: Date;
}

export class GetWorkspaceDocumentListResponse {
cursor: string | null;
documents: Array<Workspace>;
}
2 changes: 1 addition & 1 deletion frontend/src/hooks/api/types/workspace.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface Workspace {
export class Workspace {
id: string;
title: string;
slug: string;
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/hooks/api/workspaceDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useInfiniteQuery } from "@tanstack/react-query";
import axios from "axios";
import { GetWorkspaceDocumentListResponse } from "./types/document";

export const generateGetWorkspaceDocumentListQueryKey = (workspaceId: string) => {
return ["workspaces", workspaceId, "documents"];
};

export const useGetWorkspaceDocumentListQuery = (workspaceId?: string) => {
const query = useInfiniteQuery<GetWorkspaceDocumentListResponse>({
queryKey: generateGetWorkspaceDocumentListQueryKey(workspaceId || ""),
queryFn: async ({ pageParam }) => {
const res = await axios.get<GetWorkspaceDocumentListResponse>(
`/workspaces/${workspaceId}/documents`,
{
params: {
cursor: pageParam,
},
}
);
return res.data;
},
enabled: Boolean(workspaceId),
initialPageParam: undefined,
getPreviousPageParam: (firstPage) => firstPage.cursor ?? undefined,
getNextPageParam: (lastPage) => lastPage.cursor ?? undefined,
});

return query;
};
37 changes: 36 additions & 1 deletion frontend/src/pages/workspace/Index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import { useParams } from "react-router-dom";
import WorkspaceDrawer from "../../components/drawers/WorkspaceDrawer";
import { useGetWorkspaceDocumentListQuery } from "../../hooks/api/workspaceDocument";
import { useGetWorkspaceQuery } from "../../hooks/api/workspace";
import { Box, Grid, Stack } from "@mui/material";
import DocumentCard from "../../components/cards/DocumentCard";
import { useMemo } from "react";
import { Document } from "../../hooks/api/types/document.d";

function WorkspaceIndex() {
return <WorkspaceDrawer />;
const params = useParams();
const { data: workspace } = useGetWorkspaceQuery(params.workspaceSlug);
const { data: documentPageList } = useGetWorkspaceDocumentListQuery(workspace?.id);
const documentList = useMemo(() => {
return (
documentPageList?.pages.reduce((prev, page) => {
return prev.concat(page.documents);
}, [] as Array<Document>) ?? []
);
}, [documentPageList?.pages]);

return (
<Stack direction="row">
<WorkspaceDrawer />
<Box p={2}>
<Grid
container
spacing={{ xs: 2, md: 3 }}
columns={{ xs: 4, sm: 8, md: 12, lg: 12 }}
>
{documentList.map((document, idx) => (
<Grid key={idx} item xs={4} sm={4} md={4} lg={3}>
<DocumentCard document={document} />
</Grid>
))}
</Grid>
</Box>
</Stack>
);
}

export default WorkspaceIndex;
Loading