mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
Feature/tinder (#179)
* basic framework for match/tinder * working version of swiping * working version of profile editing * introducing backend * working matching system * working profile updating * fix all linting issues * updated events to Enum * working profile generation * documentation updates * general PR feedback * working profile loading, matches show matchedAt * slight modification to event strings * added sql imports
This commit is contained in:
46 files changed
+1932
-49
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const matchState = {
|
||||
profiles: atom({
|
||||
key: 'profiles',
|
||||
default: null,
|
||||
}),
|
||||
errorLoadingProfiles: atom({
|
||||
key: 'errorLoadingProfiles',
|
||||
default: false,
|
||||
}),
|
||||
matches: atom({
|
||||
key: 'matches',
|
||||
default: null,
|
||||
}),
|
||||
errorLoadingMatches: atom({
|
||||
key: 'errorLoadingMatches',
|
||||
default: false,
|
||||
}),
|
||||
myProfile: atom({
|
||||
key: 'myProfile',
|
||||
default: null,
|
||||
}),
|
||||
noProfileExists: atom({
|
||||
key: 'noProfileExists',
|
||||
default: false,
|
||||
}),
|
||||
};
|
||||
@@ -0,0 +1,86 @@
|
||||
import { useSetRecoilState } from 'recoil';
|
||||
import { matchState } from './state';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
|
||||
import { Profile, FormattedProfile, Match, MatchEvents } from '../../../../../typings/match';
|
||||
import { IAlert, useSnackbar } from '../../../ui/hooks/useSnackbar';
|
||||
|
||||
export const useMatchService = () => {
|
||||
const { addAlert } = useSnackbar();
|
||||
const { t } = useTranslation();
|
||||
const setNoProfileExists = useSetRecoilState(matchState.noProfileExists);
|
||||
const setProfiles = useSetRecoilState(matchState.profiles);
|
||||
const setProfile = useSetRecoilState(matchState.myProfile);
|
||||
const setMatches = useSetRecoilState(matchState.matches);
|
||||
const setErrorLoadingMatches = useSetRecoilState(matchState.errorLoadingMatches);
|
||||
const setErrorLoadingProfiles = useSetRecoilState(matchState.errorLoadingProfiles);
|
||||
|
||||
const handleAddAlert = ({ message, type }: IAlert) => {
|
||||
addAlert({
|
||||
message: t(message),
|
||||
type,
|
||||
});
|
||||
};
|
||||
|
||||
function formatProfile(profile: Profile): FormattedProfile | null {
|
||||
return {
|
||||
...profile,
|
||||
tagList: profile.tags.split(',').filter((t) => t), // remove any empty tags
|
||||
lastActiveFormatted: dayjs.unix(profile.lastActive).format(t('DATE_TIME_FORMAT')),
|
||||
viewed: false,
|
||||
};
|
||||
}
|
||||
|
||||
const _setProfiles = (profiles: Profile[]): void => {
|
||||
setProfiles(profiles.map(formatProfile));
|
||||
};
|
||||
const _setProfile = (profile: Profile) => {
|
||||
if (!profile) {
|
||||
setNoProfileExists(true);
|
||||
} else {
|
||||
setNoProfileExists(false);
|
||||
setProfile(formatProfile(profile));
|
||||
}
|
||||
};
|
||||
const _handleGetProfilesFailed = (alert: IAlert): void => {
|
||||
handleAddAlert(alert);
|
||||
setErrorLoadingProfiles(true);
|
||||
};
|
||||
|
||||
const _setMatches = (matches: Match[]): void => {
|
||||
setMatches(
|
||||
matches.map((match) => ({
|
||||
...match,
|
||||
tagList: match.tags.split(',').filter((t) => t), // remove any empty tags
|
||||
lastActiveFormatted: dayjs.unix(match.lastActive).format(t('DATE_TIME_FORMAT')),
|
||||
matchedAtFormatted: dayjs.unix(match.matchedAt).format(t('DATE_TIME_FORMAT')),
|
||||
})),
|
||||
);
|
||||
};
|
||||
|
||||
const _handleGetMatchesFailed = (alert: IAlert): void => {
|
||||
handleAddAlert(alert);
|
||||
setErrorLoadingMatches(true);
|
||||
};
|
||||
|
||||
const _handleUpdateProfile = (profile: Profile) => {
|
||||
_setProfile(profile);
|
||||
handleAddAlert({ message: 'APPS_MATCH_UPDATE_PROFILE_SUCCEEDED', type: 'info' });
|
||||
};
|
||||
|
||||
useNuiEvent('MATCH', MatchEvents.GET_PROFILES_SUCCESS, _setProfiles);
|
||||
useNuiEvent('MATCH', MatchEvents.GET_PROFILES_FAILED, _handleGetProfilesFailed);
|
||||
useNuiEvent('MATCH', MatchEvents.GET_MY_PROFILE_SUCCESS, _setProfile);
|
||||
useNuiEvent('MATCH', MatchEvents.GET_MY_PROFILE_FAILED, handleAddAlert);
|
||||
useNuiEvent('MATCH', MatchEvents.GET_MATCHES_SUCCESS, _setMatches);
|
||||
useNuiEvent('MATCH', MatchEvents.GET_MATCHES_FAILED, _handleGetMatchesFailed);
|
||||
useNuiEvent('MATCH', MatchEvents.SAVE_LIKES_FAILED, handleAddAlert);
|
||||
useNuiEvent('MATCH', MatchEvents.NEW_MATCH, handleAddAlert);
|
||||
useNuiEvent('MATCH', MatchEvents.UPDATE_MY_PROFILE_SUCCESS, _handleUpdateProfile);
|
||||
useNuiEvent('MATCH', MatchEvents.UPDATE_MY_PROFILE_FAILED, handleAddAlert);
|
||||
useNuiEvent('MATCH', MatchEvents.CREATE_MY_PROFILE_SUCCESS, _handleUpdateProfile);
|
||||
useNuiEvent('MATCH', MatchEvents.CREATE_MY_PROFILE_FAILED, handleAddAlert);
|
||||
return {};
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { matchState } from './state';
|
||||
|
||||
import { FormattedMatch } from '../../../../../typings/match';
|
||||
|
||||
interface IUseMatches {
|
||||
matches: FormattedMatch[];
|
||||
error: boolean;
|
||||
}
|
||||
|
||||
export const useMatches = (): IUseMatches => {
|
||||
const matches = useRecoilValue<FormattedMatch[]>(matchState.matches);
|
||||
const error = useRecoilValue(matchState.errorLoadingMatches);
|
||||
return { matches, error };
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||
import { matchState } from './state';
|
||||
|
||||
import { FormattedProfile } from '../../../../../typings/match';
|
||||
|
||||
interface IUseProfiles {
|
||||
profile: FormattedProfile;
|
||||
setProfile: (profile: FormattedProfile) => void;
|
||||
noProfileExists: boolean;
|
||||
}
|
||||
|
||||
export const useProfile = (): IUseProfiles => {
|
||||
const [profile, setProfile] = useRecoilState<FormattedProfile>(matchState.myProfile);
|
||||
const noProfileExists = useRecoilValue(matchState.noProfileExists);
|
||||
return { profile, setProfile, noProfileExists };
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||
import { matchState } from './state';
|
||||
|
||||
import { FormattedProfile, MatchEvents } from '../../../../../typings/match';
|
||||
import Nui from '../../../os/nui-events/utils/Nui';
|
||||
|
||||
interface IUseProfiles {
|
||||
profiles: FormattedProfile[];
|
||||
activeProfile: FormattedProfile | null;
|
||||
setViewed: (index: number, liked: boolean) => void;
|
||||
error: boolean;
|
||||
}
|
||||
|
||||
export const useProfiles = (): IUseProfiles => {
|
||||
const [profiles, setProfiles] = useRecoilState(matchState.profiles);
|
||||
const error = useRecoilValue(matchState.errorLoadingProfiles);
|
||||
|
||||
const setViewed = (id: number, liked: boolean) => {
|
||||
setProfiles((profiles) =>
|
||||
profiles.map((profile) => {
|
||||
if (id === profile.id) return { ...profile, viewed: true, liked };
|
||||
return profile;
|
||||
}),
|
||||
);
|
||||
|
||||
Nui.send(MatchEvents.SAVE_LIKES, [{ id, liked }]);
|
||||
};
|
||||
|
||||
const filteredProfiles = profiles ? profiles.filter((profile) => !profile.viewed) : null;
|
||||
const activeProfile = profiles ? filteredProfiles[0] : null;
|
||||
|
||||
return { profiles, activeProfile, error, setViewed };
|
||||
};
|
||||
Reference in new issue
Block a user