Skip to content

Commit

Permalink
Merge from main and sorted test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
epadams committed Mar 31, 2024
2 parents 35aebb5 + c3bc51b commit 6c26c82
Show file tree
Hide file tree
Showing 11 changed files with 1,385 additions and 43 deletions.
1 change: 1 addition & 0 deletions FU.API/FU.API/Helpers/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static UserQuery ToUserQuery(this UserSearchRequestDTO dto)
query.SortType = arr[0] switch
{
"username" => UserSortType.Username,
"dob" => UserSortType.DOB,
"chatactivity" => UserSortType.ChatActivity,
_ => UserSortType.Username,
};
Expand Down
1 change: 1 addition & 0 deletions FU.API/FU.API/Models/Sorting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace FU.API.Models;
public enum UserSortType
{
Username,
DOB,
ChatActivity,
}

Expand Down
3 changes: 3 additions & 0 deletions FU.API/FU.API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ When users navigate to a pages with a chat, they will be connected the chat grou
return Created(response);
}
}

## Understanding Services
Services usually interact directly with the database and use LINQ queries to sort, remove, and insert data into the database. To get a better and more complete understanding of how services work, look at the `FU.API.Tests` directory and look at the unit test.
4 changes: 4 additions & 0 deletions FU.API/FU.API/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class SearchService : CommonService, ISearchService
{
private static readonly DateTime DefaultTime = DateTime.MinValue;

private static readonly DateOnly DefaultDate = new DateOnly(1, 1, 1);

private readonly AppDbContext _dbContext;

public SearchService(AppDbContext dbContext)
Expand Down Expand Up @@ -127,6 +129,7 @@ private static Expression<Func<ApplicationUser, object>> SelectUserProperty(User
return sortType switch
{
UserSortType.Username => (user) => user.NormalizedUsername,
UserSortType.DOB => (user) => user.DOB ?? DefaultDate,
_ => (user) => user.NormalizedUsername,
};
}
Expand All @@ -137,6 +140,7 @@ private static Expression<Func<UserRelation, object>> SelectConnectedUserPropert
{
UserSortType.Username => (ur) => ur.User2.NormalizedUsername,
UserSortType.ChatActivity => (ur) => ur.Chat.LastMessageAt ?? DefaultTime,
UserSortType.DOB => (ur) => ur.User2.DOB ?? DefaultDate,
_ => (ur) => ur.User2.NormalizedUsername,
};
}
Expand Down
2 changes: 1 addition & 1 deletion FU.SPA/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "npx vite preview",
"format": "npx prettier . --write",
"format-check": "npx prettier . --check",
"selenium-test": "npx selenium-side-runner tests/*.side -c browserName=firefox"
"selenium-test": "npx selenium-side-runner tests/Forces_Unite.side -c browserName=firefox"
},
"dependencies": {
"@emotion/react": "^11.11.1",
Expand Down
78 changes: 63 additions & 15 deletions FU.SPA/src/components/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,24 @@ export default function Edit({ postId }) {
const [game, setGame] = useState();
const [title, setTitle] = useState('');
const [startTime, setStartTime] = useState(dayjs());
const [endTime, setEndTime] = useState(dayjs().add(30, 'minute'));
const [endTime, setEndTime] = useState(dayjs().add(5, 'minute'));
const [description, setDescription] = useState('');
const [tags, setTags] = useState([]);
const navigate = useNavigate();
const [postsDetails, setPostsDetails] = useState('');

const { user } = useContext(UserContext);

useEffect(() => {
const init = async () => {
try {
const postDetails = await PostService.getPostDetails(postId);
if (user.id !== postDetails.creator.id) {
const ogPostDetails = await PostService.getPostDetails(postId);
setPostsDetails(ogPostDetails);
setTitle(ogPostDetails.title);
setDescription(ogPostDetails.description);
setStartTime(dayjs(ogPostDetails.startTime));
setEndTime(dayjs(ogPostDetails.endTime));
if (user.id !== ogPostDetails.creator.id) {
alert('You are not authorized to edit this post');
navigate(`/discover`);
}
Expand All @@ -47,7 +53,7 @@ export default function Edit({ postId }) {
};

init();
});
}, [postId, user, navigate]);

const handleSubmit = async (e) => {
// change to get post state, autofill fields based on info
Expand Down Expand Up @@ -124,7 +130,12 @@ export default function Edit({ postId }) {
onChange={(e) => setTitle(e.target.value)}
/>
<Grid item xs={12}>
<GameSelector onChange={setGame} />
{postsDetails.game !== undefined && (
<GameSelector
initialValue={postsDetails.game}
onChange={setGame}
/>
)}
</Grid>
<br />
<LocalizationProvider dateAdapter={AdapterDayjs}>
Expand All @@ -139,7 +150,12 @@ export default function Edit({ postId }) {
onChange={(newValue) => setEndTime(newValue)}
/>
</LocalizationProvider>
<TagsSelector onChange={setTags} />
{postsDetails.tags !== undefined && (
<TagsSelector
initialValues={postsDetails.tags}
onChange={setTags}
/>
)}
<Box
sx={{
display: 'flex',
Expand Down Expand Up @@ -175,13 +191,27 @@ const checkboxIconBlank = <CheckBoxOutlineBlankIcon fontSize="small" />;
const checkboxIconChecked = <CheckBoxIcon fontSize="small" />;
const filter = createFilterOptions();

const GameSelector = ({ onChange }) => {
const [gammeOptions, setGameOptions] = useState([]);
const GameSelector = ({ onChange, initialValue }) => {
const [gameOptions, setGameOptions] = useState([]);
const [value, setValue] = useState('');

useEffect(() => {
GameService.searchGames('').then((games) => setGameOptions(games));
}, []);
const getGames = async () => {
try {
const game = await GameService.searchGames('');
setGameOptions(game);
const gameChoice = game.find((g) => g.name === initialValue);
if (gameChoice) {
setValue(gameChoice);
onChange(gameChoice);
}
} catch (e) {
console.error('Problem getting games', e);
}
};

getGames();
}, [initialValue, onChange]);

const onInputChange = (event, newValue) => {
setValue(newValue);
Expand All @@ -207,18 +237,19 @@ const GameSelector = ({ onChange }) => {

return (
<Autocomplete
autoHighlight
clearOnBlur
value={value}
onChange={onInputChange}
options={gammeOptions}
disableCloseOnSelect
options={gameOptions}
filterOptions={onFilterOptions}
getOptionLabel={(o) => (o ? o.name : '')}
isOptionEqualToValue={(option, value) => option.name === value.name}
renderOption={(props, option) => <li {...props}>{option.name}</li>}
renderInput={(params) => (
<TextField
{...params}
autoHighlight
label="Game"
required
placeholder="Select or create a game"
Expand All @@ -228,13 +259,29 @@ const GameSelector = ({ onChange }) => {
);
};

const TagsSelector = ({ onChange }) => {
const TagsSelector = ({ onChange, initialValues }) => {
const [tagOptions, setTagOptions] = useState([]);
const [value, setValue] = useState([]);

useEffect(() => {
TagService.searchTags('').then((tags) => setTagOptions(tags));
}, []);
const getTags = async () => {
try {
const tags = await TagService.searchTags('');
setTagOptions(tags);
//if tags aren't null and isn't empty string.
if (initialValues && initialValues.length > 0) {
const initialTags = tags.filter((tag) =>
initialValues.includes(tag.name),
);
setValue(initialTags);
onChange(initialTags);
}
} catch (e) {
console.error('Something went wrong getting tags: ', e);
}
};
getTags();
}, [initialValues, onChange]);

const onInputChange = (event, newValues) => {
for (const newValue of newValues) {
Expand Down Expand Up @@ -270,6 +317,7 @@ const TagsSelector = ({ onChange }) => {

return (
<Autocomplete
autoHighlight
multiple
clearOnBlur
value={value}
Expand Down
45 changes: 39 additions & 6 deletions FU.SPA/src/components/pages/CreatePost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import {
Button,
TextField,
Box,
Container,
Typography,
Grid,
Checkbox,
Autocomplete,
createFilterOptions,
} from '@mui/material';
import { useEffect, useState } from 'react';
import { useEffect, useState, useContext } from 'react';
import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank';
import CheckBoxIcon from '@mui/icons-material/CheckBox';
import PostService from '../../services/postService';
Expand All @@ -20,8 +19,11 @@ import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import dayjs from 'dayjs';
import { useNavigate } from 'react-router-dom';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import PostCard from '../PostCard';
import UserContext from '../../context/userContext';

export default function CreatePost() {
const { user } = useContext(UserContext);
const [game, setGame] = useState(null);
const [title, setTitle] = useState('');
const [startTime, setStartTime] = useState(dayjs().add(30, 'minute'));
Expand Down Expand Up @@ -60,15 +62,46 @@ export default function CreatePost() {
}
};

const getTagsForPreview = () => {
if (tags.length === 0) {
return ['Tag1', 'Tag2'];
}

return tags.map((tag) => tag.name);
};

return (
<Container component="main" maxWidth="xs">
<div
style={{
display: 'flex',
justifyContent: 'center',
gap: '30px',
paddingRight: '10%',
}}
>
<div style={{ alignContent: 'center' }}>
<PostCard
post={{
creator: user,
game: game?.name ?? 'Game Name',
title: title.length > 0 ? title : 'Post Title',
description:
description.length > 0 ? description : 'Post Description',
startTime: startTime,
endTime: endTime,
tags: getTagsForPreview(),
}}
showActions={false}
/>
</div>
<Box
sx={{
marginTop: 1,
m: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
width: 'fit-content',
}}
>
<Typography component="h1" variant="h5">
Expand All @@ -89,10 +122,10 @@ export default function CreatePost() {
}}
>
<TextField
required //may want to get rid of this and just check if it's empty when clicking create button.
required
fullWidth
id="searchGames"
label="Title" //might want to put a Search icon in front, if we can figure it out.
label="Title"
autoFocus
value={title}
onChange={(e) => setTitle(e.target.value)}
Expand Down Expand Up @@ -142,7 +175,7 @@ export default function CreatePost() {
</Button>
</Box>
</Box>
</Container>
</div>
);
}

Expand Down
Loading

0 comments on commit 6c26c82

Please sign in to comment.