Skip to content

Commit

Permalink
more minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DingDongSoLong4 committed Jul 27, 2023
1 parent 906a44c commit a0986f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import isEqual from "lodash-es/isEqual";

interface IGalleryChapterForm {
galleryID: string;
editingChapter?: GQL.GalleryChapterDataFragment;
chapter?: GQL.GalleryChapterDataFragment;
onClose: () => void;
}

export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({
galleryID,
editingChapter,
chapter,
onClose,
}) => {
const intl = useIntl();
Expand All @@ -30,6 +30,8 @@ export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({
const [galleryChapterDestroy] = useGalleryChapterDestroy();
const Toast = useToast();

const isNew = chapter === undefined;

const schema = yup.object({
title: yup.string().ensure(),
image_index: yup
Expand All @@ -41,8 +43,8 @@ export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({
});

const initialValues = {
title: editingChapter?.title ?? "",
image_index: editingChapter?.image_index ?? 1,
title: chapter?.title ?? "",
image_index: chapter?.image_index ?? 1,
};

type InputValues = yup.InferType<typeof schema>;
Expand All @@ -56,7 +58,7 @@ export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({

async function onSave(input: InputValues) {
try {
if (!editingChapter) {
if (isNew) {
await galleryChapterCreate({
variables: {
gallery_id: galleryID,
Expand All @@ -66,7 +68,7 @@ export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({
} else {
await galleryChapterUpdate({
variables: {
id: editingChapter.id,
id: chapter.id,
gallery_id: galleryID,
...input,
},
Expand All @@ -80,10 +82,10 @@ export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({
}

async function onDelete() {
if (!editingChapter) return;
if (isNew) return;

try {
await galleryChapterDestroy({ variables: { id: editingChapter.id } });
await galleryChapterDestroy({ variables: { id: chapter.id } });
} catch (e) {
Toast.error(e);
} finally {
Expand Down Expand Up @@ -130,9 +132,7 @@ export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({
<div className="col d-flex">
<Button
variant="primary"
disabled={
(editingChapter && !formik.dirty) || !isEqual(formik.errors, {})
}
disabled={(!isNew && !formik.dirty) || !isEqual(formik.errors, {})}
onClick={() => formik.submitForm()}
>
<FormattedMessage id="actions.save" />
Expand All @@ -145,7 +145,7 @@ export const GalleryChapterForm: React.FC<IGalleryChapterForm> = ({
>
<FormattedMessage id="actions.cancel" />
</Button>
{editingChapter && (
{!isNew && (
<Button
variant="danger"
className="ml-auto"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const GalleryChapterPanel: React.FC<IGalleryChapterPanelProps> = ({
return (
<GalleryChapterForm
galleryID={gallery.id}
editingChapter={editingChapter}
chapter={editingChapter}
onClose={closeEditor}
/>
);
Expand Down
28 changes: 14 additions & 14 deletions ui/v2.5/src/components/Scenes/SceneDetails/SceneMarkerForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ import isEqual from "lodash-es/isEqual";

interface ISceneMarkerForm {
sceneID: string;
editingMarker?: GQL.SceneMarkerDataFragment;
marker?: GQL.SceneMarkerDataFragment;
onClose: () => void;
}

export const SceneMarkerForm: React.FC<ISceneMarkerForm> = ({
sceneID,
editingMarker,
marker,
onClose,
}) => {
const [sceneMarkerCreate] = useSceneMarkerCreate();
const [sceneMarkerUpdate] = useSceneMarkerUpdate();
const [sceneMarkerDestroy] = useSceneMarkerDestroy();
const Toast = useToast();

const isNew = marker === undefined;

const schema = yup.object({
title: yup.string().ensure(),
seconds: yup.number().required().integer(),
Expand All @@ -39,10 +41,10 @@ export const SceneMarkerForm: React.FC<ISceneMarkerForm> = ({
});

const initialValues = {
title: editingMarker?.title ?? "",
seconds: editingMarker?.seconds ?? Math.round(getPlayerPosition() ?? 0),
primary_tag_id: editingMarker?.primary_tag.id ?? "",
tag_ids: editingMarker?.tags.map((tag) => tag.id) ?? [],
title: marker?.title ?? "",
seconds: marker?.seconds ?? Math.round(getPlayerPosition() ?? 0),
primary_tag_id: marker?.primary_tag.id ?? "",
tag_ids: marker?.tags.map((tag) => tag.id) ?? [],
};

type InputValues = yup.InferType<typeof schema>;
Expand All @@ -56,7 +58,7 @@ export const SceneMarkerForm: React.FC<ISceneMarkerForm> = ({

async function onSave(input: InputValues) {
try {
if (!editingMarker) {
if (isNew) {
await sceneMarkerCreate({
variables: {
scene_id: sceneID,
Expand All @@ -66,7 +68,7 @@ export const SceneMarkerForm: React.FC<ISceneMarkerForm> = ({
} else {
await sceneMarkerUpdate({
variables: {
id: editingMarker.id,
id: marker.id,
scene_id: sceneID,
...input,
},
Expand All @@ -80,10 +82,10 @@ export const SceneMarkerForm: React.FC<ISceneMarkerForm> = ({
}

async function onDelete() {
if (!editingMarker) return;
if (isNew) return;

try {
await sceneMarkerDestroy({ variables: { id: editingMarker.id } });
await sceneMarkerDestroy({ variables: { id: marker.id } });
} catch (e) {
Toast.error(e);
} finally {
Expand Down Expand Up @@ -169,9 +171,7 @@ export const SceneMarkerForm: React.FC<ISceneMarkerForm> = ({
<div className="col d-flex">
<Button
variant="primary"
disabled={
(editingMarker && !formik.dirty) || !isEqual(formik.errors, {})
}
disabled={(!isNew && !formik.dirty) || !isEqual(formik.errors, {})}
onClick={() => formik.submitForm()}
>
<FormattedMessage id="actions.save" />
Expand All @@ -184,7 +184,7 @@ export const SceneMarkerForm: React.FC<ISceneMarkerForm> = ({
>
<FormattedMessage id="actions.cancel" />
</Button>
{editingMarker && (
{!isNew && (
<Button
variant="danger"
className="ml-auto"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const SceneMarkersPanel: React.FC<ISceneMarkersPanelProps> = ({
return (
<SceneMarkerForm
sceneID={sceneId}
editingMarker={editingMarker}
marker={editingMarker}
onClose={closeEditor}
/>
);
Expand Down

0 comments on commit a0986f8

Please sign in to comment.