Skip to content

Commit

Permalink
add: default processors
Browse files Browse the repository at this point in the history
  • Loading branch information
benchambule committed Aug 22, 2024
1 parent e0346ec commit bea0c1f
Showing 1 changed file with 131 additions and 1 deletion.
132 changes: 131 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
export {
FORM,
INFORMATION,

Message,
Lottus
Lottus,

create_options_processor,
create_input_processor,
create_form_processor
}

const FORM = "FORM";
Expand Down Expand Up @@ -194,4 +199,129 @@ class Lottus {

return result;
}
}


Message.prototype.header = "";
Message.prototype.body = "";
Message.prototype.footer = "Powered by Lottus";


Message.prototype.addOption = function addOption(key, label, next, params){
if(!this.form){
this.form = {options: new Map()};
}

if(!this.form.options){
this.form.options = new Map();
}

if(key === undefined || key === null){
key = this.form.options.size + 1;
}

if(!label){
throw new Error("Argument label cannot be null nor empty");
}

this.form.options.set(key.toString(), {label, next, params});
}


Message.prototype.addInput = function addInput(name, type, next){
if(!this.form){
this.form = {input: {name, type, next}};
}

if(!this.form.input){
this.form = {input: {name, type, next}};
}
}


function create_options_processor(bot){
async function create_options_processor(req, res){
const options = req.form?.options;

if(options){
const option = options.get(req.prompt.toString());

if(option){
req.selected_option = option;
let next = req.form?.next;

if(req.selected_option.next){
next = req.selected_option.next;
}

return await bot.redirect(next, req);
} else {
res.error = "You selected an invalid option";
}
}

return res;
}

return create_options_processor;
}


function create_input_processor(bot){
async function input_processor(req, res){
const input = req.form?.input;

if(input){
req.input = input;
// let next = req.form?.next;
return await bot.redirect(input.next, req);
} else {
res.error = "You selected an invalid option";
}

return res;
}

return input_processor;
}


function create_form_processor(bot){
async function form_processor(req, res){
const options = req.form?.options;
const input = req.form?.input;

if(options){
const option = options.get(req.prompt);

if(option){
req.selected_option = option;
let next = req.form?.next;

if(req.selected_option.next){
next = req.selected_option.next;
}

return await bot.redirect(next, req);
} else {
res.error = "You selected an invalid option";
}
}

if(input){
req.input = input;

let next = req.form?.next;

return await bot.redirect(next, req);
}

if(!res.error){
res.error = "The form has no input nor options";
}

return res;
}

return form_processor;
}

0 comments on commit bea0c1f

Please sign in to comment.