Skip to content

Commit

Permalink
Upgrade datetime picker. Fix ts issues
Browse files Browse the repository at this point in the history
  • Loading branch information
grassick committed Oct 18, 2022
1 parent 8b99c0d commit 305e431
Show file tree
Hide file tree
Showing 22 changed files with 1,739 additions and 2,245 deletions.
2 changes: 2 additions & 0 deletions lib/CalculationsDisplayComponent.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ResponseRow from "./ResponseRow";
export declare const CalculationsDisplayComponent: (props: {
formDesign: FormDesign;
locale: string;
/** Localizer to use */
T: any;
schema: Schema;
responseRow: ResponseRow;
}) => JSX.Element;
4 changes: 2 additions & 2 deletions lib/CalculationsDisplayComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ const CalculationsDisplayComponent = (props) => {
return (react_1.default.createElement("table", { className: "table table-bordered table-sm" },
react_1.default.createElement("thead", null,
react_1.default.createElement("tr", null,
react_1.default.createElement("th", null, "Calculation"),
react_1.default.createElement("th", null, "Value"))),
react_1.default.createElement("th", null, props.T("Calculation")),
react_1.default.createElement("th", null, props.T("Value")))),
react_1.default.createElement("tbody", null, props.formDesign.calculations.filter((calc) => !calc.roster).map((calc, index) => renderCalc(calc, index)))));
};
exports.CalculationsDisplayComponent = CalculationsDisplayComponent;
2 changes: 1 addition & 1 deletion lib/DateTimePickerComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DateTimePickerComponent extends react_1.default.Component {
if (this.props.date) {
this.control.dates.setFromInput(this.props.date.format(format), 0);
}
this.control.subscribe(tempus_dominus_1.Namespace.events.change, e => {
this.control.subscribe("change", e => {
this.props.onChange(e.date ? (0, moment_1.default)(e.date) : null);
});
}
Expand Down
19 changes: 10 additions & 9 deletions lib/ResponseAnswersComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
})());
}
else {
return R("span", { className: "badge bg-danger" }, "Invalid Choice");
return R("span", { className: "badge bg-danger" }, this.props.T("Invalid Choice"));
}
case "choices":
return lodash_1.default.map(answer.value, (v) => {
Expand All @@ -144,7 +144,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
})());
}
else {
return R("div", { className: "badge bg-danger" }, "Invalid Choice");
return R("div", { className: "badge bg-danger" }, this.props.T("Invalid Choice"));
}
});
case "date":
Expand All @@ -165,7 +165,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
// Find units
const units = lodash_1.default.findWhere(q.units, { id: answer.value.units });
const valueStr = "" + answer.value.quantity;
const unitsStr = units ? formUtils.localizeString(units.label, this.props.locale) : "(Invalid)";
const unitsStr = units ? formUtils.localizeString(units.label, this.props.locale) : this.props.T("(Invalid)");
if (q.unitsPosition === "prefix") {
return R("div", null, R("em", null, unitsStr), " ", valueStr);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
T: this.props.T
});
case "admin_region":
return R("div", { className: "alert alert-warning" }, "Admin region questions are no longer supported");
return R("div", { className: "alert alert-warning" }, this.props.T("Admin region questions are no longer supported"));
case "items_choices":
for (let item of q.items) {
const choiceId = answer.value[item.id];
Expand All @@ -244,7 +244,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
return R("div", null, formUtils.localizeString(choice.label, this.props.locale));
}
else {
return R("span", { className: "badge bg-danger" }, "Invalid Choice");
return R("span", { className: "badge bg-danger" }, this.props.T("Invalid Choice"));
}
}
}
Expand Down Expand Up @@ -297,7 +297,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
contents.push(R("tr", null, itemTd, R("td", null, formUtils.localizeString(choice.label, this.props.locale))));
}
else {
contents.push(R("tr", null, itemTd, R("td", null, R("span", { className: "badge bg-danger" }, "Invalid Choice"))));
contents.push(R("tr", null, itemTd, R("td", null, R("span", { className: "badge bg-danger" }, this.props.T("Invalid Choice")))));
}
if (this.props.showPrevAnswers && prevAnswer) {
choiceId = prevAnswer.value[item.id];
Expand All @@ -307,7 +307,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
contents.push(R("tr", null, itemTd, R("td", null, formUtils.localizeString(choice.label, this.props.locale))));
}
else {
contents.push(R("tr", null, itemTd, R("td", null, R("span", { className: "badge bg-danger" }, "Invalid Choice"))));
contents.push(R("tr", null, itemTd, R("td", null, R("span", { className: "badge bg-danger" }, this.props.T("Invalid Choice")))));
}
}
}
Expand Down Expand Up @@ -588,7 +588,7 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
if (!this.state.visibilityStructure) {
return R("div", null, "Loading...");
}
return R("div", null, R("table", { className: "table table-bordered table-sm", style: { marginBottom: 0 } }, R("thead", null, R("tr", null, R("th", null, "Question"), R("th", null, "Answer"), this.props.showPrevAnswers ? R("th", null, "Original Answer") : undefined)), R("tbody", null, this.props.formDesign.contents.map((item) => {
return R("div", null, R("table", { className: "table table-bordered table-sm", style: { marginBottom: 0 } }, R("thead", null, R("tr", null, R("th", null, this.props.T("Question")), R("th", null, this.props.T("Answer")), this.props.showPrevAnswers ? R("th", null, this.props.T("Original Answer")) : undefined)), R("tbody", null, this.props.formDesign.contents.map((item) => {
return this.renderItem(item, this.state.visibilityStructure, item._id);
}))), this.props.formDesign.calculations &&
this.props.formDesign.calculations.length > 0 &&
Expand All @@ -598,7 +598,8 @@ class ResponseAnswersComponent extends AsyncLoadComponent_1.default {
formDesign: this.props.formDesign,
schema: this.props.schema,
responseRow: this.state.responseRow,
locale: this.props.locale
locale: this.props.locale,
T: this.props.T,
}))
: undefined);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ResponseDisplayComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ResponseDisplayComponent extends react_1.default.Component {
return null;
}
return R(ModalPopupComponent_1.default, {
header: "Change history",
header: this.state.T("Change history"),
size: "large",
showCloseX: true,
onClose: () => this.setState({ showArchive: false })
Expand Down
3 changes: 1 addition & 2 deletions lib/conditionUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export declare function applicableOps(lhsQuestion: any): {
id: string;
text: string;
}[];
declare function _rhsType(lhsQuestion: any, op: any): "text" | "number" | "date" | "choice" | "choices" | null;
export { _rhsType as rhsType };
export declare function getRhsType(lhsQuestion: any, op: any): "text" | "number" | "date" | "choice" | "choices" | null;
export declare function rhsChoices(lhsQuestion: Question, op: any): {
id: string;
text: string;
Expand Down
16 changes: 8 additions & 8 deletions lib/conditionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.summarizeCondition = exports.summarizeConditions = exports.validateCondition = exports.rhsChoices = exports.rhsType = exports.applicableOps = exports.compileConditions = exports.compileCondition = void 0;
exports.summarizeCondition = exports.summarizeConditions = exports.validateCondition = exports.rhsChoices = exports.getRhsType = exports.applicableOps = exports.compileConditions = exports.compileCondition = void 0;
const lodash_1 = __importDefault(require("lodash"));
const formUtils = __importStar(require("./formUtils"));
// Helpful utilities when building conditions
Expand Down Expand Up @@ -251,7 +251,7 @@ function applicableOps(lhsQuestion) {
exports.applicableOps = applicableOps;
// Gets rhs type for a question and operator.
// Can be null (for unary), "text", "number", "choice", "choices", "date", "datetime"
function _rhsType(lhsQuestion, op) {
function getRhsType(lhsQuestion, op) {
switch (op) {
case "present":
case "!present":
Expand Down Expand Up @@ -283,7 +283,7 @@ function _rhsType(lhsQuestion, op) {
throw new Error("Unknown op");
}
}
exports.rhsType = _rhsType;
exports.getRhsType = getRhsType;
// In the case of choice, returns choices for rhs (returns base localization)
// Return includes id and text for each one, suitable for a select2 control
function rhsChoices(lhsQuestion, op) {
Expand Down Expand Up @@ -322,11 +322,11 @@ function validateCondition(cond, formDesign) {
if (!cond.op) {
return false;
}
if (!lodash_1.default.contains(lodash_1.default.pluck(exports.applicableOps(lhsQuestion), "id"), cond.op)) {
if (!lodash_1.default.contains(lodash_1.default.pluck(applicableOps(lhsQuestion), "id"), cond.op)) {
return false;
}
// Check rhs
const rhsType = exports.rhsType(lhsQuestion, cond.op);
const rhsType = getRhsType(lhsQuestion, cond.op);
if (rhsType) {
if (!cond.rhs || cond.rhs.literal == null) {
return false;
Expand Down Expand Up @@ -369,7 +369,7 @@ function validateCondition(cond, formDesign) {
}
exports.validateCondition = validateCondition;
function summarizeConditions(conditions = [], formDesign, locale) {
return lodash_1.default.map(conditions, (cond) => exports.summarizeCondition(cond, formDesign, locale)).join(" and ");
return lodash_1.default.map(conditions, (cond) => summarizeCondition(cond, formDesign, locale)).join(" and ");
}
exports.summarizeConditions = summarizeConditions;
function summarizeCondition(cond, formDesign, locale) {
Expand All @@ -383,7 +383,7 @@ function summarizeCondition(cond, formDesign, locale) {
}
let str = formUtils.localizeString(lhsQuestion.text, locale);
str += " " + ((_b = getOpDetails(cond.op)) === null || _b === void 0 ? void 0 : _b.text);
const rhsType = exports.rhsType(lhsQuestion, cond.op);
const rhsType = getRhsType(lhsQuestion, cond.op);
switch (rhsType) {
case "text":
case "number":
Expand All @@ -399,7 +399,7 @@ function summarizeCondition(cond, formDesign, locale) {
str += lodash_1.default.map(cond.rhs.literal, (choice) => { var _a; return (_a = lodash_1.default.findWhere(choices, { id: choice })) === null || _a === void 0 ? void 0 : _a.text; }).join(", ");
break;
case "date":
case "datetime":
// case "datetime":
// TODO prettier
str += ` ${cond.rhs.literal}`;
break;
Expand Down
5 changes: 3 additions & 2 deletions lib/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const react_dom_1 = __importDefault(require("react-dom"));
const R = react_1.default.createElement;
require("./index");
const FormComponent_1 = __importDefault(require("./FormComponent"));
const sampleForm2_1 = __importDefault(require("./sampleForm2"));
const ResponseViewEditComponent_1 = __importDefault(require("./ResponseViewEditComponent"));
const mwater_expressions_1 = require("mwater-expressions");
const FormSchemaBuilder_1 = __importDefault(require("./FormSchemaBuilder"));
Expand Down Expand Up @@ -745,12 +746,12 @@ class DemoComponent extends react_1.default.Component {
// design = matrixFormDesign
// design = rosterFormDesign
// design = exprFormDesign
const design = cascadingRefFormDesign;
// const design = cascadingRefFormDesign as unknown as Form
// design = sampleFormAdvancedValidations
// design = require('./sampleFormRM')
// design = randomAskFormDesign
// const design = rankedDesign as unknown as Form
// const design = sampleForm2 as unknown as Form
const design = sampleForm2_1.default;
schema = new FormSchemaBuilder_1.default().addForm(schema, design);
schema = new CustomTablesetSchemaBuilder_1.CustomTablesetSchemaBuilder().addTableset(schema, cascadingRefCustomTableset);
return R("div", { className: "row" }, R("div", { className: "col-md-6" }, R(FormComponent_1.default, {
Expand Down
3 changes: 0 additions & 3 deletions lib/formUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ function prepareQuestion(q) {
case "SiteColumnQuestion":
lodash_1.default.defaults(q, { siteType: "water_point" });
break;
case "LikertQuestion":
lodash_1.default.defaults(q, { items: [], choices: [] });
break;
case "DateQuestion": // , "DateTimeQuestion"??
lodash_1.default.defaults(q, { format: "YYYY-MM-DD" });
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/siteCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ function seqToCode(seq) {
exports.seqToCode = seqToCode;
function isValid(code) {
const seq = parseInt(code.substring(0, code.length - 1));
return exports.seqToCode(seq) === code;
return seqToCode(seq) === code;
}
exports.isValid = isValid;
2 changes: 1 addition & 1 deletion lib/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export declare function formatRelativeLocation(relLoc: any, T: any): string;
/** Calculates the relative strength of a GPS signal into "none", "poor", "fair", "good" or "excellent" */
export declare function calculateGPSStrength(pos?: GeolocationPosition | null): PositionStrength;
export declare let formatGPSStrength: (pos: any, T: any) => {
class: string | undefined;
class: string;
text: any;
};
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function formatRelativeLocation(relLoc, T) {
else {
distance = relLoc.distance.toFixed(0) + " " + T("m");
}
return distance + " " + exports.getCompassBearing(relLoc.angle, T);
return distance + " " + getCompassBearing(relLoc.angle, T);
}
exports.formatRelativeLocation = formatRelativeLocation;
/** Calculates the relative strength of a GPS signal into "none", "poor", "fair", "good" or "excellent" */
Expand Down Expand Up @@ -70,7 +70,7 @@ exports.calculateGPSStrength = calculateGPSStrength;
// Format GPS strength in human-readable, Bootstrap-friendly way
let formatGPSStrength = (pos, T) => {
let text, textClass;
const strength = exports.calculateGPSStrength(pos);
const strength = calculateGPSStrength(pos);
switch (strength) {
case "none":
text = T("Waiting for GPS...");
Expand Down
36 changes: 36 additions & 0 deletions localizations.json
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,42 @@
{
"_base": "en",
"en": "Error getting image"
},
{
"_base": "en",
"en": "Calculation"
},
{
"_base": "en",
"en": "Value"
},
{
"_base": "en",
"en": "Invalid Choice"
},
{
"_base": "en",
"en": "(Invalid)"
},
{
"_base": "en",
"en": "Admin region questions are no longer supported"
},
{
"_base": "en",
"en": "Question"
},
{
"_base": "en",
"en": "Answer"
},
{
"_base": "en",
"en": "Original Answer"
},
{
"_base": "en",
"en": "Change history"
}
]
}
Loading

0 comments on commit 305e431

Please sign in to comment.