Skip to content

Commit

Permalink
refactor(announcement): mutate at data level
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore03109 committed Sep 20, 2023
1 parent 8b5b38b commit 352dda8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
15 changes: 0 additions & 15 deletions src/layouts/EditHomepage/EditHomepage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { DragDropContext } from "@hello-pangea/dnd"
import { Button, Tag } from "@opengovsg/design-system-react"
import update from "immutability-helper"
import _ from "lodash"
import moment from "moment-timezone"
import { useEffect, createRef, useState } from "react"

import { Footer } from "components/Footer"
Expand Down Expand Up @@ -984,20 +983,6 @@ const EditHomepage = ({ match }) => {
return newSection
})

// Modify announcements date time object
if (filteredFrontMatter.announcements) {
filteredFrontMatter.announcements.announcement_items = filteredFrontMatter.announcements.announcement_items.map(
(announcement) => {
return announcement.$set(
"date",
moment(announcement.date_time, "DD/MM/YYYY").format(
"DD MMMM YYYY"
)
)
}
)
}

const params = {
content: {
frontMatter: filteredFrontMatter,
Expand Down
15 changes: 10 additions & 5 deletions src/layouts/EditHomepage/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import moment from "moment"

export const RESOURCES_SECTION = {
title: "Resources",
subtitle: "Add a preview and link to your Resource Room",
Expand All @@ -15,11 +17,14 @@ export type AnnouncementSectionType = {
export const getDefaultAnnouncementSection = (): AnnouncementSectionType => {
return {
title: "Announcement",
date: new Date()
.toLocaleString("en-SG", {
timeZone: "Asia/Singapore",
})
.slice(0, "dd/mm/yyyy".length),
date: moment(
new Date()
.toLocaleString("en-SG", {
timeZone: "Asia/Singapore",
})
.slice(0, "dd/mm/yyyy".length),
"DD/MM/YYYY"
).format("DD MMMM YYYY"),
announcement: "Announcement content",
link_text: "",
link_url: "",
Expand Down
10 changes: 8 additions & 2 deletions src/layouts/components/Homepage/AnnouncementBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Textarea,
} from "@opengovsg/design-system-react"
import _ from "lodash"
import moment from "moment"
import { BiPlus } from "react-icons/bi"

import { useEditableContext } from "contexts/EditableContext"
Expand Down Expand Up @@ -108,12 +109,17 @@ export const AnnouncementBody = ({
<FormLabel>Date</FormLabel>
<DatePicker
id={`announcements-${announcementIndex}-date`}
inputValue={announcementDate}
inputValue={moment(
announcementDate,
"DD MMMM YYYY"
).format("DD/MM/YYYY")}
onInputValueChange={(value) => {
onChange({
target: {
id: `announcement-${announcementIndex}-date`,
value,
value: moment(value, "DD/MM/YYYY").format(
"DD MMMM YYYY"
),
},
})
}}
Expand Down
5 changes: 1 addition & 4 deletions src/templates/homepage/AnnouncementsSection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import moment from "moment-timezone"
import { forwardRef } from "react"

import editorStyles from "styles/isomer-cms/pages/Editor.module.scss"
Expand Down Expand Up @@ -113,9 +112,7 @@ export const TemplateAnnouncementsSection = forwardRef<
]
}
>
{moment(announcement.date, "DD/MM/YYYY").format(
"DD MMMM YYYY"
)}
{announcement.date}
</p>
</div>
<div
Expand Down

0 comments on commit 352dda8

Please sign in to comment.