Merge pull request #1756 from payloadcms/fix/1753-date-field-updates
Fix: updatedAt and createdAt fields change
This commit is contained in:
@@ -47,6 +47,9 @@ const Duplicate: React.FC<Props> = ({ slug, collection, id }) => {
|
||||
});
|
||||
let data = await response.json();
|
||||
|
||||
if ('createdAt' in data) delete data.createdAt;
|
||||
if ('updatedAt' in data) delete data.updatedAt;
|
||||
|
||||
if (typeof collection.admin.hooks?.beforeDuplicate === 'function') {
|
||||
data = await collection.admin.hooks.beforeDuplicate({
|
||||
data,
|
||||
|
||||
@@ -34,7 +34,7 @@ const DefaultGlobalView: React.FC<Props> = (props) => {
|
||||
const { t, i18n } = useTranslation('general');
|
||||
|
||||
const {
|
||||
global, data, onSave, permissions, action, apiURL, initialState, isLoading,
|
||||
global, data, onSave, permissions, action, apiURL, initialState, isLoading, updatedAt,
|
||||
} = props;
|
||||
|
||||
const {
|
||||
@@ -170,10 +170,10 @@ const DefaultGlobalView: React.FC<Props> = (props) => {
|
||||
</a>
|
||||
</li>
|
||||
)}
|
||||
{data.updatedAt && (
|
||||
{updatedAt && (
|
||||
<li>
|
||||
<div className={`${baseClass}__label`}>{t('lastModified')}</div>
|
||||
<div>{format(new Date(data.updatedAt as string), dateFormat)}</div>
|
||||
<div>{format(new Date(updatedAt as string), dateFormat)}</div>
|
||||
</li>
|
||||
)}
|
||||
</ul>
|
||||
|
||||
@@ -22,6 +22,7 @@ const GlobalView: React.FC<IndexProps> = (props) => {
|
||||
const { setStepNav } = useStepNav();
|
||||
const { user } = useAuth();
|
||||
const [initialState, setInitialState] = useState<Fields>();
|
||||
const [updatedAt, setUpdatedAt] = useState<string>();
|
||||
const { getVersions, preferencesKey, docPermissions, getDocPermissions } = useDocumentInfo();
|
||||
const { getPreference } = usePreferences();
|
||||
const { t } = useTranslation();
|
||||
@@ -51,6 +52,7 @@ const GlobalView: React.FC<IndexProps> = (props) => {
|
||||
const onSave = useCallback(async (json) => {
|
||||
getVersions();
|
||||
getDocPermissions();
|
||||
setUpdatedAt(json?.result?.updatedAt);
|
||||
const state = await buildStateFromSchema({ fieldSchema: fields, data: json.result, operation: 'update', user, locale, t });
|
||||
setInitialState(state);
|
||||
}, [getVersions, fields, user, locale, t, getDocPermissions]);
|
||||
@@ -93,6 +95,7 @@ const GlobalView: React.FC<IndexProps> = (props) => {
|
||||
onSave,
|
||||
apiURL: `${serverURL}${api}/globals/${slug}${global.versions?.drafts ? '?draft=true' : ''}`,
|
||||
action: `${serverURL}${api}/globals/${slug}?locale=${locale}&depth=0&fallback-locale=null`,
|
||||
updatedAt: updatedAt || dataToRender?.updatedAt,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -17,4 +17,5 @@ export type Props = {
|
||||
initialState: Fields
|
||||
isLoading: boolean
|
||||
autosaveEnabled: boolean
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ const DefaultEditView: React.FC<Props> = (props) => {
|
||||
disableLeaveWithoutSaving,
|
||||
customHeader,
|
||||
id,
|
||||
updatedAt,
|
||||
} = props;
|
||||
|
||||
const {
|
||||
@@ -264,10 +265,10 @@ const DefaultEditView: React.FC<Props> = (props) => {
|
||||
)}
|
||||
{timestamps && (
|
||||
<React.Fragment>
|
||||
{data.updatedAt && (
|
||||
{updatedAt && (
|
||||
<li>
|
||||
<div className={`${baseClass}__label`}>{t('lastModified')}</div>
|
||||
<div>{format(new Date(data.updatedAt), dateFormat)}</div>
|
||||
<div>{format(new Date(updatedAt), dateFormat)}</div>
|
||||
</li>
|
||||
)}
|
||||
{(publishedDoc?.createdAt || data?.createdAt) && (
|
||||
|
||||
@@ -41,6 +41,7 @@ const EditView: React.FC<IndexProps> = (props) => {
|
||||
const { state: locationState } = useLocation();
|
||||
const history = useHistory();
|
||||
const [initialState, setInitialState] = useState<Fields>();
|
||||
const [updatedAt, setUpdatedAt] = useState<string>();
|
||||
const { user } = useAuth();
|
||||
const { getVersions, preferencesKey, getDocPermissions, docPermissions } = useDocumentInfo();
|
||||
const { getPreference } = usePreferences();
|
||||
@@ -49,6 +50,7 @@ const EditView: React.FC<IndexProps> = (props) => {
|
||||
const onSave = useCallback(async (json: any) => {
|
||||
getVersions();
|
||||
getDocPermissions();
|
||||
setUpdatedAt(json?.doc?.updatedAt);
|
||||
if (!isEditing) {
|
||||
setRedirect(`${admin}/collections/${collection.slug}/${json?.doc?.id}`);
|
||||
} else {
|
||||
@@ -69,6 +71,7 @@ const EditView: React.FC<IndexProps> = (props) => {
|
||||
return;
|
||||
}
|
||||
const awaitInitialState = async () => {
|
||||
setUpdatedAt(dataToRender?.updatedAt);
|
||||
const state = await buildStateFromSchema({ fieldSchema: fields, data: dataToRender, user, operation: isEditing ? 'update' : 'create', id, locale, t });
|
||||
await getPreference(preferencesKey);
|
||||
setInitialState(state);
|
||||
@@ -110,6 +113,7 @@ const EditView: React.FC<IndexProps> = (props) => {
|
||||
hasSavePermission,
|
||||
apiURL,
|
||||
action,
|
||||
updatedAt: updatedAt || dataToRender?.updatedAt,
|
||||
}}
|
||||
/>
|
||||
</EditDepthContext.Provider>
|
||||
|
||||
@@ -24,4 +24,5 @@ export type Props = IndexProps & {
|
||||
disableActions?: boolean
|
||||
disableLeaveWithoutSaving?: boolean
|
||||
customHeader?: React.ReactNode
|
||||
updatedAt?: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user