mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
Force guest user (#210)
This commit is contained in:
parent
000d105983
commit
1d768083ee
17 changed files with 111 additions and 146 deletions
|
@ -110,11 +110,7 @@ export const PollContextProvider: React.VoidFunctionComponent<{
|
|||
|
||||
const userAlreadyVoted =
|
||||
user && participants
|
||||
? participants.some((participant) =>
|
||||
user.isGuest
|
||||
? participant.guestId === user.id
|
||||
: participant.userId === user.id,
|
||||
)
|
||||
? participants.some((participant) => participant.userId === user.id)
|
||||
: false;
|
||||
|
||||
const optionIds = parsedOptions.options.map(({ optionId }) => optionId);
|
||||
|
|
|
@ -113,9 +113,9 @@ const ParticipantRow: React.VoidFunctionComponent<ParticipantRowProps> = ({
|
|||
|
||||
const isYou = session.user && session.ownsObject(participant) ? true : false;
|
||||
|
||||
const isAnonymous = !participant.userId && !participant.guestId;
|
||||
const isUnclaimed = !participant.userId;
|
||||
|
||||
const canEdit = !poll.closed && (poll.admin || isYou || isAnonymous);
|
||||
const canEdit = !poll.closed && (poll.admin || isYou || isUnclaimed);
|
||||
|
||||
if (editMode) {
|
||||
return (
|
||||
|
|
|
@ -69,10 +69,8 @@ const MobilePoll: React.VoidFunctionComponent = () => {
|
|||
}
|
||||
const { user } = session;
|
||||
if (user) {
|
||||
const userParticipant = participants.find((participant) =>
|
||||
user.isGuest
|
||||
? participant.guestId === user.id
|
||||
: participant.userId === user.id,
|
||||
const userParticipant = participants.find(
|
||||
(participant) => participant.userId === user.id,
|
||||
);
|
||||
return userParticipant?.id;
|
||||
}
|
||||
|
|
|
@ -9,12 +9,11 @@ import { useRequiredContext } from "./use-required-context";
|
|||
export type UserSessionData = NonNullable<IronSessionData["user"]>;
|
||||
|
||||
export type SessionProps = {
|
||||
user: UserSessionData | null;
|
||||
user: UserSessionData;
|
||||
};
|
||||
|
||||
type ParticipantOrComment = {
|
||||
userId: string | null;
|
||||
guestId: string | null;
|
||||
};
|
||||
|
||||
export type UserSessionDataExtended = UserSessionData & {
|
||||
|
@ -36,35 +35,33 @@ SessionContext.displayName = "SessionContext";
|
|||
|
||||
export const SessionProvider: React.VoidFunctionComponent<{
|
||||
children?: React.ReactNode;
|
||||
session: UserSessionData | null;
|
||||
}> = ({ children, session }) => {
|
||||
defaultUser: UserSessionData;
|
||||
}> = ({ children, defaultUser }) => {
|
||||
const queryClient = trpc.useContext();
|
||||
const {
|
||||
data: user = session,
|
||||
data: user = defaultUser,
|
||||
refetch,
|
||||
isLoading,
|
||||
} = trpc.useQuery(["session.get"]);
|
||||
|
||||
const logout = trpc.useMutation(["session.destroy"], {
|
||||
onSuccess: () => {
|
||||
queryClient.setQueryData(["session.get"], null);
|
||||
queryClient.invalidateQueries(["session.get"]);
|
||||
},
|
||||
});
|
||||
|
||||
const sessionData: SessionContextValue = {
|
||||
user: user
|
||||
? {
|
||||
...user,
|
||||
shortName:
|
||||
// try to get the first name in the event
|
||||
// that the user entered a full name
|
||||
user.isGuest
|
||||
? user.id.substring(0, 12)
|
||||
: user.name.length > 12 && user.name.indexOf(" ") !== -1
|
||||
? user.name.substring(0, user.name.indexOf(" "))
|
||||
: user.name,
|
||||
}
|
||||
: null,
|
||||
user: {
|
||||
...user,
|
||||
shortName:
|
||||
// try to get the first name in the event
|
||||
// that the user entered a full name
|
||||
user.isGuest
|
||||
? user.id.substring(0, 10)
|
||||
: user.name.length > 12 && user.name.indexOf(" ") !== -1
|
||||
? user.name.substring(0, user.name.indexOf(" "))
|
||||
: user.name,
|
||||
},
|
||||
refresh: () => {
|
||||
refetch();
|
||||
},
|
||||
|
@ -77,14 +74,6 @@ export const SessionProvider: React.VoidFunctionComponent<{
|
|||
});
|
||||
},
|
||||
ownsObject: (obj) => {
|
||||
if (!user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user.isGuest) {
|
||||
return obj.guestId === user.id;
|
||||
}
|
||||
|
||||
return obj.userId === user.id;
|
||||
},
|
||||
};
|
||||
|
@ -106,7 +95,7 @@ export const withSession = <P extends SessionProps>(
|
|||
const ComposedComponent: React.VoidFunctionComponent<P> = (props: P) => {
|
||||
const Component = component;
|
||||
return (
|
||||
<SessionProvider session={props.user}>
|
||||
<SessionProvider defaultUser={props.user}>
|
||||
<Component {...props} />
|
||||
</SessionProvider>
|
||||
);
|
||||
|
@ -115,5 +104,4 @@ export const withSession = <P extends SessionProps>(
|
|||
return ComposedComponent;
|
||||
};
|
||||
|
||||
export const isUnclaimed = (obj: ParticipantOrComment) =>
|
||||
!obj.guestId && !obj.userId;
|
||||
export const isUnclaimed = (obj: ParticipantOrComment) => !obj.userId;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue