Skip to content

Commit

Permalink
Improved required_files in project view
Browse files Browse the repository at this point in the history
  • Loading branch information
RunoBoy committed May 19, 2024
1 parent d54e312 commit d028afe
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def test_can_create_submission_without_forbidden_wildcard(self):
"file_urls": "src/main.jar, src/test.dockerfile"})
self.assertEqual(0, response.data['success'])
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

#
def test_can_create_submission_with_forbidden_wildcard(self):
response = self.client.post(API_ENDPOINT, {"group_id": self.group_4.group_id, "file_urls": "src/main.py"})
Expand Down
53 changes: 51 additions & 2 deletions frontend/app/[locale]/components/ProjectDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,16 @@ const ProjectDetailsPage: React.FC<ProjectDetailsPageProps> = ({
</IconButton>
)}
<Typography variant="h6">{t("required_files")}</Typography>
<Typography>{project?.file_structure}</Typography>
<Typography variant={"body1"}>
<pre>
{generateDirectoryTree(project?.file_structure).split('\n').map((line: string, index: number) => (
<React.Fragment key={index}>
{line}
<br/>
</React.Fragment>
))}
</pre>
</Typography>
<Typography variant="h6">{t("conditions")}</Typography>
<Typography>{project?.conditions}</Typography>
<Typography>
Expand Down Expand Up @@ -220,4 +229,44 @@ const ProjectDetailsPage: React.FC<ProjectDetailsPageProps> = ({
);
};

export default ProjectDetailsPage;
function buildTree(paths) {
const tree = {};
const paths_list = paths.split(',');
paths_list.forEach(path => {
const parts = path.split('/');
let current = tree;

parts.forEach((part, index) => {
if (!current[part]) {
if (index === parts.length - 1) {
current[part] = {};
} else {
current[part] = current[part] || {};
}
}
current = current[part];
});
});

return tree;
}

function buildTreeString(tree, indent = '') {
let treeString = '';

const keys = Object.keys(tree);
keys.forEach((key, index) => {
const isLast = index === keys.length - 1;
treeString += `${indent}${isLast ? '└── ' : '├── '}${key}\n`;
treeString += buildTreeString(tree[key], indent + (isLast ? ' ' : '│ '));
});

return treeString;
}

function generateDirectoryTree(filePaths) {
const tree = buildTree(filePaths);
return `.\n${buildTreeString(tree)}`;
}

export default ProjectDetailsPage;
107 changes: 107 additions & 0 deletions frontend/app/[locale]/components/general/RequiredFilesList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"use client"

import {IconButton, List, ListItem, ListItemText, TextField, Typography, Button} from "@mui/material";
import DeleteIcon from "@mui/icons-material/Delete";
import React, {useState} from "react";
import Box from "@mui/material/Box";

interface ItemsListProps {
items: string[],
setItems: (value: (((prevState: any[]) => any[]) | any[])) => void,
input_placeholder: string,
empty_list_placeholder:string,
button_text: string
}

const ItemsList = ({items, setItems, input_placeholder, empty_list_placeholder, button_text}: ItemsListProps) => {
const [newItem, setNewItem] = useState('')
const [noInput, setNoInput] = useState(false)

const handleDelete = (index: number) => {
const newFields = [...items];
newFields.splice(index, 1);
setItems(newFields);
}

const addNewFile = () => {
if (newItem !== '') {
const newItems = [...items];
newItems.push(newItem)
setItems(newItems);
setNewItem('');
setNoInput(false);
console.log(items);
} else {
setNoInput(true);
}
}

return (
<Box>
{items.length === 0 ? (
<Typography variant={"body1"} color={"text.disabled"} sx={{padding: 1}}>{empty_list_placeholder}</Typography>
) : (
<List
sx={{
width: '100%',
maxWidth: 360,
bgcolor: 'background.paper',
maxHeight: 150,
overflow: 'auto',
}}
>
{items.map((field, index) => (
<ListItem
key={index}
secondaryAction={
<IconButton
edge="end"
aria-label="delete"
onClick={() => handleDelete(index)}
>
<DeleteIcon />
</IconButton>
}
>
<ListItemText
primary={field}
/>
</ListItem>
))}
</List>
)
}
<Box
display={'flex'}
flexDirection={'row'}
justifyContent={'flex-start'}
>
<TextField
value={newItem}
onChange={(event) => setNewItem(event.target.value)}
variant="outlined"
size="small"
error={noInput}
placeholder={input_placeholder}
sx={{
width: 'fit-content',
}}
/>
<Button
onClick={() => addNewFile()}
variant={'contained'}
color={'secondary'}
sx={{
width: 'fit-content',
color: 'secondary.contrastText',
marginX: 1
}}
>
<Typography>{button_text}</Typography>
</Button>
</Box>
</Box>
);
}

export default ItemsList
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function ProjectEditForm({project_id, add_course_id}: ProjectEditFormProps) {
</Box>
<RemoveDialog
confirmRemove={confirmRemove}
handle_remove={handle_remove}
handleRemove={handle_remove}
setConfirmRemove={setConfirmRemove}/>
</div>
) : (
Expand Down

0 comments on commit d028afe

Please sign in to comment.