Skip to content

Commit

Permalink
Add controls for property pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantGeekDev committed Jan 5, 2024
1 parent 289e974 commit 256e451
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/controllers/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,63 @@ import {
generatePropertyDescription,
generatePropertyPhotoAlbum
} from "../services/Property/property.service.js";
import { propertyControlKeyboard } from "../menus/propertyMenu.js";

export const propertiesController = new Composer<CustomContext>();
let currentPropertyIndex = 0;

propertiesController.command("properties", async ctx => {
const properties = (await ctx.db.property.find({}).toArray()) as Property[];
const currentPropertyIndex = 0;

const currentProperty = properties[currentPropertyIndex];
const totalProperties = properties.length;

const { videoFileId: videoUrl, albumUrls } = currentProperty;
const { videoFileId, albumUrls } = currentProperty;

const propertyDescription = generatePropertyDescription(currentProperty);
const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`);
await ctx.reply(propertyDescription, {
parse_mode: "MarkdownV2"
});
await ctx.replyWithVideo(videoUrl);
await ctx.replyWithVideo(videoFileId);
await ctx.replyWithMediaGroup(propertyPhotoAlbum);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`, {
reply_markup: propertyControlKeyboard
});

propertiesController.callbackQuery("next-property", async ctx => {
ctx.answerCallbackQuery("");
currentPropertyIndex++;
const currentProperty = properties[currentPropertyIndex];
const totalProperties = properties.length;
const { videoFileId, albumUrls } = currentProperty;
const propertyDescription = generatePropertyDescription(currentProperty);
const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`);
await ctx.reply(propertyDescription, {
parse_mode: "MarkdownV2"
});
await ctx.replyWithVideo(videoFileId);
await ctx.replyWithMediaGroup(propertyPhotoAlbum);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`, {
reply_markup: propertyControlKeyboard
});
});

propertiesController.callbackQuery("previous-property", async ctx => {
ctx.answerCallbackQuery("");
currentPropertyIndex--;
const currentProperty = properties[currentPropertyIndex];
const totalProperties = properties.length;
const { videoFileId, albumUrls } = currentProperty;
const propertyDescription = generatePropertyDescription(currentProperty);
const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`);
await ctx.reply(propertyDescription, {
parse_mode: "MarkdownV2"
});
await ctx.replyWithVideo(videoFileId);
await ctx.replyWithMediaGroup(propertyPhotoAlbum);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`, {
reply_markup: propertyControlKeyboard
});
});
});
5 changes: 5 additions & 0 deletions src/menus/propertyMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { InlineKeyboard } from "grammy";

export const propertyControlKeyboard = new InlineKeyboard()
.text("« Previous Property", "previous-property")
.text("Next Property » ", "next-property");

0 comments on commit 256e451

Please sign in to comment.