0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Wired Timezone settings to use real data

refs https://github.com/TryGhost/Team/issues/3150

- wires Timezone setting to read list of timezones and allow editing/saving new value
- handles read/write of real timezone setting from settings context
- uses `@tryghost/timezone-data` to fetch list of all timezones
- adds typings.d.ts to handle missing types for @tryghost/timezone-data
This commit is contained in:
Rishabh 2023-05-19 18:13:47 +05:30
parent a420adf684
commit 7dd8628b40
4 changed files with 62 additions and 26 deletions

View file

@ -27,6 +27,7 @@
}, },
"scripts": { "scripts": {
"dev": "concurrently \"vite preview\" \"vite build --watch\"", "dev": "concurrently \"vite preview\" \"vite build --watch\"",
"dev:start": "vite",
"build": "tsc && vite build", "build": "tsc && vite build",
"lint": "yarn run lint:js", "lint": "yarn run lint:js",
"lint:js": "eslint --ext .js,.ts,.cjs,.tsx --cache src test", "lint:js": "eslint --ext .js,.ts,.cjs,.tsx --cache src test",
@ -40,7 +41,8 @@
}, },
"dependencies": { "dependencies": {
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0",
"@tryghost/timezone-data": "0.3.0"
}, },
"devDependencies": { "devDependencies": {
"@storybook/addon-essentials": "7.0.12", "@storybook/addon-essentials": "7.0.12",

View file

@ -17,7 +17,7 @@ interface SettingGroupProps {
} }
const SettingGroup: React.FC<SettingGroupProps> = ({title, description, state, customHeader, customButtons, children, onStateChange, onSave}) => { const SettingGroup: React.FC<SettingGroupProps> = ({title, description, state, customHeader, customButtons, children, onStateChange, onSave}) => {
const handleEdit = () => { const handleEdit = () => {
if (onStateChange) { if (onStateChange) {
onStateChange('edit'); onStateChange('edit');
} }
@ -44,11 +44,11 @@ const SettingGroup: React.FC<SettingGroupProps> = ({title, description, state, c
case 'edit': case 'edit':
styles = 'border-grey-500'; styles = 'border-grey-500';
break; break;
case 'unsaved': case 'unsaved':
styles = 'border-green'; styles = 'border-green';
break; break;
default: default:
styles = 'border-grey-200'; styles = 'border-grey-200';
break; break;
@ -84,9 +84,9 @@ const SettingGroup: React.FC<SettingGroupProps> = ({title, description, state, c
return ( return (
<div className={`flex flex-col gap-6 rounded border p-5 md:p-7 ${styles}`}> <div className={`flex flex-col gap-6 rounded border p-5 md:p-7 ${styles}`}>
{customHeader ? customHeader : {customHeader ? customHeader :
<SettingGroupHeader description={description} title={title!}> <SettingGroupHeader description={description} title={title!}>
{customButtons ? customButtons : {customButtons ? customButtons :
<ButtonGroup buttons={state === 'view' ? viewButtons : editButtons} link={true} />} <ButtonGroup buttons={state === 'view' ? viewButtons : editButtons} link={true} />}
</SettingGroupHeader> </SettingGroupHeader>
} }

View file

@ -1,14 +1,21 @@
import ButtonGroup from '../../../admin-x-ds/global/ButtonGroup'; import Dropdown from '../../../admin-x-ds/global/Dropdown';
import React, {useContext, useEffect, useState} from 'react'; import React, {useContext, useEffect, useState} from 'react';
import SettingGroup from '../../../admin-x-ds/settings/SettingGroup'; import SettingGroup, {TSettingGroupStates} from '../../../admin-x-ds/settings/SettingGroup';
import SettingGroupContent from '../../../admin-x-ds/settings/SettingGroupContent'; import SettingGroupContent from '../../../admin-x-ds/settings/SettingGroupContent';
import SettingGroupHeader from '../../../admin-x-ds/settings/SettingGroupHeader'; import timezoneData from '@tryghost/timezone-data';
import {SettingsContext} from '../../SettingsProvider'; import {SettingsContext} from '../../SettingsProvider';
import {getLocalTime, getSettingValue} from '../../../utils/helpers'; import {getLocalTime, getSettingValue} from '../../../utils/helpers';
interface TimezoneDataDropdownOption {
name: string;
label: string;
}
const TimeZone: React.FC = () => { const TimeZone: React.FC = () => {
const {settings} = useContext(SettingsContext) || {}; const [currentState, setCurrentState] = useState<TSettingGroupStates>('view');
const publicationTimezone = getSettingValue(settings, 'timezone'); const {settings, saveSettings} = useContext(SettingsContext) || {};
const savedPublicationTimezone = getSettingValue(settings, 'timezone');
const [publicationTimezone, setPublicationTimezone] = useState(savedPublicationTimezone);
const [currentTime, setCurrentTime] = useState(getLocalTime(publicationTimezone)); const [currentTime, setCurrentTime] = useState(getLocalTime(publicationTimezone));
useEffect(() => { useEffect(() => {
@ -21,12 +28,19 @@ const TimeZone: React.FC = () => {
}; };
}, [publicationTimezone]); }, [publicationTimezone]);
const buttons = [ const handleStateChange = (newState: TSettingGroupStates) => {
{ setCurrentState(newState);
label: 'Edit', };
color: 'green'
} const handleSave = () => {
]; saveSettings?.([
{
key: 'timezone',
value: publicationTimezone
}
]);
setCurrentState('view');
};
const viewValues = [ const viewValues = [
{ {
@ -36,18 +50,37 @@ const TimeZone: React.FC = () => {
} }
]; ];
const customHeader = ( const timezoneOptions = timezoneData.map((tzOption: TimezoneDataDropdownOption) => {
<SettingGroupHeader return {
description="Set the time and date of your publication, used for all published posts" value: tzOption.name,
title="Site timezone" label: tzOption.label
> };
<ButtonGroup buttons={buttons} link={true} /> });
</SettingGroupHeader>
const inputFields = (
<SettingGroupContent columns={1}>
<Dropdown
defaultSelectedOption={publicationTimezone}
hint={`The local time here is currently ${currentTime}`}
options={timezoneOptions}
title="Site timezone"
onSelect={(value) => {
setCurrentState('unsaved');
setPublicationTimezone(value);
}}
/>
</SettingGroupContent>
); );
return ( return (
<SettingGroup customHeader={customHeader}> <SettingGroup
<SettingGroupContent values={viewValues} /> description='Set the time and date of your publication, used for all published posts'
state={currentState}
title='Site timezone'
onSave={handleSave}
onStateChange={handleStateChange}
>
{currentState === 'view' ? <SettingGroupContent values={viewValues} /> : inputFields}
</SettingGroup> </SettingGroup>
); );
}; };

View file

@ -0,0 +1 @@
declare module '@tryghost/timezone-data'