-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from osu-tournament-rating/beatmap-submission
Refactor tournament submission to use the otr-api-client
- Loading branch information
Showing
20 changed files
with
374 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use server'; | ||
|
||
import { isHttpValidationProblemDetails } from "@/lib/api"; | ||
import { apiWrapperConfiguration } from "@/lib/auth"; | ||
import { BeatmapLinkPattern, MatchLinkPattern } from "@/lib/regex"; | ||
import { TournamentSubmissionFormSchema } from "@/lib/schemas"; | ||
import { FormState } from "@/lib/types"; | ||
import { extractFormData } from "@/util/forms"; | ||
import { TournamentSubmissionDTO, TournamentsWrapper } from "@osu-tournament-rating/otr-api-client"; | ||
import { ZodError } from "zod"; | ||
|
||
/** | ||
* Handles parsing, submiting, and handling errors for tournament submission data | ||
* @param _previousState Previous form state | ||
* @param formData Form data | ||
* @returns The state of the form after performing the action | ||
*/ | ||
export async function tournamentSubmissionFormAction( | ||
_previousState: FormState<TournamentSubmissionDTO>, | ||
formData: FormData | ||
): Promise<FormState<TournamentSubmissionDTO>> { | ||
const result: FormState<TournamentSubmissionDTO> = { | ||
success: false, | ||
message: "", | ||
errors: {} | ||
}; | ||
|
||
try { | ||
const parsedForm = TournamentSubmissionFormSchema.parse(extractFormData<TournamentSubmissionDTO>(formData, { | ||
ruleset: value => parseInt(value), | ||
ids: value => value | ||
// Split at new lines | ||
.split(/\r?\n/g) | ||
// Filter out empty strings | ||
.filter(s => s.trim() !== '') | ||
.map(s => { | ||
// Trim whitespace | ||
s = s.trim(); | ||
|
||
// If the string is parseable to an int as is, do so | ||
if (!isNaN(parseFloat(s))) { | ||
return parseFloat(s); | ||
} | ||
|
||
// Try to extract the id using regex | ||
const match = MatchLinkPattern.exec(s); | ||
return match ? parseFloat(match[1]) : s; | ||
}), | ||
beatmapIds: value => value | ||
.split(/\r?\n/g) | ||
.filter(s => s.trim() !== '') | ||
.map(s => { | ||
s = s.trim(); | ||
|
||
if (!isNaN(parseFloat(s))) { | ||
return parseFloat(s); | ||
} | ||
|
||
const match = BeatmapLinkPattern.exec(s); | ||
return match ? parseFloat(match[1]) : s; | ||
}) | ||
})); | ||
|
||
const wrapper = new TournamentsWrapper(apiWrapperConfiguration); | ||
await wrapper.create({ body: parsedForm }); | ||
|
||
result.message = "Successfully processed your submission. Thank you for contributing!"; | ||
result.success = true; | ||
} catch (err) { | ||
result.message = "Submission was not successful."; | ||
result.success = false; | ||
|
||
// Handle parsing errors | ||
if (err instanceof ZodError) { | ||
Object.assign(result.errors, err.flatten().fieldErrors); | ||
result.message += " There was a problem processing your submission."; | ||
} | ||
|
||
// Handle API errors | ||
if (isHttpValidationProblemDetails(err)) { | ||
Object.assign(result.errors, err.errors); | ||
result.message += " The server rejected your submission."; | ||
} | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.inputError { | ||
color: hsla(var(--red-600)); | ||
font-weight: 500; | ||
font-size: 0.8rem; | ||
} | ||
|
||
.inputError:empty { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use client'; | ||
|
||
import styles from './InputError.module.css'; | ||
|
||
export default function FormInputError({ message }: { message: string | string[] | undefined }) { | ||
if (!message) { | ||
return; | ||
} | ||
|
||
return ( | ||
<span className={styles.inputError}> | ||
{Array.isArray(message) ? message.map((m, idx) => (<p key={`error${idx}`}>{m}</p>)) : (<p>{message}</p>)} | ||
</span> | ||
) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
1 change: 1 addition & 0 deletions
1
...s/SubmitMatches/Guidelines/Guidelines.tsx → ...ents/Submission/Guidelines/Guidelines.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
'use client'; | ||
|
||
import Card from '@/components/Card/Card'; | ||
import styles from './Guidelines.module.css'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.