Skip to content

Commit 59c4268

Browse files
committed
Add Reactjs modal form clear collection values
Signed-off-by: Hamza JGUERIM <hamzajg@gmail.com>
1 parent 78fb17b commit 59c4268

File tree

8 files changed

+57
-9
lines changed

8 files changed

+57
-9
lines changed

src/main/resources/codegen/reactjs/AggregateDetail.ftl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,40 @@ const ${fns.capitalize(aggregate.aggregateName)} = () => {
110110
}
111111
setCurrentModal(null);
112112
}, []);
113-
114113
<#list aggregate.methods as method>
115114
<#if !method.useFactory >
115+
116116
const _${method.name} = useCallback((e) => {
117117
console.log('showing ${method.name} modal');
118118
const form = {
119119
id: item.id,
120120
<#list method.parameters as p>
121-
${p}: item.${p}<#if p?has_next>,</#if>
121+
${p}: copyAndClearObject(item.${p})<#if p?has_next>,</#if>
122122
</#list>
123123
};
124124
setCurrentModal(<${aggregate.aggregateName}${fns.capitalize(method.name)} id={id} defaultForm={form} complete={onModalActionComplete}/>);
125125
}, [id, item, onModalActionComplete]);
126126
</#if>
127127
</#list>
128+
<#if aggregate.methods?filter(method -> !method.useFactory)?has_content >
129+
130+
const copyAndClearObject = (obj) => {
131+
if(obj === undefined || !Array.isArray(obj))
132+
return obj;
133+
return clearObjectValues(JSON.parse(JSON.stringify(obj)))
134+
}
135+
136+
const clearObjectValues = (objToClear) => {
137+
Object.keys(objToClear).forEach((param) => {
138+
if (Array.isArray(objToClear[param]) || (objToClear[param]).toString() === "[object Object]") {
139+
clearObjectValues(objToClear[param]);
140+
} else {
141+
objToClear[param] = undefined;
142+
}
143+
})
144+
return objToClear;
145+
};
146+
</#if>
128147

129148
useEffect(() => {
130149
setLoading(true);

src/test/resources/text-expectations/reactjs/author-detail-without-change-rank.text

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const Author = () => {
3434
setCurrentModal(null);
3535
}, []);
3636

37-
3837
useEffect(() => {
3938
setLoading(true);
4039
loadItem(id);

src/test/resources/text-expectations/reactjs/author-detail.text

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,28 @@ const Author = () => {
3939
console.log('showing changeRank modal');
4040
const form = {
4141
id: item.id,
42-
rank: item.rank
42+
rank: copyAndClearObject(item.rank)
4343
};
4444
setCurrentModal(<AuthorChangeRank id={id} defaultForm={form} complete={onModalActionComplete}/>);
4545
}, [id, item, onModalActionComplete]);
4646

47+
const copyAndClearObject = (obj) => {
48+
if(obj === undefined || !Array.isArray(obj))
49+
return obj;
50+
return clearObjectValues(JSON.parse(JSON.stringify(obj)))
51+
}
52+
53+
const clearObjectValues = (objToClear) => {
54+
Object.keys(objToClear).forEach((param) => {
55+
if (Array.isArray(objToClear[param]) || (objToClear[param]).toString() === "[object Object]") {
56+
clearObjectValues(objToClear[param]);
57+
} else {
58+
objToClear[param] = undefined;
59+
}
60+
})
61+
return objToClear;
62+
};
63+
4764
useEffect(() => {
4865
setLoading(true);
4966
loadItem(id);

src/test/resources/text-expectations/reactjs/catalog-detail.text

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,28 @@ const Catalog = () => {
3939
console.log('showing addItem modal');
4040
const form = {
4141
id: item.id,
42-
items: item.items
42+
items: copyAndClearObject(item.items)
4343
};
4444
setCurrentModal(<CatalogAddItem id={id} defaultForm={form} complete={onModalActionComplete}/>);
4545
}, [id, item, onModalActionComplete]);
4646

47+
const copyAndClearObject = (obj) => {
48+
if(obj === undefined || !Array.isArray(obj))
49+
return obj;
50+
return clearObjectValues(JSON.parse(JSON.stringify(obj)))
51+
}
52+
53+
const clearObjectValues = (objToClear) => {
54+
Object.keys(objToClear).forEach((param) => {
55+
if (Array.isArray(objToClear[param]) || (objToClear[param]).toString() === "[object Object]") {
56+
clearObjectValues(objToClear[param]);
57+
} else {
58+
objToClear[param] = undefined;
59+
}
60+
})
61+
return objToClear;
62+
};
63+
4764
useEffect(() => {
4865
setLoading(true);
4966
loadItem(id);

src/test/resources/text-expectations/reactjs/order-detail.text

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const Order = () => {
3434
setCurrentModal(null);
3535
}, []);
3636

37-
3837
useEffect(() => {
3938
setLoading(true);
4039
loadItem(id);

src/test/resources/text-expectations/reactjs/proposal-detail.text

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const Proposal = () => {
3434
setCurrentModal(null);
3535
}, []);
3636

37-
3837
useEffect(() => {
3938
setLoading(true);
4039
loadItem(id);

src/test/resources/text-expectations/reactjs/proposals-detail.text

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const Proposal = () => {
3434
setCurrentModal(null);
3535
}, []);
3636

37-
3837
useEffect(() => {
3938
setLoading(true);
4039
loadItem(id);

src/test/resources/text-expectations/reactjs/user-detail.text

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const User = () => {
3434
setCurrentModal(null);
3535
}, []);
3636

37-
3837
useEffect(() => {
3938
setLoading(true);
4039
loadItem(id);

0 commit comments

Comments
 (0)