Skip to content

Commit

Permalink
Add Reactjs modal form clear collection values
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza JGUERIM <hamzajg@gmail.com>
  • Loading branch information
hamzajg committed Sep 4, 2021
1 parent 78fb17b commit 59c4268
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 9 deletions.
23 changes: 21 additions & 2 deletions src/main/resources/codegen/reactjs/AggregateDetail.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,40 @@ const ${fns.capitalize(aggregate.aggregateName)} = () => {
}
setCurrentModal(null);
}, []);

<#list aggregate.methods as method>
<#if !method.useFactory >

const _${method.name} = useCallback((e) => {
console.log('showing ${method.name} modal');
const form = {
id: item.id,
<#list method.parameters as p>
${p}: item.${p}<#if p?has_next>,</#if>
${p}: copyAndClearObject(item.${p})<#if p?has_next>,</#if>
</#list>
};
setCurrentModal(<${aggregate.aggregateName}${fns.capitalize(method.name)} id={id} defaultForm={form} complete={onModalActionComplete}/>);
}, [id, item, onModalActionComplete]);
</#if>
</#list>
<#if aggregate.methods?filter(method -> !method.useFactory)?has_content >

const copyAndClearObject = (obj) => {
if(obj === undefined || !Array.isArray(obj))
return obj;
return clearObjectValues(JSON.parse(JSON.stringify(obj)))
}

const clearObjectValues = (objToClear) => {
Object.keys(objToClear).forEach((param) => {
if (Array.isArray(objToClear[param]) || (objToClear[param]).toString() === "[object Object]") {
clearObjectValues(objToClear[param]);
} else {
objToClear[param] = undefined;
}
})
return objToClear;
};
</#if>

useEffect(() => {
setLoading(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const Author = () => {
setCurrentModal(null);
}, []);


useEffect(() => {
setLoading(true);
loadItem(id);
Expand Down
19 changes: 18 additions & 1 deletion src/test/resources/text-expectations/reactjs/author-detail.text
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,28 @@ const Author = () => {
console.log('showing changeRank modal');
const form = {
id: item.id,
rank: item.rank
rank: copyAndClearObject(item.rank)
};
setCurrentModal(<AuthorChangeRank id={id} defaultForm={form} complete={onModalActionComplete}/>);
}, [id, item, onModalActionComplete]);

const copyAndClearObject = (obj) => {
if(obj === undefined || !Array.isArray(obj))
return obj;
return clearObjectValues(JSON.parse(JSON.stringify(obj)))
}

const clearObjectValues = (objToClear) => {
Object.keys(objToClear).forEach((param) => {
if (Array.isArray(objToClear[param]) || (objToClear[param]).toString() === "[object Object]") {
clearObjectValues(objToClear[param]);
} else {
objToClear[param] = undefined;
}
})
return objToClear;
};

useEffect(() => {
setLoading(true);
loadItem(id);
Expand Down
19 changes: 18 additions & 1 deletion src/test/resources/text-expectations/reactjs/catalog-detail.text
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,28 @@ const Catalog = () => {
console.log('showing addItem modal');
const form = {
id: item.id,
items: item.items
items: copyAndClearObject(item.items)
};
setCurrentModal(<CatalogAddItem id={id} defaultForm={form} complete={onModalActionComplete}/>);
}, [id, item, onModalActionComplete]);

const copyAndClearObject = (obj) => {
if(obj === undefined || !Array.isArray(obj))
return obj;
return clearObjectValues(JSON.parse(JSON.stringify(obj)))
}

const clearObjectValues = (objToClear) => {
Object.keys(objToClear).forEach((param) => {
if (Array.isArray(objToClear[param]) || (objToClear[param]).toString() === "[object Object]") {
clearObjectValues(objToClear[param]);
} else {
objToClear[param] = undefined;
}
})
return objToClear;
};

useEffect(() => {
setLoading(true);
loadItem(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const Order = () => {
setCurrentModal(null);
}, []);


useEffect(() => {
setLoading(true);
loadItem(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const Proposal = () => {
setCurrentModal(null);
}, []);


useEffect(() => {
setLoading(true);
loadItem(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const Proposal = () => {
setCurrentModal(null);
}, []);


useEffect(() => {
setLoading(true);
loadItem(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const User = () => {
setCurrentModal(null);
}, []);


useEffect(() => {
setLoading(true);
loadItem(id);
Expand Down

0 comments on commit 59c4268

Please sign in to comment.