date-fns#addWeeks JavaScript Examples

The following examples show how to use date-fns#addWeeks. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useGrid.js    From react-nice-dates with MIT License 5 votes vote down vote up
getEndDate = (date, locale) => endOfWeek(addWeeks(endOfMonth(date), 6 - rowsInMonth(date, locale)), { locale })
Example #2
Source File: index.js    From neutron with Mozilla Public License 2.0 4 votes vote down vote up
DialogPauseNotifications = () => {
  // const classes = useStyles();
  const dispatch = useDispatch();

  const pauseNotificationsInfo = useSelector((state) => state.notifications.pauseNotificationsInfo);
  const showDateTimePicker = useSelector((state) => state.notifications.showDateTimePicker);

  const shouldPauseNotifications = pauseNotificationsInfo !== null;

  const quickShortcuts = [
    {
      name: '15 minutes',
      calcDate: () => addMinutes(new Date(), 15),
    },
    {
      name: '30 minutes',
      calcDate: () => addMinutes(new Date(), 30),
    },
    {
      name: '45 minutes',
      calcDate: () => addMinutes(new Date(), 45),
    },
    {
      name: '1 hour',
      calcDate: () => addHours(new Date(), 1),
    },
    {
      name: '2 hours',
      calcDate: () => addHours(new Date(), 2),
    },
    {
      name: '4 hours',
      calcDate: () => addHours(new Date(), 4),
    },
    {
      name: '6 hours',
      calcDate: () => addHours(new Date(), 6),
    },
    {
      name: '8 hours',
      calcDate: () => addHours(new Date(), 8),
    },
    {
      name: '10 hours',
      calcDate: () => addHours(new Date(), 8),
    },
    {
      name: '12 hours',
      calcDate: () => addHours(new Date(), 12),
    },
    {
      name: 'Until tomorrow',
      calcDate: () => addDays(new Date(), 1),
    },
    {
      name: 'Until next week',
      calcDate: () => addWeeks(new Date(), 1),
    },
  ];

  const pauseNotif = (tilDate) => {
    requestSetPreference('pauseNotifications', `pause:${tilDate.toString()}`);
    requestShowNotification({
      title: 'Notifications paused',
      body: `Notifications paused until ${formatDate(tilDate)}.`,
    });
    getCurrentWindow().close();
  };

  const renderList = () => {
    if (shouldPauseNotifications) {
      return (
        <List
          dense
          disablePadding
        >
          <ListItem sx={{
            background: `url(${nightBackgroundPng})`,
            height: 210,
            backgroundSize: 400,
            alignItems: 'flex-end',
          }}
          >
            <ListItemText
              primary={`Notifications paused until
              ${formatDate(new Date(pauseNotificationsInfo.tilDate))}.`}
              sx={{
                color: 'common.white',
              }}
            />
          </ListItem>
          <ListItem button>
            <ListItemText
              primary="Resume notifications"
              onClick={() => {
                if (pauseNotificationsInfo.reason === 'scheduled') {
                  requestSetPreference('pauseNotifications', `resume:${pauseNotificationsInfo.tilDate}`);
                } else if (pauseNotificationsInfo.schedule
                  && new Date() < new Date(pauseNotificationsInfo.schedule.to)) {
                  requestSetPreference('pauseNotifications', `resume:${pauseNotificationsInfo.schedule.to}`);
                } else {
                  requestSetPreference('pauseNotifications', null);
                }
                requestShowNotification({
                  title: 'Notifications resumed',
                  body: 'Notifications are now resumed.',
                });
                getCurrentWindow().close();
              }}
            />
          </ListItem>
          {pauseNotificationsInfo.reason !== 'scheduled' && (
            <>
              <Divider />
              <ListItem
                button
                onClick={() => {
                  const template = quickShortcuts.map((shortcut) => ({
                    label: shortcut.name,
                    click: () => pauseNotif(shortcut.calcDate()),
                  }));
                  template.push({
                    label: 'Custom',
                    click: () => dispatch(updateShowDateTimePicker(true)),
                  });
                  const menu = Menu.buildFromTemplate(template);
                  menu.popup({
                    window: getCurrentWindow(),
                  });
                }}
              >
                <ListItemText primary="Adjust time" />
                <ChevronRightIcon color="action" />
              </ListItem>
            </>
          )}
          <Divider />
          <ListItem button>
            <ListItemText
              primary={pauseNotificationsInfo.reason === 'scheduled' ? 'Adjust schedule...' : 'Pause notifications by schedule...'}
              onClick={() => {
                requestShowPreferencesWindow('notifications');
                getCurrentWindow().close();
              }}
            />
          </ListItem>
        </List>
      );
    }

    return (
      <List
        dense
        disablePadding
        subheader={<ListSubheader component="div">Pause notifications</ListSubheader>}
      >
        {quickShortcuts.map((shortcut) => (
          <ListItem
            button
            key={shortcut.name}
            onClick={() => pauseNotif(shortcut.calcDate())}
          >
            <ListItemText primary={shortcut.name} />
          </ListItem>
        ))}
        <ListItem
          button
          onClick={() => dispatch(updateShowDateTimePicker(true))}
        >
          <ListItemText primary="Custom..." />
        </ListItem>
        <Divider />
        <DateTimePicker
          value={new Date()}
          onChange={pauseNotif}
          label="Custom"
          open={showDateTimePicker}
          onOpen={() => dispatch(updateShowDateTimePicker(true))}
          onClose={() => dispatch(updateShowDateTimePicker(false))}
          disablePast
          showTodayButton
          // eslint-disable-next-line react/jsx-props-no-spreading
          renderInput={() => (
            <ListItem button>
              <ListItemText
                primary="Pause notifications by schedule..."
                onClick={() => {
                  requestShowPreferencesWindow('notifications');
                  getCurrentWindow().close();
                }}
              />
            </ListItem>
          )}
        />
      </List>
    );
  };

  return (
    <>
      {renderList()}
    </>
  );
}