Skip to content

Commit c1e2db8

Browse files
author
ahmetkuslular
committed
FIX code review issues
1 parent f6a2827 commit c1e2db8

File tree

8 files changed

+47
-89
lines changed

8 files changed

+47
-89
lines changed

jest.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Jest configuration
2-
// https://facebook.github.io/jest/docs/en/configuration.html
31
module.exports = {
42
verbose: true,
53
automock: false,
@@ -11,6 +9,11 @@ module.exports = {
119
'!src/public/**',
1210
'!src/tools/**'
1311
],
12+
env: {
13+
production: {
14+
plugins: ['transform-es2015-modules-commonjs']
15+
}
16+
},
1417
coverageDirectory: '<rootDir>/coverage',
1518
globals: {
1619
window: true,

src/render.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import xss from 'xss';
22

33
import { matchUrlInRouteConfigs } from './universal/core/route/routeUtils';
44
import Preview from './universal/components/Preview';
5-
import { HTTP_STATUS_CODES } from './universal/utils/constants';
5+
import { BLACKLIST_OUTPUT, HTTP_STATUS_CODES } from './universal/utils/constants';
66
import metrics from './metrics';
77
import {
88
renderComponent,
@@ -13,6 +13,7 @@ import {
1313
} from './universal/service/RenderService';
1414
import Component from './universal/model/Component';
1515
import logger from './universal/utils/logger';
16+
import omit from 'lodash/omit';
1617

1718
const appConfig = require('__APP_CONFIG__');
1819

@@ -60,9 +61,9 @@ const render = async (req, res) => {
6061
scripts,
6162
activeComponent,
6263
componentName,
63-
seoState,
6464
isPreviewQuery,
65-
responseOptions
65+
responseOptions,
66+
...responseData
6667
} = renderResponse;
6768

6869
const statusCode = responseOptions?.isPartialContent
@@ -71,8 +72,8 @@ const render = async (req, res) => {
7172

7273
if (!isPreview(context.query)) {
7374
const html = renderLinksAndScripts(output, '', '');
74-
75-
res.status(statusCode).json({ html, scripts, style: links, activeComponent, seoState });
75+
const otherParams = omit(responseData, BLACKLIST_OUTPUT);
76+
res.status(statusCode).json({ html, scripts, style: links, activeComponent, ...otherParams });
7677

7778
metrics.fragmentRequestDurationMicroseconds
7879
.labels(componentName, isWithoutHTML(context.query) ? '1' : '0')

src/universal/model/Renderer.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import omit from 'lodash/omit';
22
import { isPreview, renderComponent, renderLinksAndScripts } from '../service/RenderService';
3+
import { BLACKLIST_OUTPUT } from '../utils/constants';
34

4-
const blacklistOutput = [
5-
'componentName',
6-
'fullWidth',
7-
'isMobileComponent',
8-
'isPreviewQuery',
9-
'responseOptions'
10-
];
115
export default class Renderer {
126
constructor(component, context) {
137
this.component = component;
@@ -47,7 +41,7 @@ export default class Renderer {
4741
return new Promise(resolve => {
4842
renderComponent(this.component, this.context, this.initialState).then(response => {
4943
const { output, links, fullHtml, ...rest } = response;
50-
const otherParams = omit(rest, blacklistOutput);
44+
const otherParams = omit(rest, BLACKLIST_OUTPUT);
5145
const html = renderLinksAndScripts(output, '', '');
5246

5347
resolve({

src/universal/partials/Welcome/PartialList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Welcome = () => {
1616
<Link href={item.previewUrl ? item.previewUrl : `${item.url}?preview`} target="_blank">
1717
<Name>{item.name}</Name>
1818
<Url>{item.url}</Url>
19-
<Footer>
19+
<Footer status={item.status}>
2020
<Label status={item.status}>
2121
{item.status} <Dot status={item.status} />
2222
</Label>

src/universal/partials/Welcome/styled.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ export const ListItem = styled.li`
2424
display: inline-block;
2525
vertical-align: top;
2626
height: 120px;
27-
width: 320px;
27+
width: 240px;
2828
margin: 10px;
2929
cursor: pointer;
30-
border-radius: 20px;
31-
border: 1px solid ${({ status }) => (status && STATUS_COLOR[status]) || '#8dc63f'};
30+
border-radius: 10px;
3231
3332
position: relative;
3433
background-color: #fff;
@@ -38,14 +37,14 @@ export const ListItem = styled.li`
3837
3938
:after {
4039
content: '';
41-
border-radius: 20px;
40+
border-radius: 10px;
4241
position: absolute;
4342
z-index: -1;
4443
top: 0;
4544
left: 0;
4645
width: 100%;
4746
height: 100%;
48-
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
47+
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
4948
opacity: 0;
5049
-webkit-transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);
5150
transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);
@@ -104,7 +103,7 @@ export const Footer = styled.span`
104103
right: 0;
105104
width: 100%;
106105
padding: 20px;
107-
border-top: 1px solid #eee;
106+
border-top: 1px solid ${({ status }) => (status && STATUS_COLOR[status]) || '#eeeeee'}50;
108107
font-size: 13px;
109108
`;
110109

src/universal/service/RenderService.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ const isWithoutHTML = query => {
5454
};
5555

5656
const isPreview = query => {
57-
return query.preview === '';
57+
if (query.preview || query.preview === '') {
58+
return true;
59+
}
60+
return false;
5861
};
5962

6063
const getPreviewLayout = query => {
61-
if (query?.previewLayout) {
62-
return query?.previewLayout;
64+
if (isPreview(query)) {
65+
return query?.preview;
6366
}
6467

6568
return '';
@@ -70,7 +73,7 @@ const isWithoutState = query => {
7073
};
7174

7275
const isRequestDispatcher = query => {
73-
return query.requestDispathcer === '' || query.requestDispathcer !== 'false';
76+
return query.requestDispatcher === '' || query.requestDispatcher !== 'false';
7477
};
7578

7679
const renderComponent = async (component, context, predefinedInitialState = null) => {

src/universal/service/getStates.js

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,31 @@
1-
import omit from 'lodash/omit';
2-
import camelCase from 'lodash/camelCase';
3-
4-
const blacklistFunctionName = [
5-
'getServerSideProps',
6-
'getServicesWithMultiple',
7-
'getInitialStateWithMultiple',
8-
'setDependencies',
9-
'setSeoState',
10-
'setRedirection',
11-
'setPageData'
12-
];
13-
14-
const getCustomSetters = (component, context, data) => {
15-
const pattern = new RegExp(`^set`);
16-
const functions = omit(component, blacklistFunctionName);
1+
const getResponseData = (component, context, data) => {
172
let result = {};
183

19-
Object.entries(functions).forEach(entity => {
20-
const [name, method] = entity;
21-
const isValidName = pattern.test(name);
22-
if (isValidName) {
23-
const propertyName = camelCase(name.replace(pattern, ''));
24-
let value = null;
25-
if (typeof method === 'function') {
26-
value = method(context, data);
27-
} else {
28-
value = method;
29-
}
30-
31-
if (value) {
32-
result = {
33-
...result,
34-
[propertyName]: value
35-
};
36-
}
4+
if (component?.setResponseData) {
5+
if (typeof component.setResponseData === 'function') {
6+
result = component.setResponseData(context, data);
7+
} else {
8+
result = component.setResponseData;
379
}
38-
});
10+
}
3911

4012
return result;
4113
};
4214

4315
const getStates = async (component, context, predefinedInitialState) => {
4416
const initialState = predefinedInitialState || { data: {} };
4517
let subComponentFiles = [];
46-
let seoState = {};
4718
let responseOptions = {};
48-
let dependencies = [];
49-
let redirection = null;
50-
51-
if (component.setDependencies) {
52-
dependencies = component.setDependencies(context);
53-
}
19+
const responseData = getResponseData(component, context, initialState.data);
5420

5521
if (context.isWithoutState) {
56-
return { initialState, seoState, dependencies, subComponentFiles, responseOptions };
22+
return { initialState, subComponentFiles, responseOptions, ...responseData };
5723
}
5824

59-
if (!predefinedInitialState && component.getServerSideProps) {
25+
if (!predefinedInitialState && component?.getServerSideProps) {
6026
initialState.data = await component.getServerSideProps(context);
6127
}
6228

63-
if (component?.setSeoState) {
64-
seoState = component.setSeoState(initialState?.data) || {};
65-
}
66-
6729
if (initialState?.data?.subComponentFiles) {
6830
subComponentFiles = initialState?.data?.subComponentFiles || [];
6931
}
@@ -72,24 +34,11 @@ const getStates = async (component, context, predefinedInitialState) => {
7234
responseOptions = initialState?.data?.responseOptions || {};
7335
}
7436

75-
if (component.setRedirection) {
76-
redirection = component.setRedirection(context, initialState.data);
77-
}
78-
79-
if (component.setPageData) {
80-
redirection = component.setPageData(context, initialState.data);
81-
}
82-
83-
const setters = getCustomSetters(component, context, initialState.data);
84-
8537
return {
8638
initialState,
87-
seoState,
8839
subComponentFiles,
8940
responseOptions,
90-
dependencies,
91-
redirection,
92-
...setters
41+
...responseData
9342
};
9443
};
9544

src/universal/utils/constants.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ const SERVICES = Object.freeze(
2525
}, {})
2626
);
2727

28+
const BLACKLIST_OUTPUT = [
29+
'componentName',
30+
'fullWidth',
31+
'isMobileComponent',
32+
'isPreviewQuery',
33+
'responseOptions'
34+
];
35+
2836
export {
2937
HTTP_STATUS_CODES,
3038
WINDOW_GLOBAL_PARAMS,
3139
JSON_CONTENT_TYPE,
3240
CONTENT_TYPE_HEADER,
3341
REQUEST_TYPES_WITH_BODY,
34-
SERVICES
42+
SERVICES,
43+
BLACKLIST_OUTPUT
3544
};

0 commit comments

Comments
 (0)