mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 06:28:51 +00:00
* 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
87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
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 {};
|
|
};
|