ktmud commented on a change in pull request #12793:
URL: https://github.com/apache/superset/pull/12793#discussion_r565811586



##########
File path: 
superset-frontend/src/explore/components/controls/DateFilterControl/utils.ts
##########
@@ -60,6 +61,19 @@ const defaultCustomRange: CustomRangeType = {
 };
 const SPECIFIC_MODE = ['specific', 'today', 'now'];
 
+export const dttmToMoment = (dttm: string): Moment => {
+  if (dttm === 'now') {
+    return moment().utc().startOf('second');
+  }
+  if (dttm === 'today') {
+    return moment().utc().startOf('day');
+  }
+  return moment(dttm);
+};
+
+export const dttmToString = (dttm: string): string =>
+  dttmToMoment(dttm).format(MOMENT_FORMAT);
+
 export const customTimeRangeDecode = (

Review comment:
       Style nit: can we break `utils.ts` into separate files under a `utils` 
folder? These functions are complex enough to warrant a separate file.

##########
File path: 
superset-frontend/src/explore/components/controls/DateFilterControl/utils.ts
##########
@@ -183,31 +197,27 @@ export const customTimeRangeEncode = (customRange: 
CustomRangeType): string => {
   } = { ...customRange };
   // specific : specific
   if (SPECIFIC_MODE.includes(sinceMode) && SPECIFIC_MODE.includes(untilMode)) {
-    const since = sinceMode === 'specific' ? sinceDatetime : sinceMode;
-    const until = untilMode === 'specific' ? untilDatetime : untilMode;
+    const since = sinceMode === 'specific' ? dttmToString(sinceDatetime) : 
sinceMode; // eslint-disable-line

Review comment:
       ```ts
   export const customTimeRangeEncode = (customRange: CustomRangeType): string 
=> {
     const {
       sinceDatetime,
       sinceMode,
       sinceGrain,
       sinceGrainValue,
       untilDatetime,
       untilMode,
       untilGrain,
       untilGrainValue,
       anchorValue,
     } = { ...customRange };
     let since: string = sinceMode;
     let until: string = untilMode;
   
     if (since === 'specific') {
       since = dttmToString(sinceDatetime);
     }
     if (until === 'specific') {
       until = dttmToString(untilDatetime);
     }
   
     if (since === 'relative' && until === 'relative') {
       since = `DATEADD(DATETIME("${anchorValue}"), ${-Math.abs(
         sinceGrainValue,
       )}, ${sinceGrain})`;
       until = `DATEADD(DATETIME("${anchorValue}"), ${untilGrainValue}, 
${untilGrain})`;
     }
   
     if (since === 'relative') {
       since = `DATEADD(DATETIME("${until}"), ${-Math.abs(
         sinceGrainValue,
       )}, ${sinceGrain})`;
     }
   
     if (until === 'relative') {
       until = `DATEADD(DATETIME("${since}"), ${untilGrainValue}, 
${untilGrain})`;
     }
   
     return `${since} : ${until}`;
   };
   ```
   
   @zhaoyongjie would this be cleaner? It passes all your test cases.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to