Skip to content

Commit

Permalink
Make Catalogs keep selections when creating Games or Versions or Goals (
Browse files Browse the repository at this point in the history
#1964)

* make catalogs keep selections when creating games or versions or goals

* simplify the js by sending everything always

This also means when you e.g. create a new game, the url will contain the version and goal ids of the old game. But we properly validate this in the OnGet anyway, as we should.
  • Loading branch information
Masterjun3 authored Aug 23, 2024
1 parent dd7060e commit e570e91
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 13 deletions.
10 changes: 7 additions & 3 deletions TASVideos/Pages/Games/Goals/List.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ public async Task<IActionResult> OnPost(string? goalToCreate)
return BackToList();
}

db.GameGoals.Add(new GameGoal
var gameGoal = new GameGoal
{
GameId = GameId,
DisplayName = goalToCreate
});
};

db.GameGoals.Add(gameGoal);

SetMessage(await db.TrySaveChanges(), $"Goal {goalToCreate} created successfully", $"Unable to create goal {goalToCreate}");
return BackToList();
return string.IsNullOrWhiteSpace(Request.ReturnUrl())
? RedirectToPage("List", new { GameId })
: BaseReturnUrlRedirect(new() { ["GameGoalId"] = gameGoal.Id.ToString() });
}

public async Task<IActionResult> OnPostEdit(int gameGoalId, string? newGoalName)
Expand Down
2 changes: 1 addition & 1 deletion TASVideos/Pages/Games/Versions/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public async Task<IActionResult> OnPost()
SuccessStatusMessage($"Game Version {Id} updated");
return string.IsNullOrWhiteSpace(HttpContext.Request.ReturnUrl())
? RedirectToPage("List", new { gameId = GameId })
: BaseReturnUrlRedirect(new() { ["GameId"] = GameId.ToString(), ["GameVersionId"] = version.Id.ToString() });
: BaseReturnUrlRedirect(new() { ["SystemId"] = system.Id.ToString(), ["GameId"] = GameId.ToString(), ["GameVersionId"] = version.Id.ToString() });
}

public async Task<IActionResult> OnPostDelete()
Expand Down
29 changes: 27 additions & 2 deletions TASVideos/Pages/Publications/Catalog.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ public class CatalogModel(ApplicationDbContext db, ExternalMediaPublisher publis
[FromRoute]
public int Id { get; set; }

[FromQuery]
public int? SystemId { get; set; }

[FromQuery]
public int? GameId { get; set; }

[FromQuery]
public int? GameVersionId { get; set; }

[FromQuery]
public int? GameGoalId { get; set; }

[BindProperty]
public PublicationCatalog Catalog { get; set; } = new();

Expand Down Expand Up @@ -45,6 +51,16 @@ public async Task<IActionResult> OnGet()
}

Catalog = catalog;

if (SystemId.HasValue)
{
var system = await db.GameSystems.FindAsync(SystemId);
if (system is not null)
{
Catalog.System = system.Id;
}
}

if (GameId.HasValue)
{
var game = await db.Games.FindAsync(GameId);
Expand All @@ -61,6 +77,15 @@ public async Task<IActionResult> OnGet()
Catalog.GameVersion = gameVersion.Id;
}
}

if (GameGoalId.HasValue)
{
var gameGoal = await db.GameGoals.SingleOrDefaultAsync(gg => gg.GameId == game.Id && gg.Id == GameGoalId);
if (gameGoal is not null)
{
Catalog.Goal = gameGoal.Id;
}
}
}
}

Expand Down Expand Up @@ -202,9 +227,9 @@ public class PublicationCatalog
{
public string Title { get; init; } = "";
public int GameVersion { get; set; }
public int Goal { get; init; }
public int Goal { get; set; }
public int Game { get; set; }
public int System { get; init; }
public int System { get; set; }
public int SystemFramerate { get; init; }

[StringLength(50)]
Expand Down
6 changes: 5 additions & 1 deletion TASVideos/Pages/Submissions/Catalog.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@
<span asp-validation-for="Catalog.Goal"></span>
</div>
<div class="col-2">
<button id="create-goal" type="button" class="btn btn-primary">Manage</button>
<button
disable="Model.Catalog.Game is null or -1"
id="create-goal"
type="button"
class="btn btn-primary">Manage</button>
</div>
</row>
</fieldset>
Expand Down
29 changes: 27 additions & 2 deletions TASVideos/Pages/Submissions/Catalog.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ public class CatalogModel(ApplicationDbContext db, ExternalMediaPublisher publis
[FromRoute]
public int Id { get; set; }

[FromQuery]
public int? SystemId { get; set; }

[FromQuery]
public int? GameId { get; set; }

[FromQuery]
public int? GameVersionId { get; set; }

[FromQuery]
public int? GameGoalId { get; set; }

[BindProperty]
public SubmissionCatalog Catalog { get; set; } = new();

Expand Down Expand Up @@ -45,6 +51,16 @@ public async Task<IActionResult> OnGet()
}

Catalog = catalog;

if (SystemId.HasValue)
{
var system = await db.GameSystems.FindAsync(SystemId);
if (system is not null)
{
Catalog.System = system.Id;
}
}

if (GameId.HasValue)
{
var game = await db.Games.FindAsync(GameId);
Expand All @@ -61,6 +77,15 @@ public async Task<IActionResult> OnGet()
Catalog.GameVersion = version.Id;
}
}

if (GameGoalId.HasValue)
{
var gameGoal = await db.GameGoals.SingleOrDefaultAsync(gg => gg.GameId == game.Id && gg.Id == GameGoalId);
if (gameGoal is not null)
{
Catalog.Goal = gameGoal.Id;
}
}
}
}

Expand Down Expand Up @@ -238,12 +263,12 @@ public class SubmissionCatalog
public int? Game { get; set; }

[Required]
public int? System { get; init; }
public int? System { get; set; }

[Required]
public int? SystemFramerate { get; init; }

public int? Goal { get; init; }
public int? Goal { get; set; }

[StringLength(50)]
public string? Emulator { get; init; }
Expand Down
30 changes: 26 additions & 4 deletions TASVideos/wwwroot/js/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ function enableCataloging() {
const createVersionBtn = document.getElementById('create-version');
const gameGoalModel = document.querySelector('[data-id="goal"]');
const gameGoalBtn = document.getElementById('create-goal');
const returnUrl = encodeURIComponent(systemModel.dataset.returnUrl);
const returnUrlPathAndQuery = systemModel.dataset.returnUrl.split(/\?(.*)/s) // splits string at the first question mark (?)
const returnUrlPath = returnUrlPathAndQuery[0];
const returnUrlQuery = returnUrlPathAndQuery[1] ?? "";

systemModel.onchange = function () {
if (this.value) {
Expand Down Expand Up @@ -58,20 +60,40 @@ function enableCataloging() {
} else {
createVersionBtn.classList.add('disabled');
createVersionBtn.setAttribute('disabled', 'disabled');
gameGoalBtn.classList.add('disabled');
gameGoalBtn.setAttribute('disabled', 'disabled');
clearDropdown(versionElemId);
clearDropdown(gameGoalElemId);
}
}

document.getElementById('create-version')?.addEventListener('click', function () {
document.location = `/Games/${gameModel.value}/Versions/Edit?returnUrl=${returnUrl}&systemId=${systemModel.value}`;
document.location = `/Games/${gameModel.value}/Versions/Edit?SystemId=${systemModel.value}&returnUrl=${generateCurrentReturnUrl() }`;
});

document.getElementById('create-game')?.addEventListener('click', function () {
document.location = `/Games/Edit?returnUrl=${returnUrl}`;
document.location = `/Games/Edit?returnUrl=${generateCurrentReturnUrl() }`;
});

gameGoalBtn?.addEventListener('click', function () {
document.location = `/Games/${gameModel.value}/Goals/List?returnUrl=${returnUrl}`;
document.location = `/Games/${gameModel.value}/Goals/List?returnUrl=${generateCurrentReturnUrl()}`;
});

function generateCurrentReturnUrl() {
const returnUrlQueryModified = new URLSearchParams();
if (systemModel.value) {
returnUrlQueryModified.set('SystemId', systemModel.value);
}
if (gameModel.value) {
returnUrlQueryModified.set('GameId', gameModel.value);
}
if (versionModel.value) {
returnUrlQueryModified.set('GameVersionId', versionModel.value);
}
if (gameGoalModel.value) {
returnUrlQueryModified.set('GameGoalId', gameGoalModel.value);
}

return encodeURIComponent(returnUrlPath + '?' + returnUrlQueryModified.toString());
}
}

0 comments on commit e570e91

Please sign in to comment.