Skip to content

Commit

Permalink
fix: prevent TypeError when deleting assets in table view
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpyon committed Jul 23, 2023
1 parent b7b08f3 commit a86764c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/components/DialogAssetEdit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const DialogAssetEdit = (props: Props) => {
const assetItem = useTypedSelector(state => selectAssetById(state, String(assetId))) // TODO: check casting
const tags = useTypedSelector(selectTags)

const assetUpdatedPrev = useRef<string | null>(null)
const assetUpdatedPrev = useRef<string | undefined>(undefined)

// Generate a snapshot of the current asset
const [assetSnapshot, setAssetSnapshot] = useState(assetItem?.asset)
Expand Down Expand Up @@ -222,7 +222,7 @@ const DialogAssetEdit = (props: Props) => {
<FormSubmitButton
disabled={formUpdating || !isDirty || !isValid}
isValid={isValid}
lastUpdated={currentAsset._updatedAt}
lastUpdated={currentAsset?._updatedAt}
onClick={handleSubmit(onSubmit)}
/>
</Flex>
Expand All @@ -242,7 +242,7 @@ const DialogAssetEdit = (props: Props) => {
*/}
<Flex direction={['column-reverse', 'column-reverse', 'row-reverse']}>
<Box flex={1} marginTop={[5, 5, 0]} padding={4}>
<WithReferringDocuments documentStore={documentStore} id={assetItem.asset._id}>
<WithReferringDocuments documentStore={documentStore} id={currentAsset._id}>
{({isLoading, referringDocuments}) => {
const uniqueReferringDocuments = getUniqueDocuments(referringDocuments)
return (
Expand Down
6 changes: 4 additions & 2 deletions src/components/TableRowAsset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const TableRowAsset = (props: Props) => {
(e: MouseEvent<HTMLDivElement>) => {
e.stopPropagation()

if (!asset) return
if (onSelect) {
dispatch(dialogActions.showAssetEdit({assetId: asset._id}))
} else if (shiftPressed.current && !picked) {
Expand All @@ -108,13 +109,14 @@ const TableRowAsset = (props: Props) => {
dispatch(assetsActions.pick({assetId: asset._id, picked: !picked}))
}
},
[asset._id, dispatch, lastPicked, onSelect, picked, shiftPressed]
[asset, dispatch, lastPicked, onSelect, picked, shiftPressed]
)

const handleClick = useCallback(
(e: MouseEvent<HTMLDivElement>) => {
e.stopPropagation()

if (!asset) return
if (onSelect) {
onSelect([{kind: 'assetDocumentId', value: asset._id}])
} else if (shiftPressed.current) {
Expand All @@ -127,7 +129,7 @@ const TableRowAsset = (props: Props) => {
dispatch(dialogActions.showAssetEdit({assetId: asset._id}))
}
},
[asset._id, dispatch, lastPicked, onSelect, picked, shiftPressed]
[asset, dispatch, lastPicked, onSelect, picked, shiftPressed]
)

const opacityCell = updating ? 0.5 : 1
Expand Down
5 changes: 4 additions & 1 deletion src/modules/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,10 @@ export const selectAssetById = createSelector(
(state: RootReducerState) => state.assets.byIds,
(_state: RootReducerState, assetId: string) => assetId
],
(byIds, assetId) => byIds[assetId]
(byIds, assetId) => {
const asset = byIds[assetId]
return asset ? asset : undefined
}
)

export const selectAssets: Selector<RootReducerState, AssetItem[]> = createSelector(
Expand Down

0 comments on commit a86764c

Please sign in to comment.