Skip to content

Commit

Permalink
Merge pull request #266 from lalithkota/17.0-develop
Browse files Browse the repository at this point in the history
Program Reg Info Json to dict fix. Nested json fix
  • Loading branch information
shibu-narayanan authored Oct 24, 2024
2 parents b4aec7d + 0204732 commit 73dd294
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 83 deletions.
95 changes: 40 additions & 55 deletions g2p_program_registrant_info/static/src/js/g2p_additional_info.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,56 @@
/** @odoo-module **/

import {markup, onMounted, useExternalListener, useState} from "@odoo/owl";
import {markup, useRef, useState} from "@odoo/owl";
import {TextField} from "@web/views/fields/text/text_field";
import {_t} from "@web/core/l10n/translation";
import {registry} from "@web/core/registry";
import {useService} from "@web/core/utils/hooks";

export class G2PAdditionalInfoWidget extends TextField {
export class G2PProgramRegInfoComponent extends TextField {
static template = "g2p_program_reg_info_tpl";

setup() {
super.setup();
this.state = useState({recordClicked: false, noValue: false});
this.state = useState({editingMode: false});
this.textareaRef = useRef("textarea");
this.notification = useService("notification");
onMounted(() => this.validateValue());
useExternalListener(window, "click", this.onclick);
useExternalListener(window, "mouseup", this.onMouseup);
}

validateValue() {
const val = this.props.record.data.program_registrant_info;

if (!val) {
this.state.noValue = true;
} else if (typeof val === "string") {
if (val.charAt(0) === "{" && val.charAt(val.length - 1) === "}") {
this.state.noValue = false;
} else {
this.state.noValue = true;
}
} else if (typeof val === "object") {
this.state.noValue = false;
} else {
this.state.noValue = true;
editButtonClick() {
const val = this.props.record.data[this.props.name];
if (typeof val !== "string") {
this.props.record.data[this.props.name] = JSON.stringify(val);
}
this.state.editingMode = true;
}

onclick(event) {
if (this.editingRecord && event.target.closest(".json-widget")) {
this.state.recordClicked = true;
this.state.noValue = true;
doneButtonClick() {
let val = null;
try {
val = JSON.parse(this.textareaRef.el.value);
} catch (err) {
this.notification.add(_t("Program Info"), {
title: _t("Invalid Json Value"),
type: "danger",
});
console.error(err);
return;
}
this.validateValue();
}

onMouseup(ev) {
if (!ev.target.closest(".o_field_g2p_addl_info_widget textarea")) {
this.state.recordClicked = false;
this.state.noValue = false;
try {
this.props.record.update({[this.props.name]: val});
this.state.editingMode = false;
} catch (err) {
this.notification.add(_t("Program Info"), {
title: _t("Error Updating Json"),
type: "danger",
});
console.error(err);
}
this.validateValue();
}

get editingRecord() {
return !this.props.readonly;
}

renderjson() {
const valuesJsonOrig = this.props.record.data[this.props.name];
try {
const valuesJsonOrig = this.props.record.data.program_registrant_info;
if (typeof valuesJsonOrig === "string" || valuesJsonOrig instanceof String) {
const parsedValue = this.flattenJson(valuesJsonOrig);
return parsedValue;
}

if (Array.isArray(valuesJsonOrig)) {
const sectionsJson = {};
valuesJsonOrig.forEach((element) => {
Expand All @@ -77,25 +66,23 @@ export class G2PAdditionalInfoWidget extends TextField {
title: _t("Invalid Json Value"),
type: "danger",
});
this.state.recordClicked = true;
this.state.editingMode = true;
return {};
}
}

flattenJson(object) {
// Const jsonObject = JSON.parse(object);
let jsonObject = object;
if (typeof object === "string") {
jsonObject = JSON.parse(object);
}
for (const key in jsonObject) {
if (!jsonObject[key]) continue;
if (Array.isArray(jsonObject[key])) {
// Handle arrays by joining their elements with a comma and space
jsonObject[key] = jsonObject[key].join(", ");
if (!jsonObject[key]) {
continue;
} else if (
Array.isArray(jsonObject[key]) &&
jsonObject[key].length > 0 &&
typeof jsonObject[key][0] === "object" &&
"document_id" in jsonObject[key][0] &&
"document_slug" in jsonObject[key][0]
) {
Expand All @@ -117,10 +104,8 @@ export class G2PAdditionalInfoWidget extends TextField {
}
}

G2PAdditionalInfoWidget.template = "addl_info_table";

export const G2PAdditionalInfoWidgets = {
component: G2PAdditionalInfoWidget,
supportedTypes: ["json", "text", "html"],
export const g2pProgramRegInfoWidget = {
component: G2PProgramRegInfoComponent,
supportedTypes: ["jsonb", "text", "html"],
};
registry.category("fields").add("g2p_addl_info_widget", G2PAdditionalInfoWidgets);
registry.category("fields").add("g2p_program_reg_info", g2pProgramRegInfoWidget);
64 changes: 42 additions & 22 deletions g2p_program_registrant_info/static/src/xml/g2p_additional_info.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="addl_info_table">
<t t-if="! state.recordClicked and ! state.noValue">
<table
class="o_group o_inner_group table table-bordered json-widget"
border="1"
style="border-collapse:collapse"
>
<tbody>
<t t-foreach="Object.entries(this.renderjson())" t-as="key" t-key="key">
<tr>
<t t-name="g2p_program_reg_info_tpl">
<t t-if="!state.editingMode">
<t t-set="jsonToRender" t-value="this.renderjson()" />
<t t-call="g2p_program_reg_info_table_tpl" />
<button
class="btn btn-success"
name="g2p_program_reg_info_edit_button"
t-on-click="editButtonClick"
>Edit</button>
</t>
<t t-else="">
<t t-call="web.TextField" />
<br />
<button
class="btn btn-success"
name="g2p_program_reg_info_save_button"
t-on-click="doneButtonClick"
>Done</button>
</t>
</t>
<t t-name="g2p_program_reg_info_table_tpl">
<table class="o_group o_inner_group table table-bordered" border="1" style="border-collapse:collapse">
<tbody>
<t t-set="isArray" t-value="Array.isArray(jsonToRender)" />
<t t-foreach="Object.entries(jsonToRender)" t-as="key" t-key="key[0]">
<tr>
<t t-if="!isArray">
<td class="o_td_label">
<label class="o_form_label o_quick_editable">
<label class="o_form_label">
<strong>
<t t-esc="key[0]" />
</strong>
</label>
</td>
<td>
<span class="o_field_widget o_quick_editable">
</t>
<td>
<t t-if="typeof key[1] === 'object' || Array.isArray(key[1])">
<t t-set="jsonToRender" t-value="key[1]" />
<t t-call="g2p_program_reg_info_table_tpl" />
</t>
<t t-else="">
<span class="o_field_widget">
<t t-esc="key[1]" />
</span>
</td>
</tr>
</t>
</tbody>
</table>
</t>
<t t-else="">
<t t-call="web.TextField" />
</t>
</t>
</td>
</tr>
</t>
</tbody>
</table>
</t>
</templates>
2 changes: 1 addition & 1 deletion g2p_program_registrant_info/views/program_membership.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<field name="create_date" string="Date Submitted" />
<!-- <field
name="program_registrant_info"
widget="g2p_addl_info_widget"
widget="g2p_program_reg_info"
invisible="1"
/> -->
<button
Expand Down
6 changes: 3 additions & 3 deletions g2p_program_registrant_info/views/program_registrant_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Part of OpenG2P. See LICENSE file for full copyright and licensing details.
/>
<field name="program_id" />
<field name="state" />
<field name="program_registrant_info" widget="g2p_addl_info_widget" />
<field name="program_registrant_info" widget="g2p_program_reg_info" />
</tree>
</field>
</page>
Expand Down Expand Up @@ -58,7 +58,7 @@ Part of OpenG2P. See LICENSE file for full copyright and licensing details.
<field name="create_date" column_invisible="1" />
<field name="program_id" />
<field name="state" />
<field name="program_registrant_info" widget="g2p_addl_info_widget" />
<field name="program_registrant_info" widget="g2p_program_reg_info" />
</tree>
</field>
</page>
Expand Down Expand Up @@ -92,7 +92,7 @@ Part of OpenG2P. See LICENSE file for full copyright and licensing details.
</h2>
</div>
<group colspan="4" col="4">
<field name="program_registrant_info" nolabel="1" widget="g2p_addl_info_widget" />
<field name="program_registrant_info" nolabel="1" widget="g2p_program_reg_info" />
</group>
</sheet>
</form>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from datetime import datetime
from odoo.addons.g2p_programs_rest_api.models import program_membership
from odoo.addons.g2p_registry_rest_api.models import naive_orm_model


class ProgramRegistrantInfoOut(naive_orm_model.NaiveOrmModel):
state: str | None = ""
program_registrant_info: dict | list[dict] = {}
program_registrant_info: dict | list | None = None
create_date: datetime


class ProgramMembershipIn(
program_membership.RegistrantProgramMembershipIn,
extends=program_membership.RegistrantProgramMembershipIn,
):
program_registrant_info: dict | list[dict] | None = {}
program_registrant_info: dict | list | None = None


class ProgramMembershipOut(
Expand Down

0 comments on commit 73dd294

Please sign in to comment.