import React, { useEffect, useState } from 'react'; import tw from 'twin.macro'; import { useRouteMatch } from 'react-router-dom'; import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy'; import { Location } from '@/api/admin/locations/getLocations'; import getLocation from '@/api/admin/locations/getLocation'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import Spinner from '@/components/elements/Spinner'; import FlashMessageRender from '@/components/FlashMessageRender'; import { ApplicationStore } from '@/state'; interface ctx { location: Location | undefined; setLocation: Action; } export const Context = createContextStore({ location: undefined, setLocation: action((state, payload) => { state.location = payload; }), }); const LocationEditContainer = () => { const match = useRouteMatch<{ id?: string }>(); const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); const [ loading, setLoading ] = useState(true); const location = Context.useStoreState(state => state.location); const setLocation = Context.useStoreActions(actions => actions.setLocation); useEffect(() => { clearFlashes('location'); getLocation(Number(match.params?.id)) .then(location => setLocation(location)) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'location', error }); }) .then(() => setLoading(false)); }, []); if (loading || location === undefined) { return (
); } return (

{location.short}

{location.long}

); }; export default () => { return ( ); };