aboutsummaryrefslogtreecommitdiffstats
path: root/util/adhoc.lua
blob: a0ad52bbf2fbf179a6c626be0e104130687367c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-- luacheck: ignore 212/self

local function new_simple_form(form, result_handler)
	return function(self, data, state)
		if state or data.form then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			return { status = "executing", actions = {"next", "complete", default = "complete"}, form = form }, "executing";
		end
	end
end

local function new_initial_data_form(form, initial_data, result_handler)
	return function(self, data, state)
		if state or data.form then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			local values, err = initial_data(data);
			if type(err) == "table" then
				return {status = "error"; error = err}
			elseif type(err) == "string" then
				return {status = "error"; error = {type = "cancel"; condition = "internal-server-error", err}}
			end
			return { status = "executing", actions = {"next", "complete", default = "complete"},
				 form = { layout = form, values = values } }, "executing";
		end
	end
end

return { new_simple_form = new_simple_form,
	 new_initial_data_form = new_initial_data_form };