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

SPA Discover users sorting #436

Merged
merged 8 commits into from
Mar 31, 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
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
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 @@
{
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 @@
return sortType switch
{
UserSortType.Username => (user) => user.NormalizedUsername,
UserSortType.DOB => (user) => user.DOB ?? DefaultDate,
_ => (user) => user.NormalizedUsername,
};
}
Expand All @@ -137,6 +140,7 @@
{
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 All @@ -160,7 +164,7 @@
IQueryable<UserRelation> dbQuery = _dbContext.UserRelations
.Where(ur => ur.User1Id == query.UserId && ur.Status == query.RelationStatus)
.Include(ur => ur.User2)
.Include(ur => ur.Chat).ThenInclude(c => c.LastMessage).ThenInclude(m => m.Sender);

Check warning on line 167 in FU.API/FU.API/Services/SearchService.cs

View workflow job for this annotation

GitHub Actions / build-api

Dereference of a possibly null reference.

Check warning on line 167 in FU.API/FU.API/Services/SearchService.cs

View workflow job for this annotation

GitHub Actions / build-api

Dereference of a possibly null reference.

dbQuery = dbQuery.Where(ConnectedUserContainsKeywords(query.Keywords));

Expand All @@ -176,7 +180,7 @@
{
dbQuery = _dbContext.Posts
.Where(p => p.Chat.Members.Any(m => m.UserId == query.UserId))
.Include(p => p.Chat).ThenInclude(c => c.LastMessage).ThenInclude(m => m.Sender)

Check warning on line 183 in FU.API/FU.API/Services/SearchService.cs

View workflow job for this annotation

GitHub Actions / build-api

Dereference of a possibly null reference.

Check warning on line 183 in FU.API/FU.API/Services/SearchService.cs

View workflow job for this annotation

GitHub Actions / build-api

Dereference of a possibly null reference.
.Select(p => p);
}
else
Expand Down
50 changes: 34 additions & 16 deletions FU.SPA/src/components/pages/Discover.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export default function Discover() {
);
const [gameOptions, setGameOptions] = useState([]);
const [tagOptions, setTagOptions] = useState([]);
const [sortOption, setSortOption] = useState(null);

const [postSortOption, setPostSortOption] = useState(null);
const [userSortOption, setUserSortOption] = useState(null);

const [dateRangeRadioValue, setDateRangeRadioValue] = useState(() => {
const paramValue = searchParams.get(paramKey.dateRadio);
Expand Down Expand Up @@ -181,7 +183,7 @@ export default function Discover() {
tags: tags,
limit: queryLimit,
page: page,
sort: sortOption,
sort: postSortOption,
};

if (startDate) query.startDate = startDate;
Expand Down Expand Up @@ -213,16 +215,20 @@ export default function Discover() {
}
}

// Search and set posts
const response = await SearchService.searchPosts(query);
setPosts(response.data);
setTotalResults(response.totalCount);
} else {
// on User tab
const query = {
keywords: searchText,
limit: queryLimit,
page: page,
sort: userSortOption,
};

// Search and set users
const response = await SearchService.searchUsers(query);
setPlayers(response.data);
setTotalResults(response.totalCount);
Expand All @@ -249,7 +255,8 @@ export default function Discover() {
startTime,
endTime,
tabOption,
sortOption,
postSortOption,
userSortOption,
]);

useEffect(() => {
Expand Down Expand Up @@ -306,18 +313,28 @@ export default function Discover() {
}
};

const renderSortSelector = () => {
if (tabOption === tabOptions.Posts) {
return (
<SortOptionsSelector
options={config.POST_SORT_OPTIONS}
onChange={(newValue) => {
setSortOption(newValue);
setPage(1);
}}
/>
);
}
const renderPostSortSelector = () => {
return (
<SortOptionsSelector
options={config.POST_SORT_OPTIONS}
onChange={(newValue) => {
setPostSortOption(newValue);
setPage(1);
}}
/>
);
};

const renderUserSortSelector = () => {
return (
<SortOptionsSelector
options={config.USER_SORT_OPTIONS}
onChange={(newValue) => {
setUserSortOption(newValue);
setPage(1);
}}
/>
);
};

const renderTabSelectors = () => {
Expand Down Expand Up @@ -419,7 +436,8 @@ export default function Discover() {
searchText={searchText}
onSearchSubmit={setSearchText}
/>
{renderSortSelector()}
{tabOption === tabOptions.Posts && renderPostSortSelector()}
{tabOption === tabOptions.Users && renderUserSortSelector()}
</div>
{renderTabContent()}
<div
Expand Down
5 changes: 4 additions & 1 deletion FU.SPA/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const SOCIAL_POST_SORT_OPTIONS = [
{ value: 'chatactivity', label: 'Chat Activity' },
];

const USER_SORT_OPTIONS = [{ value: 'username', label: 'Name' }];
const USER_SORT_OPTIONS = [
{ value: 'username', label: 'Name' },
{ value: 'dob', label: 'DOB' },
];

const SOCIAL_USER_SORT_OPTIONS = [
...USER_SORT_OPTIONS,
Expand Down
2 changes: 2 additions & 0 deletions FU.SPA/src/helpers/requestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const buildPostQueryString = (query) => {
queryString += '&limit=' + query.limit;
}

// Sort option
if (query.sort) {
queryString += '&sort=' + query.sort;
}
Expand All @@ -67,6 +68,7 @@ const buildUserQueryString = (query) => {
queryString += '&limit=' + query.limit;
}

// Sort option
if (query.sort) {
queryString += '&sort=' + query.sort;
}
Expand Down
Loading