Force guest user (#210)

This commit is contained in:
Luke Vella 2022-06-28 10:33:17 +01:00 committed by GitHub
parent 000d105983
commit 1d768083ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 111 additions and 146 deletions

View file

@ -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);

View file

@ -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 (

View file

@ -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;
}

View file

@ -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;