Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stoneliuCS committed Nov 26, 2024
1 parent 1d69953 commit 3909764
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 130 deletions.
32 changes: 18 additions & 14 deletions backend/src/controllers/users/getUsers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import express from "express";
import { UserModel } from "../../models/users";
import { DEFAULT_LIMIT, DEFAULT_PAGE } from "../../consts/pagination";
import express from 'express';
import { UserModel } from '../../models/users';
import { DEFAULT_LIMIT, DEFAULT_PAGE } from '../../consts/pagination';

export default async function getUsersPaginated(req: express.Request, res: express.Response) {
export default async function getUsersPaginated(
req: express.Request,
res: express.Response,
) {
try {
const { text = "", page = DEFAULT_PAGE, limit = DEFAULT_LIMIT } = req.query;
const { text = '', page = DEFAULT_PAGE, limit = DEFAULT_LIMIT } = req.query;
const pageSize = parseInt(page as string);
const limitSize = parseInt(limit as string);

if (pageSize < 1 || limitSize < 1) {
return res.status(400).json({
error: "Invalid pagination parameters. Page and limit must be positive numbers.",
error:
'Invalid pagination parameters. Page and limit must be positive numbers.',
});
}

Expand All @@ -19,28 +23,28 @@ export default async function getUsersPaginated(req: express.Request, res: expre
const query = [
{
$search: {
index: "username",
index: 'username',
compound: {
should: [
{
text: {
query: text,
path: "username",
path: 'username',
fuzzy: {
maxEdits: 2,
prefixLength: 3,
maxEdits: 2,
prefixLength: 3,
},
score: {
boost: { value: 3 },
boost: { value: 3 },
},
},
},
{
autocomplete: {
query: text,
path: "username",
path: 'username',
fuzzy: {
maxEdits: 2,
maxEdits: 2,
},
score: {
boost: { value: 5 },
Expand All @@ -65,7 +69,7 @@ export default async function getUsersPaginated(req: express.Request, res: expre
} catch (error) {
console.error(error);
return res.status(500).json({
error: "An error occurred while fetching users.",
error: 'An error occurred while fetching users.',
});
}
}
2 changes: 1 addition & 1 deletion backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export default (): express.Router => {
user(router);
species(router);
swagger(router);
users(router)
users(router);
return router;
};
6 changes: 3 additions & 3 deletions backend/src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import express from 'express';
import { isAuthenticated } from '../middlewares/authMiddleware';
import getUsersPaginated from '../controllers/users/getUsers';

export default (router : express.Router) => {
router.get("/users", isAuthenticated, getUsersPaginated);
}
export default (router: express.Router) => {
router.get('/users', isAuthenticated, getUsersPaginated);
};
81 changes: 41 additions & 40 deletions frontend/app/(app)/(tabs)/explore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import { useState } from 'react';
import SearchResult from '../../../components/search-result';
import InfoPopup from '../../../components/info-popup';

type Toggle = "Fish" | "Users" | "Posts"
type Toggle = 'Fish' | 'Users' | 'Posts';

export default function Explore() {
const options: Toggle[] = ["Users", "Fish", "Posts"]
const [search, setSearch] = useState("");
const options: Toggle[] = ['Users', 'Fish', 'Posts'];
const [search, setSearch] = useState('');
const [toggle, setToggle] = useState<Toggle>(options[0]);
const [selected, setSelected] = useState<Toggle>(options[0]);

const changeText = (input: string) => {
setSearch(input);
}
};

/**
* Depending on what data comes from the endpoint, it could be formatted differently.
* This method ensures that no matter what data comes through, it will be an array.
* @param data
* @param data
* @returns an Array of the data
*/
const matchOnData = (data: any): any[] => {
Expand All @@ -32,9 +32,9 @@ export default function Explore() {
} else if (Array.isArray(data.results)) {
return data.results as any[];
} else {
throw new Error("Malformed Data")
throw new Error('Malformed Data');
}
}
};

/**
* On Query, will pattern match against the toggle options which will
Expand All @@ -45,20 +45,20 @@ export default function Explore() {
if (!search) return null;
let endpoint;
switch (toggle) {
case 'Fish': endpoint = `/species/search/${search}`; break;
default: endpoint = `/users?text=${search}`;
case 'Fish':
endpoint = `/species/search/${search}`;
break;
default:
endpoint = `/users?text=${search}`;
}
const res = await fetchData(
endpoint,
'Failed to fetch users',
);
const res = await fetchData(endpoint, 'Failed to fetch users');
return res;
}
};

const { isPending, error, data } = useQuery({
queryKey: ["search", search],
queryKey: ['search', search],
queryFn: () => onQueryFunction(),
enabled: search.length > 0
enabled: search.length > 0,
});

const values = matchOnData(data);
Expand All @@ -70,10 +70,10 @@ export default function Explore() {
*/
const ToggleButtons = () => {
const onSelected = (selection: Toggle) => {
setToggle(selection)
setSelected(selection)
setSearch("")
}
setToggle(selection);
setSelected(selection);
setSearch('');
};
return (
<View className="w-full flex justify-between flex-row w-96">
{options.map((option, key) => (
Expand All @@ -83,8 +83,7 @@ export default function Explore() {
className="flex items-center"
>
<Text
className={`text-xl ${selected === option ? "underline" : ""
}`}
className={`text-xl ${selected === option ? 'underline' : ''}`}
>
{option}
</Text>
Expand All @@ -96,30 +95,32 @@ export default function Explore() {

if (error) {
return (
<View className='h-screen w-screen'>
<Text>
Error occurred {error.message}
</Text>
<View className="h-screen w-screen">
<Text>Error occurred {error.message}</Text>
</View>
)
);
}

return (
<View className="h-screen w-screen flex items-center">
<View className='w-96 pt-20 pb-4'>
<Input border='black' onChangeText={changeText} value={search} />
<View className="w-96 pt-20 pb-4">
<Input border="black" onChangeText={changeText} value={search} />
<ToggleButtons />
</View>
{isPending ? <Text> Nothing to see here... </Text> : (values.length > 0) &&
<ScrollView className="w-96">
{values.map((d: any) =>
<View className='mb-4' key={d._id}>
<SearchResult {...d} />
</View>)}
</ScrollView>
}
<InfoPopup/>
{isPending ? (
<Text> Nothing to see here... </Text>
) : (
values.length > 0 && (
<ScrollView className="w-96">
{values.map((d: any) => (
<View className="mb-4" key={d._id}>
<SearchResult {...d} />
</View>
))}
</ScrollView>
)
)}
<InfoPopup />
</View>
);
};

}
Loading

0 comments on commit 3909764

Please sign in to comment.