Skip to content

Commit 352b747

Browse files
authored
Merge pull request #44 from dunglas/improve-utils
[react] Improve utils
2 parents 2427db0 + e815d15 commit 352b747

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

templates/react/utils/helpers.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,17 @@ export function itemToLinks(items) {
77
}
88

99
function createLink(item) {
10-
if (typeof(item) === 'string' && item.includes(API_PATH) > 0) {
11-
let route = null;
12-
const routeTest = item.replace(API_PATH, '');
13-
if( routeTest[0] === '/') {
14-
route = item.replace(API_PATH, '').split('/')[1];
15-
} else {
16-
route = item.replace(API_PATH, '').split('/')[0];
17-
}
18-
return (
19-
<span key={item}>
20-
<Link to={`/${route}/show/${encodeURIComponent(item)}`}>
21-
{item}
22-
</Link>
23-
<br/>
24-
</span>
25-
);
10+
if ('string' !== typeof(item) || !item.includes(API_PATH)) {
11+
return <span key={item}>{item}<br/></span>;
2612
}
27-
return <span key={item}>{item}<br/></span>;
13+
14+
const routeWithoutPrefix = item.replace(API_PATH, '');
15+
const splittedRoute = routeWithoutPrefix.split('/');
16+
const route = '/' === routeWithoutPrefix[0] ? splittedRoute[1] : splittedRoute[0];
17+
18+
return <span><Link key={item} to={`/${route}/show/${encodeURIComponent(item)}`}>{item}</Link><br/></span>;
2819
}
2920

3021
export function paginationRoute(item) {
31-
return '/' + item.split('/').splice(-1,1);
22+
return '/' + item.split('/').splice(-1, 1);
3223
}

0 commit comments

Comments
 (0)