Plus Server suggestions visible for everyone

This commit is contained in:
Kalle 2023-01-15 19:52:12 +02:00
parent ce9b8b0b85
commit d985eadb9a
5 changed files with 30 additions and 68 deletions

View File

@ -51,7 +51,7 @@ export function findVisibleForUser(
args: MonthYear &
Pick<UserWithPlusTier, "plusTier"> & { includeBio?: boolean }
): FindVisibleForUser | undefined {
if (!args.plusTier) return;
if (typeof args.plusTier !== "number") return;
return sortNewestPlayersToBeSuggestedFirst(
mapFindVisibleForUserRowsToResult(
findVisibleForUserStm.all(args),
@ -60,6 +60,12 @@ export function findVisibleForUser(
);
}
export function findAll(args: MonthYear & { includeBio?: boolean }) {
// plusTier 0 is a bit hacky way to get all suggestions
// while reusing the query
return findVisibleForUser({ ...args, plusTier: 0 })!;
}
function mapFindVisibleForUserRowsToResult(
rows: any[],
includeBio?: boolean

View File

@ -46,6 +46,7 @@ export function canAddCommentToSuggestionFE(
return allTruthy([
!alreadyCommentedByUser(args),
isPlusServerMember(args.user),
args.user?.plusTier && args.targetPlusTier >= args.user?.plusTier,
]);
}

View File

@ -18,7 +18,7 @@ import { nextNonCompletedVoting } from "~/modules/plus-server";
import { db } from "~/db";
import type * as plusSuggestions from "~/db/models/plusSuggestions/queries.server";
import type { PlusSuggestion, User } from "~/db/types";
import { getUser, requireUser, useUser } from "~/modules/auth";
import { requireUser, useUser } from "~/modules/auth";
import {
canAddCommentToSuggestionFE,
canSuggestNewUserFE,
@ -30,11 +30,12 @@ import { parseRequestFormData, validate } from "~/utils/remix";
import { makeTitle } from "~/utils/strings";
import { discordFullName } from "~/utils/strings";
import { actualNumber } from "~/utils/zod";
import { FAQ_PAGE, LOG_IN_URL, userPage } from "~/utils/urls";
import { userPage } from "~/utils/urls";
import { RelativeTime } from "~/components/RelativeTime";
import { databaseTimestampToDate } from "~/utils/dates";
import { PLUS_TIERS } from "~/constants";
import { assertUnreachable } from "~/utils/types";
import { getUserId } from "~/modules/auth/user.server";
export const meta: MetaFunction = () => {
return {
@ -133,7 +134,7 @@ export const action: ActionFunction = async ({ request }) => {
};
export interface PlusSuggestionsLoaderData {
suggestions?: plusSuggestions.FindVisibleForUser;
suggestions: plusSuggestions.FindVisibleForUser;
suggestedForTiers: number[];
}
@ -143,23 +144,18 @@ export const unstable_shouldReload: ShouldReloadFunction = ({ submission }) => {
};
export const loader: LoaderFunction = async ({ request }) => {
const user = await getUser(request);
if (!user) {
return json<PlusSuggestionsLoaderData>({
suggestedForTiers: [],
});
}
const user = await getUserId(request);
return json<PlusSuggestionsLoaderData>({
suggestions: db.plusSuggestions.findVisibleForUser({
suggestions: db.plusSuggestions.findAll({
...nextNonCompletedVoting(new Date()),
plusTier: user.plusTier,
}),
suggestedForTiers: db.plusSuggestions.tiersSuggestedFor({
...nextNonCompletedVoting(new Date()),
userId: user.id,
}),
suggestedForTiers: user
? db.plusSuggestions.tiersSuggestedFor({
...nextNonCompletedVoting(new Date()),
userId: user.id,
})
: [],
});
};
@ -173,27 +169,6 @@ export default function PlusSuggestionsPage() {
setSearchParams({ tier });
};
if (!user) {
return (
<form className="text-sm" action={LOG_IN_URL} method="post">
<p className="button-text-paragraph">
To view your suggestion status{" "}
<Button type="submit" variant="minimal">
log in
</Button>
</p>
<p className="mt-2">
Not sure what the Plus Server is about? Read the{" "}
<Link to={FAQ_PAGE}>FAQ</Link>
</p>
</form>
);
}
if (!data.suggestions) {
return <SuggestedForInfo />;
}
const visibleSuggestions =
tierVisible && data.suggestions[tierVisible]
? data.suggestions[tierVisible]
@ -205,7 +180,7 @@ export default function PlusSuggestionsPage() {
<Outlet />
<div className="plus__container">
<div className="stack md">
<SuggestedForInfo hideText />
<SuggestedForInfo />
<div className="stack lg">
<div
className={clsx("plus__top-container", {
@ -298,34 +273,13 @@ function tierVisibleInitialState(
return String(Math.min(...Object.keys(suggestions).map(Number)));
}
function SuggestedForInfo({ hideText = false }: { hideText?: boolean }) {
function SuggestedForInfo() {
const data = useLoaderData<PlusSuggestionsLoaderData>();
const user = useUser();
// no need to show anything if they can't be suggested anyway...
if (user?.plusTier === 1) {
return null;
}
if (data.suggestedForTiers.length === 0) {
if (hideText) return null;
return (
<div className="plus__suggested-info-text">
You are not suggested yet this month.
</div>
);
}
if (data.suggestedForTiers.length === 0) return null;
return (
<div className="stack md">
{!hideText ? (
<div className="plus__suggested-info-text">
You are suggested to{" "}
{data.suggestedForTiers.map((tier) => `+${tier}`).join(" and ")} this
month.
</div>
) : null}
{canDeleteSuggestionOfThemselves() ? (
<div className="stack horizontal md">
{data.suggestedForTiers.map((tier) => (

View File

@ -21,7 +21,7 @@ import type { PlusSuggestionsLoaderData } from "../suggestions";
import { CommentTextarea } from "./new";
const commentActionSchema = z.object({
text: z.preprocess(
comment: z.preprocess(
trimmedString,
z.string().min(1).max(PlUS_SUGGESTION_COMMENT_MAX_LENGTH)
),
@ -60,6 +60,7 @@ export const action: ActionFunction = async ({ request }) => {
db.plusSuggestions.create({
authorId: user.id,
...data,
text: data.comment,
...nextNonCompletedVoting(new Date()),
});

View File

@ -42,7 +42,7 @@ const commentActionSchema = z.object({
.min(Math.min(...PLUS_TIERS))
.max(Math.max(...PLUS_TIERS))
),
text: z.preprocess(
comment: z.preprocess(
trimmedString,
z.string().min(1).max(PlUS_SUGGESTION_FIRST_COMMENT_MAX_LENGTH)
),
@ -80,7 +80,7 @@ export const action: ActionFunction = async ({ request }) => {
authorId: user.id,
suggestedId: suggested.id,
tier: data.tier,
text: data.text,
text: data.comment,
...nextNonCompletedVoting(new Date()),
});
@ -222,7 +222,7 @@ export function CommentTextarea({ maxLength }: { maxLength: number }) {
return (
<div>
<Label
htmlFor="text"
htmlFor="comment"
valueLimits={{
current: value.trim().length,
max: maxLength,
@ -231,8 +231,8 @@ export function CommentTextarea({ maxLength }: { maxLength: number }) {
Your comment
</Label>
<textarea
id="text"
name="text"
id="comment"
name="comment"
className="plus__modal-textarea"
rows={4}
value={value}