-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Carlos Valente <carlosvalente@pm.me>
- Loading branch information
Showing
7 changed files
with
181 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
apps/client/src/features/app-settings/panel/project-panel/ProjectMergeForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { useState } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { Button, Switch } from '@chakra-ui/react'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { PROJECT_DATA } from '../../../../common/api/constants'; | ||
import { getDb, patchData } from '../../../../common/api/db'; | ||
import { maybeAxiosError } from '../../../../common/api/utils'; | ||
import * as Panel from '../PanelUtils'; | ||
|
||
import { makeProjectPatch } from './project.utils'; | ||
|
||
import style from './ProjectPanel.module.scss'; | ||
|
||
interface ProjectMergeFromProps { | ||
onClose: () => void; | ||
fileName: string; | ||
} | ||
|
||
type ProjectMergeFormValues = { | ||
project: boolean; | ||
rundown: boolean; | ||
viewSettings: boolean; | ||
urlPresets: boolean; | ||
osc: boolean; | ||
http: boolean; | ||
}; | ||
|
||
export default function ProjectMergeForm(props: ProjectMergeFromProps) { | ||
const { onClose, fileName } = props; | ||
const [error, setError] = useState<string | null>(null); | ||
const queryClient = useQueryClient(); | ||
|
||
const { | ||
handleSubmit, | ||
register, | ||
formState: { isSubmitting, isValid, isDirty }, | ||
} = useForm<ProjectMergeFormValues>({ | ||
defaultValues: { | ||
project: false, | ||
rundown: false, | ||
viewSettings: false, | ||
urlPresets: false, | ||
osc: false, | ||
http: false, | ||
}, | ||
resetOptions: { | ||
keepDirtyValues: true, | ||
}, | ||
}); | ||
|
||
const handleSubmitCreate = async (values: ProjectMergeFormValues) => { | ||
const allFalse = Object.values(values).every((value) => !value); | ||
if (allFalse) { | ||
setError('At least one option must be selected'); | ||
return; | ||
} | ||
|
||
try { | ||
setError(null); | ||
|
||
// make patch object | ||
const { data } = await getDb(fileName); | ||
const patch = await makeProjectPatch(data, values); | ||
|
||
// request patch | ||
await patchData(patch); | ||
await queryClient.invalidateQueries({ queryKey: PROJECT_DATA }); | ||
onClose(); | ||
} catch (error) { | ||
setError(maybeAxiosError(error)); | ||
} | ||
}; | ||
|
||
return ( | ||
<Panel.Section as='form' onSubmit={handleSubmit(handleSubmitCreate)}> | ||
<Panel.Title> | ||
Merge {`"${fileName}"`} | ||
<div className={style.createActionButtons}> | ||
<Button onClick={onClose} variant='ontime-ghosted' size='sm' isDisabled={isSubmitting}> | ||
Cancel | ||
</Button> | ||
<Button | ||
isDisabled={!isValid || !isDirty} | ||
type='submit' | ||
isLoading={isSubmitting} | ||
variant='ontime-filled' | ||
size='sm' | ||
> | ||
Merge | ||
</Button> | ||
</div> | ||
</Panel.Title> | ||
{error && <Panel.Error>{error}</Panel.Error>} | ||
<div className={style.innerColumn}> | ||
<Panel.Description> | ||
Select partial data from {`"${fileName}"`} to merge into the current project. | ||
<br /> This process is irreversible. | ||
</Panel.Description> | ||
<label> | ||
<Switch variant='ontime' {...register('project')} /> | ||
Project data | ||
</label> | ||
<label> | ||
<Switch variant='ontime' {...register('rundown')} /> | ||
Rundown + Custom Fields | ||
</label> | ||
<label> | ||
<Switch variant='ontime' {...register('viewSettings')} /> | ||
View Settings | ||
</label> | ||
<label> | ||
<Switch variant='ontime' {...register('urlPresets')} /> | ||
URL Presets | ||
</label> | ||
<label> | ||
<Switch variant='ontime' {...register('osc')} /> | ||
OSC Integration | ||
</label> | ||
<label> | ||
<Switch variant='ontime' {...register('http')} /> | ||
HTTP Integration | ||
</label> | ||
</div> | ||
</Panel.Section> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
apps/client/src/features/app-settings/panel/project-panel/project.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { DatabaseModel, isKeyOfType } from 'ontime-types'; | ||
|
||
export async function makeProjectPatch(data: DatabaseModel, mergeKeys: Record<string, boolean>) { | ||
const patchObject: Partial<DatabaseModel> = {}; | ||
|
||
for (const key in mergeKeys) { | ||
if (isKeyOfType(key, data) && mergeKeys[key]) { | ||
// if the rundown is merged we also need the custom fields | ||
if (key === 'rundown') { | ||
patchObject.customFields = data['customFields']; | ||
} | ||
Object.assign(patchObject, { [key]: data[key] }); | ||
} | ||
} | ||
|
||
return patchObject; | ||
} |