Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

layouts as objects #331

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions apps/newsletters-api/static/layouts.local.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{
"UK": [
{
"title": "Get started",
"newsletters": [
"bmx-tesla",
"roi-female",
"electric-bicycle",
"dram-security",
"does-not-exist"
]
},
{
"title": "In depth",
"newsletters": [
"muddle-teal",
"van-coordinator",
"does-not-exist-two",
"dram-security"
]
}
]
}
"UK": {
"groups": [
{
"title": "Get started",
"newsletters": [
"bmx-tesla",
"roi-female",
"electric-bicycle",
"dram-security",
"does-not-exist"
]
},
{
"title": "In depth",
"newsletters": [
"muddle-teal",
"van-coordinator",
"does-not-exist-two",
"dram-security"
]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Props {
export const LayoutDisplay = ({ newsletters, layout }: Props) => {
return (
<Box component={'article'}>
{layout.map((section, index) => (
{layout.groups.map((section, index) => (
<Box key={index} component={'section'}>
<Typography variant="h3">{section.title}</Typography>
{section.subtitle && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
Layout,
NewsletterData,
} from '@newsletters-nx/newsletters-data-client';
import { editionIds } from '@newsletters-nx/newsletters-data-client';
import { editionIds, makeBlankLayout } from '@newsletters-nx/newsletters-data-client';
import { fetchPostApiData } from '../../api-requests/fetch-api-data';
import { usePermissions } from '../../hooks/user-hooks';

Expand All @@ -26,12 +26,11 @@ const LayoutOverview = ({
}) => {
const navigate = useNavigate();
const permissions = usePermissions();

const newsletterCount = layout?.flatMap(
const newsletterCount = layout?.groups.flatMap(
(section) => section.newsletters,
).length;
const invalidNewsletterCount = layout
?.flatMap((section) => section.newsletters)
const invalidNewsletterCount = layout?.groups
.flatMap((section) => section.newsletters)
.filter(
(newsletterId) =>
!newsletters.some(
Expand All @@ -40,7 +39,7 @@ const LayoutOverview = ({
).length;

const handleCreate = async (editionId: EditionId) => {
const result = await fetchPostApiData(`/api/layouts/${editionId}`, []);
const result = await fetchPostApiData(`/api/layouts/${editionId}`, makeBlankLayout());
if (result) {
navigate(`/layouts/${editionId.toLowerCase()}`);
} else {
Expand Down Expand Up @@ -73,7 +72,7 @@ const LayoutOverview = ({

{!!newsletterCount && (
<Alert>
{newsletterCount} newsletters and {layout?.length ?? 0} groups in
{newsletterCount} newsletters and {layout?.groups.length ?? 0} groups in
layout
</Alert>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const LayoutView = () => {
return (
<ContentWrapper>
<Typography variant="h2">Layout for {editionId}</Typography>
<LayoutDisplay newsletters={newsletters} layout={localLayout ?? []} />
<LayoutDisplay newsletters={newsletters} layout={localLayout ?? { groups: [] }} />
{permissions?.editLayouts && (
<JsonEditor
schema={layoutSchema}
Expand Down
13 changes: 10 additions & 3 deletions libs/newsletters-data-client/src/lib/layout-storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ export const editionIdSchema = z.enum(['UK', 'US', 'AU', 'INT', 'EUR']);
export const editionIds = editionIdSchema.options;
export type EditionId = z.infer<typeof editionIdSchema>;

export const layoutSchema = z
const layoutGroup = z
.object({
title: z.string(),
subtitle: z.string().optional(),
newsletters: z.string().array(),
})
.array();
});

export const layoutSchema = z.object({
groups: layoutGroup.array()
});

export type Layout = z.infer<typeof layoutSchema>;

export type EditionsLayouts = Partial<Record<EditionId, Layout>>;

export const makeBlankLayout = (): Layout => ({
groups: []
})
Loading