([]);
+
+ // load available user pool on mount
+ useEffect(() => {
+ if (!token) return;
+ (async function() {
+ const users = new API.User(token);
+ const result = await users.getAll();
+ if (result) setUserPool(result);
+ })();
+
+ (async function() {
+ const friends = new API.Friendship(token);
+ const result = await friends.getAll();
+ setFriendResults(result);
+ })();
+ }, [])
+
+ useEffect(() => {
+ console.log(searchTerm);
+ searchTerm && setUserPool((prev) => {
+ const newPool = prev.filter(person => {
+ if (person.firstname.toLowerCase().includes(searchTerm) || person.lastname.toLowerCase().includes(searchTerm) || person.handle.toLowerCase().includes(searchTerm)) return person;
+ })
+
+ return newPool;
+ })
+ }, [searchTerm])
+
+ useEffect(() => {
+ console.log(userPool);
+ }, [userPool])
+
+ return (
+
+ ) => setSearchTerm(e.target.value.toLowerCase())} placeholder={'Search'} />
+
+ {
+ userPool.map((friend: IUser) => {
+ return {}} />
+ })
+ }
+
+ )
+}
+
+export default FriendSearchWidget;
\ No newline at end of file
diff --git a/client/src/util/API.ts b/client/src/util/API.ts
index 69b4ed2..1b5da16 100644
--- a/client/src/util/API.ts
+++ b/client/src/util/API.ts
@@ -1,4 +1,4 @@
-import { AxiosHeaders, AxiosRequestHeaders, AxiosResponse } from "axios";
+import { AxiosError, AxiosHeaders, AxiosRequestHeaders, AxiosResponse } from "axios";
import { IUser, IUserAuth, IFriendship, IRecipe, IIngredient, ICollection, IGroceryList } from "../schemas";
import { default as _instance } from "./axiosInstance";
@@ -138,13 +138,31 @@ module API {
export class Friendship extends RestController {
constructor(token: string) {
- super(Settings.getAPISTRING() + "/app/friends", token);
+ super(Settings.getAPISTRING() + "/app/friend", token);
+ }
+
+ override async getAll() {
+ try {
+ const response = await this.instance.get(this.endpoint, this.headers);
+ return Promise.resolve(response.data);
+ } catch(e) {
+ const error = e as AxiosError;
+ if (error.response?.status == 404) {
+ console.log('no friends found');
+ return [];
+ }
+ }
}
async getPendingFriendRequests() {
const response = await this.instance.get(this.endpoint + "?pending=true", this.headers);
return Promise.resolve(response.data);
}
+
+ async addFriend(id: string | number) {
+ const response = await this.instance.post(this.endpoint + `/${id}`, this.headers);
+ return Promise.resolve(response.data);
+ }
}
export class Recipe extends RestController {
diff --git a/server/routes/friend.ts b/server/routes/friend.ts
index 42037a0..08561b9 100644
--- a/server/routes/friend.ts
+++ b/server/routes/friend.ts
@@ -9,19 +9,8 @@ const router = Router();
export const friendRouter = (app: Express) => {
app.use('/app/friend', router);
- router.use((req, res, next) => {
- let test = req.session.user;
-
- if (req.session.user == undefined) {
- throw new Error("No session found");
- } else {
- const narrowed = req.session.user;
- next();
- }
- })
-
router.post('/:targetid', restrictAccess, async (req, res, next) => {
- const user = req.session.user as IUser;
+ const user = req.user as IUser;
const { targetid } = req.params;
try {
@@ -34,7 +23,7 @@ export const friendRouter = (app: Express) => {
// get all friendships for a user
router.get('/', async (req, res, next) => {
- const user = req.session.user as IUser;
+ const user = req.user as IUser;
const { pending } = req.query;
try {
@@ -53,7 +42,7 @@ export const friendRouter = (app: Express) => {
// get one friendship by its id
router.get('/:id', async (req, res, next) => {
const { id } = req.params;
- const user = req.session.user as IUser;
+ const user = req.user as IUser;
try {
const { code, data } = await UserInstance.getFriendshipByID(id, user.id as number);
@@ -76,7 +65,7 @@ export const friendRouter = (app: Express) => {
router.put('/:id', async (req, res, next) => {
const data = req.body;
const { id } = req.params;
- const user = req.session.user as IUser;
+ const user = req.user as IUser;
try {
const response = await UserInstance.updateFriendship(id, user.id as number, data);
@@ -85,4 +74,6 @@ export const friendRouter = (app: Express) => {
next(e);
}
})
+
+ return router;
}
\ No newline at end of file