import { ChangeEvent } from "react"; import * as Pages from "../../types/Pages"; import { LocalizedValue } from "../../types"; import { errorsOn } from "./utils"; import usePageFormContext from "./usePageFormContext"; import LabelledField from "../LabelledField"; function missingPathSegment(ancestors: Pages.Ancestor[], locale: string) { for (let i = 0; i < ancestors.length; i++) { if (!ancestors[i].path_segment[locale]) { return ancestors[i]; } } return null; } export default function PathSegment() { const { state, dispatch } = usePageFormContext(); const { page, locale } = state; const value = (page.path_segment as LocalizedValue)[locale]; const handleChange = (evt: ChangeEvent) => { dispatch({ type: "update", payload: { path_segment: evt.target.value } }); }; const editAncestor = missingPathSegment(page.ancestors, locale); if (editAncestor) { const editUrl = `/admin/${locale}/pages/${editAncestor.id}/edit#metadata`; return (

Unable to add a path segment to this page, please add one to{" "} this page's ancestor first.

); } return ( ); }