refactor(Dashboard api): changed api to fetch potential match

api now asks only the userID and fetches everything itself
This commit is contained in:
Laurian-Dufrechou
2023-05-02 22:30:08 +02:00
parent e4d89a0fa1
commit edcf469c29
4 changed files with 40 additions and 37 deletions
@@ -4,6 +4,7 @@ import {
EditablePreview,
FormControl,
FormErrorMessage,
FormHelperText,
FormLabel,
} from "@chakra-ui/react";
import { Controller, ControllerProps } from "react-hook-form";
@@ -11,6 +12,7 @@ import { Controller, ControllerProps } from "react-hook-form";
type CustomEditableProps = {
label: string;
isDisabled?: boolean;
helperText?: string;
} & Omit<ControllerProps, "render">;
export default function CustomEditable(props: CustomEditableProps) {
@@ -18,6 +20,7 @@ export default function CustomEditable(props: CustomEditableProps) {
defaultValue,
label,
name,
helperText = "",
isDisabled = false,
...controllerProps
} = props;
@@ -44,6 +47,7 @@ export default function CustomEditable(props: CustomEditableProps) {
<EditablePreview />
<EditableInput />
</Editable>
<FormHelperText>{helperText}</FormHelperText>
<FormErrorMessage as="b">{error?.message}</FormErrorMessage>
</FormControl>
);
+31 -27
View File
@@ -6,39 +6,43 @@ const birthDateFromAge = (age) => {
};
const get = async (req, res) => {
const {
preferences: preferencesList,
excludedId,
userLikes,
userDislikes,
} = req.query;
const { userID } = req.query;
const preferences = preferencesList.split(",");
const prefGender = preferences[0];
const dateAgeMin = birthDateFromAge(preferences[1]);
const dateAgeMax = birthDateFromAge(preferences[2]);
// pas utilisé pour le moment
const distance = preferences[3];
let userLikesList = userLikes.split(",");
let userDislikesList = userDislikes.split(",");
if (userLikesList[0] === "") {
userLikesList = [];
}
if (userDislikesList[0] === "") {
userDislikesList = [];
}
const excludedIdArray = [excludedId, ...userLikesList, ...userDislikesList];
const prisma = new PrismaClient();
const userInfo = await prisma.user
.findUnique({
where: {
id: userID,
},
select: {
UserLikesID: true,
UserDislikesID: true,
distance: true,
ageMax: true,
ageMin: true,
prefGender: true,
},
})
.catch((e) => {
return [];
});
const dateAgeMin = birthDateFromAge(userInfo.ageMin);
const dateAgeMax = birthDateFromAge(userInfo.ageMax);
const excludedIdArray = [
userID,
...userInfo.UserDislikesID,
...userInfo.UserLikesID,
];
// a terme mettre l'age, la distance
const users = await prisma.user
.findMany({
where: {
AND: [
{ gender: { equals: prefGender } },
{ gender: { equals: userInfo.prefGender } },
{
birthdate: {
lte: dateAgeMin.toISOString(),
@@ -51,7 +55,7 @@ const get = async (req, res) => {
},
})
.catch((e) => {
return [];
return null;
});
if (users.length === 0 || users === undefined || users === null) {
return res.status(500).send({ error: "Aucun profil trouvé" });
+1 -10
View File
@@ -58,16 +58,7 @@ export default function Dashboard() {
queryKey: ["ListUsers"],
enabled: status === "authenticated" && !isLoading,
queryFn: async () => {
return fetch(
`/api/user/userDashboard?preferences=${[
loggedUser.prefGender,
loggedUser.ageMin,
loggedUser.ageMax,
loggedUser.distance,
]}&excludedId=${loggedUser.id}&userLikes=${
loggedUser.UserLikesID
}&userDislikes=${loggedUser.UserDislikesID}`
) //exclure les profils déjà like ou dislike
return fetch(`/api/user/userDashboard?userID=${loggedUser.id}`) //exclure les profils déjà like ou dislike
.then((res) => res.json())
.catch((err) => {
return err;
+4
View File
@@ -10,6 +10,7 @@ import {
Container,
Divider,
Flex,
FormHelperText,
FormLabel,
Text,
useToast,
@@ -232,6 +233,9 @@ export default function UserProfile() {
}
control={control}
label={"Ville :"}
helperText={
"Ce champ est modifié automatiquement depuis la carte"
}
/>
</Box>
<Box>