Attempting to get an edit modal to display without a re-render

This commit is contained in:
Dane Everitt 2020-02-11 23:23:06 -08:00
parent 40c377f2e0
commit f180e3ef0b
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 97 additions and 52 deletions

View file

@ -0,0 +1,15 @@
import React from 'react';
import { Schedule } from '@/api/server/schedules/getServerSchedules';
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
type Props = {
schedule?: Schedule;
} & RequiredModalProps;
export default ({ schedule, ...props }: Props) => {
return (
<Modal {...props}>
<p>Testing</p>
</Modal>
);
};

View file

@ -1,24 +1,25 @@
import React, { useEffect, useState } from 'react'; import React, { useMemo, useState } from 'react';
import getServerSchedules, { Schedule } from '@/api/server/schedules/getServerSchedules'; import getServerSchedules, { Schedule } from '@/api/server/schedules/getServerSchedules';
import { ServerContext } from '@/state/server'; import { ServerContext } from '@/state/server';
import Spinner from '@/components/elements/Spinner'; import Spinner from '@/components/elements/Spinner';
import { Link } from 'react-router-dom'; import { Link, RouteComponentProps } from 'react-router-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCalendarAlt } from '@fortawesome/free-solid-svg-icons/faCalendarAlt';
import classNames from 'classnames';
import format from 'date-fns/format';
import FlashMessageRender from '@/components/FlashMessageRender'; import FlashMessageRender from '@/components/FlashMessageRender';
import ScheduleRow from '@/components/server/schedules/ScheduleRow';
import { httpErrorToHuman } from '@/api/http';
import { Actions, useStoreActions } from 'easy-peasy'; import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state'; import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http'; import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
export default () => { interface Params {
schedule?: string;
}
export default ({ history, match, location: { hash } }: RouteComponentProps<Params>) => {
const { id, uuid } = ServerContext.useStoreState(state => state.server.data!); const { id, uuid } = ServerContext.useStoreState(state => state.server.data!);
const [ schedules, setSchedules ] = useState<Schedule[] | null>(null); const [ schedules, setSchedules ] = useState<Schedule[] | null>(null);
const { clearFlashes, addError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); useMemo(() => {
useEffect(() => {
clearFlashes('schedules'); clearFlashes('schedules');
getServerSchedules(uuid) getServerSchedules(uuid)
.then(schedules => setSchedules(schedules)) .then(schedules => setSchedules(schedules))
@ -26,7 +27,9 @@ export default () => {
addError({ message: httpErrorToHuman(error), key: 'schedules' }); addError({ message: httpErrorToHuman(error), key: 'schedules' });
console.error(error); console.error(error);
}); });
}, [ uuid, setSchedules ]); }, [ setSchedules ]);
const matched = (schedules || []).find(schedule => schedule.id === Number(hash.match(/\d+$/) || 0));
return ( return (
<div className={'my-10 mb-6'}> <div className={'my-10 mb-6'}>
@ -35,49 +38,23 @@ export default () => {
<Spinner size={'large'} centered={true}/> <Spinner size={'large'} centered={true}/>
: :
schedules.map(schedule => ( schedules.map(schedule => (
<Link key={schedule.id} to={`/servers/${id}/schedules/${schedule.id}`} className={'grey-row-box'}> <Link
<div className={'icon'}> key={schedule.id}
<FontAwesomeIcon icon={faCalendarAlt} fixedWidth={true}/> to={`/server/${id}/schedules/#/schedule/${schedule.id}`}
</div> className={'grey-row-box'}
<div className={'flex-1 ml-4'}> >
<p>{schedule.name}</p> <ScheduleRow schedule={schedule}/>
<p className={'text-xs text-neutral-400'}>
Last run at: {schedule.lastRunAt ? format(schedule.lastRunAt, 'MMM Do [at] h:mma') : 'never'}
</p>
</div>
<div className={'flex items-center mx-8'}>
<div>
<p className={'font-medium text-center'}>{schedule.cron.minute}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Minute</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>{schedule.cron.hour}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Hour</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>{schedule.cron.dayOfMonth}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Day (Month)</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>*</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Month</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>{schedule.cron.dayOfWeek}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Day (Week)</p>
</div>
</div>
<div>
<p className={classNames('py-1 px-3 rounded text-xs uppercase', {
'bg-green-600': schedule.isActive,
'bg-neutral-400': !schedule.isActive,
})}>
{schedule.isActive ? 'Active' : 'Inactive'}
</p>
</div>
</Link> </Link>
)) ))
} }
{matched &&
<EditScheduleModal
schedule={matched}
visible={true}
appear={true}
onDismissed={() => history.push(match.url)}
/>
}
</div> </div>
); );
}; };

View file

@ -0,0 +1,53 @@
import React from 'react';
import { Schedule } from '@/api/server/schedules/getServerSchedules';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCalendarAlt } from '@fortawesome/free-solid-svg-icons/faCalendarAlt';
import format from 'date-fns/format';
import classNames from 'classnames';
export default ({ schedule }: { schedule: Schedule }) => (
<>
<div className={'icon'}>
<FontAwesomeIcon icon={faCalendarAlt} fixedWidth={true}/>
</div>
<div className={'flex-1 ml-4'}>
<p>{schedule.name}</p>
<p className={'text-xs text-neutral-400'}>
Last run
at: {schedule.lastRunAt ? format(schedule.lastRunAt, 'MMM Do [at] h:mma') : 'never'}
</p>
</div>
<div className={'flex items-center mx-8'}>
<div>
<p className={'font-medium text-center'}>{schedule.cron.minute}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Minute</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>{schedule.cron.hour}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Hour</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>{schedule.cron.dayOfMonth}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Day (Month)</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>*</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Month</p>
</div>
<div className={'ml-4'}>
<p className={'font-medium text-center'}>{schedule.cron.dayOfWeek}</p>
<p className={'text-2xs text-neutral-500 uppercase'}>Day (Week)</p>
</div>
</div>
<div>
<p
className={classNames('py-1 px-3 rounded text-xs uppercase', {
'bg-green-600': schedule.isActive,
'bg-neutral-400': !schedule.isActive,
})}
>
{schedule.isActive ? 'Active' : 'Inactive'}
</p>
</div>
</>
);