Skip to content

Commit

Permalink
Rename prospective to perspective
Browse files Browse the repository at this point in the history
  • Loading branch information
danthonywalker committed Oct 14, 2023
1 parent fc52718 commit 2c97e98
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/commands/carsized/carsized.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Environment from "../../shared/environment";
import { isNonNullable } from "../../shared/nullable";
import { usePage } from "../../shared/puppeteer";
import RedisKey, * as redis from "../../shared/redis";
import { Prospective } from "./constants";
import { Perspective } from "./constants";

const logger = loggerFactory({
name: __filename,
Expand All @@ -36,17 +36,17 @@ export const searchCars = (carName: string) => {

export const compareCars = async ({
firstCar,
prospective,
perspective,
secondCar,
units,
}: CompareCars) =>
usePage(async (page) => {
await page.setJavaScriptEnabled(false);
const compareSegment = `${firstCar.id}-vs-${secondCar.id}`;
const prospectiveSegment =
prospective === Prospective.Side ? "" : prospective;
const perspectiveSegment =
perspective === Perspective.Side ? "" : perspective;
// prettier-ignore
await page.goto(`${CarsizedBaseUrl}/cars/compare/${compareSegment}/${prospectiveSegment}?units=${units}`);
await page.goto(`${CarsizedBaseUrl}/cars/compare/${compareSegment}/${perspectiveSegment}?units=${units}`);

const contentHandle = await page.$("div.flowcontent");
assert(contentHandle !== null);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/carsized/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export enum Units {
Metric = "metric",
}

export enum Prospective {
export enum Perspective {
Front = "front",
Rear = "rear",
Side = "side",
Expand Down
12 changes: 10 additions & 2 deletions src/commands/carsized/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ const logger = loggerFactory({
name: __filename,
});

const session = new Session<Context>();
export default session;

export const compareCarsUi = async (
context: Context,
interaction: Interaction,
) => {
// "perspective" was wrongly spelled "prospective" initially
// Use new spelling while supporting older sessions
if (context.perspective === undefined) {
context.perspective = context.prospective;
context = await session.update(context, interaction);
}

const response = interaction.isCommand()
? await interaction.deferReply()
: await interaction.update(UI.compareCars(context));
Expand Down Expand Up @@ -57,5 +67,3 @@ export const compareCarsUi = async (

return response;
};

export default new Session<Context>();
18 changes: 9 additions & 9 deletions src/commands/carsized/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { registerCommand, toChoices } from "../../shared/discord";
import Environment from "../../shared/environment";
import onAutocomplete from "./autocomplete";
import * as carsized from "./carsized.manager";
import { Prospective, Units } from "./constants";
import { Perspective, Units } from "./constants";
import session, * as withContext from "./context";
import UI from "./ui";

enum Option {
FirstCar = "first-car",
Prospective = "prospective",
Perspective = "perspective",
SecondCar = "second-car",
Units = "units",
}
Expand All @@ -37,9 +37,9 @@ const json = new SlashCommandBuilder()
)
.addStringOption((option) =>
option
.setName(Option.Prospective)
.setDescription("Prospective of the comparison")
.setChoices(...toChoices(Prospective)),
.setName(Option.Perspective)
.setDescription("Perspective of the comparison")
.setChoices(...toChoices(Perspective)),
)
.addStringOption((option) =>
option
Expand All @@ -63,16 +63,16 @@ const onCommand = async (interaction: CommandInteraction) => {
if (secondCar === undefined)
return interaction.reply(UI.noCarExists(secondCarId));

const prospectiveOption =
options.getString(Option.Prospective) ?? Prospective.Side;
const prospective = prospectiveOption as Prospective;
const perspectiveOption =
options.getString(Option.Perspective) ?? Perspective.Side;
const perspective = perspectiveOption as Perspective;

const unitsOption = options.getString(Option.Units) ?? Units.Metric;
const units = unitsOption as Units;

const partialContext = {
firstCar,
prospective,
perspective,
secondCar,
units,
};
Expand Down
6 changes: 4 additions & 2 deletions src/commands/carsized/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Prospective, Units } from "./constants";
import type { Perspective, Units } from "./constants";

export type Car = {
body: string;
Expand All @@ -10,7 +10,9 @@ export type Car = {

export type CompareCars = {
firstCar: Car;
prospective: Prospective;
perspective: Perspective | undefined;
/** @deprecated Use perspective **/
prospective?: Perspective;
secondCar: Car;
units: Units;
};
4 changes: 2 additions & 2 deletions src/commands/carsized/ui/front.button.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { registerComponent } from "../../../shared/discord";
import { Prospective } from "../constants";
import { Perspective } from "../constants";
import session, * as withContext from "../context";
import { UIID } from "../ui";

registerComponent(UIID.FrontButton, async (interaction, sessionId) => {
const context = await session.read(sessionId);
context.prospective = Prospective.Front;
context.perspective = Perspective.Front;

const newContext = await session.update(context, interaction);
return withContext.compareCarsUi(newContext, interaction);
Expand Down
20 changes: 10 additions & 10 deletions src/commands/carsized/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { Context } from "../context";
import { Color } from "../../../shared/discord";
import Environment from "../../../shared/environment";
import * as carsized from "../carsized.manager";
import { Prospective, Units } from "../constants";
import { Perspective, Units } from "../constants";

export enum UIID {
FrontButton = "26b9b11a-0134-46b0-9552-6a8d9353176e",
Expand Down Expand Up @@ -62,32 +62,32 @@ const swapActionRow = (context: Context, isLoading: boolean) =>
);
// endregion

// region Prospective
const frontButton = ({ prospective, sessionId }: Context, isLoading: boolean) =>
// region Perspective
const frontButton = ({ perspective, sessionId }: Context, isLoading: boolean) =>
new ButtonBuilder()
.setCustomId(`${UIID.FrontButton}${sessionId}`)
.setDisabled(prospective === Prospective.Front || isLoading)
.setDisabled(perspective === Perspective.Front || isLoading)
.setEmoji("🚘")
.setLabel("| Front")
.setStyle(ButtonStyle.Secondary);

const sideButton = ({ prospective, sessionId }: Context, isLoading: boolean) =>
const sideButton = ({ perspective, sessionId }: Context, isLoading: boolean) =>
new ButtonBuilder()
.setCustomId(`${UIID.SideButton}${sessionId}`)
.setDisabled(prospective === Prospective.Side || isLoading)
.setDisabled(perspective === Perspective.Side || isLoading)
.setEmoji("🚗")
.setLabel("| Side")
.setStyle(ButtonStyle.Secondary);

const rearButton = ({ prospective, sessionId }: Context, isLoading: boolean) =>
const rearButton = ({ perspective, sessionId }: Context, isLoading: boolean) =>
new ButtonBuilder()
.setCustomId(`${UIID.RearButton}${sessionId}`)
.setDisabled(prospective === Prospective.Rear || isLoading)
.setDisabled(perspective === Perspective.Rear || isLoading)
.setEmoji("🍑")
.setLabel("| Rear")
.setStyle(ButtonStyle.Secondary);

const prospectiveActionRow = (context: Context, isLoading: boolean) =>
const perspectiveActionRow = (context: Context, isLoading: boolean) =>
new ActionRowBuilder<ButtonBuilder>().setComponents(
frontButton(context, isLoading),
sideButton(context, isLoading),
Expand Down Expand Up @@ -121,7 +121,7 @@ const unitsActionRow = (context: Context, isLoading: boolean) =>

const compareCarsComponents = (context: Context, isLoading: boolean) => [
swapActionRow(context, isLoading),
prospectiveActionRow(context, isLoading),
perspectiveActionRow(context, isLoading),
unitsActionRow(context, isLoading),
];
// endregion
Expand Down
4 changes: 2 additions & 2 deletions src/commands/carsized/ui/rear.button.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { registerComponent } from "../../../shared/discord";
import { Prospective } from "../constants";
import { Perspective } from "../constants";
import session, * as withContext from "../context";
import { UIID } from "../ui";

registerComponent(UIID.RearButton, async (interaction, sessionId) => {
const context = await session.read(sessionId);
context.prospective = Prospective.Rear;
context.perspective = Perspective.Rear;

const newContext = await session.update(context, interaction);
return withContext.compareCarsUi(newContext, interaction);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/carsized/ui/side.button.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { registerComponent } from "../../../shared/discord";
import { Prospective } from "../constants";
import { Perspective } from "../constants";
import session, * as withContext from "../context";
import { UIID } from "../ui";

registerComponent(UIID.SideButton, async (interaction, sessionId) => {
const context = await session.read(sessionId);
context.prospective = Prospective.Side;
context.perspective = Perspective.Side;

const newContext = await session.update(context, interaction);
return withContext.compareCarsUi(newContext, interaction);
Expand Down

0 comments on commit 2c97e98

Please sign in to comment.