Skip to content

Commit

Permalink
Added logic for publishedAt and publishedBy
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-inge-arnes committed Dec 29, 2023
1 parent 7daae24 commit 4f1a77c
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class InformationBoxStrings {
id: getTrad("Component.Information.lastUpdate"),
defaultMessage: "Last update",
});

this.publishedAt = formatMessage({
id: getTrad("Component.Information.publishedAt"),
defaultMessage: "Last published",
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ const Body = ({ atlas }) => {
const { initialData, isCreatingEntry } = useCMEditViewDataManager();
const currentTime = React.useRef(Date.now());

const formatDate = (at) => {
const timestamp = at ? new Date(at).getTime() : Date.now();
const elapsed = timestamp - currentTime.current;
const { unit, value } = getUnits(-elapsed);

return formatRelativeTime(value, unit, { numeric: "auto" });
};

const getFieldInfo = (
atlas,
atField: "updatedAt" | "createdAt",
Expand All @@ -66,12 +74,9 @@ const Body = ({ atlas }) => {
const at = atlas[atField];

const displayName = user ? `${user.firstname} ${user.lastname}` : "-";
const timestamp = at ? new Date(at).getTime() : Date.now();
const elapsed = timestamp - currentTime.current;
const { unit, value } = getUnits(-elapsed);

return {
at: formatRelativeTime(value, unit, { numeric: "auto" }),
at: formatDate(at),
by: isCreatingEntry ? "-" : displayName,
};
};
Expand All @@ -83,15 +88,23 @@ const Body = ({ atlas }) => {
<Flex direction="column" alignItems="stretch" gap={4}>
<Flex direction="column" alignItems="stretch" gap={2} as="dl">
<KeyValuePair label={strings.created} value={created.at} />

<KeyValuePair label={strings.by} value={created.by} />
</Flex>

<Flex direction="column" alignItems="stretch" gap={2} as="dl">
<KeyValuePair label={strings.lastUpdate} value={updated.at} />

<KeyValuePair label={strings.by} value={updated.by} />
</Flex>

{atlas?.publishedAt && (
<Flex direction="column" alignItems="stretch" gap={2} as="dl">
<KeyValuePair
label={strings.publishedAt}
value={formatDate(atlas.publishedAt)}
/>
<KeyValuePair label={strings.by} value={atlas.publishedBy} />
</Flex>
)}
</Flex>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";
import { Typography } from "@strapi/design-system";

const AtlasFrontPageFields = () => {
return (
<Typography variant="delta" as="h2">
Atlas Front Page
</Typography>
);
};

export default AtlasFrontPageFields;
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import EditorPageStringsContext, {
import { getAtlas, updateAtlas } from "../../api/atlas-editor";
import InformationBox from "../../components/InformationBox";
import EditTitleModal from "./components/EditTitleModal";
import AtlasFrontPageFields from "./components/AtlasFrontPageFields";
import pluginId from "../../pluginId";
import { EditAtlasNav } from "./components/EditAtlasNav";
import { areDifferent, findDiff } from "./utils/differences";
import { update } from "lodash";

const emptyAtlas = {
id: -1,
Expand All @@ -43,12 +45,15 @@ const emptyAtlas = {
isPublished: false,
createdAt: new Date(),
updatedAt: new Date(),
publishedAt: null,
};

const EditPage = () => {
const handleUpdateAtlas = async (atlas) => {
setIsLoading(true);

updatePublishedInfo(atlas);

const { updateSuccess, err, updatedContent } = await updateAtlas(
atlas.current,
);
Expand All @@ -68,6 +73,12 @@ const EditPage = () => {
setIsLoading(false);
};

const updatePublishedInfo = (atlas) => {
if (atlas.current.isPublished && !atlas.original.isPublished) {
atlas.current.updatePublishedInfo = true;
}
};

const handleFieldChange = (fieldName, value) => {
const updatedCurrentAtlas = { ...atlas.current, [fieldName]: value };

Expand Down Expand Up @@ -185,6 +196,18 @@ const EditPage = () => {
}
/>
</Box>
<Box
hasRadius
background="neutral0"
shadow="tableShadow"
paddingLeft={6}
paddingRight={6}
paddingTop={6}
paddingBottom={6}
borderColor="neutral150"
>
<AtlasFrontPageFields />
</Box>
</Flex>
</GridItem>
<GridItem col={3} s={12}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"Component.Information.by": "By",
"Component.Information.lastUpdate": "Last update",
"Component.Information.information": "Information",
"Component.Information.publishedAt": "Last published",
"Editpage.Form.isPublished": "Is published",
"Editpage.Form.yes": "Yes",
"Editpage.Form.no": "No",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"Component.Information.by": "Av",
"Component.Information.lastUpdate": "Sist oppdatert",
"Component.Information.information": "Informasjon",
"Component.Information.publishedAt": "Sist publisert",
"Editpage.Form.isPublished": "Er publisert",
"Editpage.Form.yes": "Ja",
"Editpage.Form.no": "Nei",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ module.exports = {
type: "boolean",
default: false,
},
publishedAt: {
type: "datetime",
default: null,
},
publishedBy: {
type: "string",
required: false,
},
mainTitle: {
type: "string",
required: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ const atlas = require("../../../../api/atlas/controllers/atlas");

const populateParams = {
id: true,
isPublished: true,
mainTitle: true,
shortTitle: true,
frontPageText: true,
createdAt: true,
updatedAt: true,
locale: true,
isPublished: true,
publishedAt: true,
publishedBy: true,
createdAt: true,
createdBy: true,
updatedAt: true,
updatedBy: true,
};

Expand Down Expand Up @@ -56,15 +58,21 @@ module.exports = ({ strapi }) => ({
async update(ctx) {
const atlas = ctx.request.body;
const userInfo = ctx.state.user;
const atlasCleaned = {
let atlasCleaned = {
id: atlas.id,
isPublished: atlas.isPublished,
publishedAt: atlas.publishedAt,
mainTitle: atlas.mainTitle,
shortTitle: atlas.shortTitle,
frontPageText: atlas.frontPageText,
updatedBy: userInfo.id,
};

if (atlas.updatePublishedInfo) {
atlasCleaned.publishedBy = `${userInfo.firstname} ${userInfo.lastname}`;
atlasCleaned.publishedAt = new Date();
}

let updateResult = await strapi.entityService.update(
"plugin::atlas-editor.health-atlas",
atlas.id,
Expand Down
2 changes: 2 additions & 0 deletions apps/strapi/types/generated/contentTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ export interface PluginAtlasEditorHealthAtlas extends Schema.CollectionType {
};
attributes: {
isPublished: Attribute.Boolean & Attribute.DefaultTo<false>;
publishedAt: Attribute.DateTime;
publishedBy: Attribute.String;
mainTitle: Attribute.String & Attribute.Required;
shortTitle: Attribute.String & Attribute.Required;
frontPageText: Attribute.Text & Attribute.Required;
Expand Down

0 comments on commit 4f1a77c

Please sign in to comment.