-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Folders: Ability to set Hero Image
- Loading branch information
1 parent
89081f0
commit 63e6e26
Showing
10 changed files
with
147 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { contextPermissionsForFolder as perms } from '../../auth/contextPermissionsForFolder'; | ||
import { doAuthError } from '../../auth/doAuthError'; | ||
import Folder from '../../models/Folder'; | ||
import File from '../../models/File'; | ||
import { GraphQLID, GraphQLNonNull } from 'graphql/index'; | ||
import { folderType } from '../types/folderType'; | ||
|
||
const resolver = async (_, params, context) => { | ||
const [p, u] = await perms(context, params.folderId, true); | ||
if (p != 'Admin') doAuthError("You don't have permissions for this folder"); | ||
const folder = await Folder.findByPk(params.folderId); | ||
const heroImage = await File.findByPk(params.heroImageId); | ||
if (!heroImage) doAuthError('Invalid hero image ID'); | ||
if (heroImage.type != 'Image') doAuthError('Not an image'); | ||
if (heroImage.folderId != folder.id) doAuthError('Not in this folder'); | ||
|
||
folder.heroImageId = heroImage.id; | ||
await folder.save(); | ||
return { ...folder.toJSON(), heroImage }; | ||
}; | ||
|
||
export const editFolder = { | ||
type: new GraphQLNonNull(folderType), | ||
resolve: resolver, | ||
args: { | ||
folderId: { type: new GraphQLNonNull(GraphQLID) }, | ||
heroImageId: { type: GraphQLID }, | ||
}, | ||
}; |
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
39 changes: 39 additions & 0 deletions
39
frontend/src/components/FileListView/Review/SetHeroImageButton.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,39 @@ | ||
import { MinimalFile } from '../../../../types'; | ||
import { useMe } from '../../../hooks/useMe'; | ||
import { ActionIcon, Tooltip } from '@mantine/core'; | ||
import { TbPhotoHeart } from 'react-icons/tb'; | ||
import { gql } from '../../../helpers/gql'; | ||
import { useMutation } from 'urql'; | ||
import { useState } from 'react'; | ||
|
||
export const SetHeroImageButton = ({ file }: { file: MinimalFile }) => { | ||
const { isUser } = useMe(); | ||
const [, mutate] = useMutation(editFolderMutation); | ||
const [loading, setLoading] = useState(false); | ||
if (!isUser || !file || !file.type == 'Image') return null; | ||
//TODO: actually do mutation | ||
//TODO: look different if we are looking at the current hero image :) | ||
const onClick = () => { | ||
setLoading(true); | ||
mutate({ | ||
folderId: file.folderId, | ||
heroImageId: file?.id, | ||
}).then(() => setLoading(false)); | ||
}; | ||
return ( | ||
<Tooltip label="Use this Image as the 'Hero Image' for this folder"> | ||
<ActionIcon variant="default" onClick={onClick} loading={loading}> | ||
<TbPhotoHeart /> | ||
</ActionIcon> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
const editFolderMutation = gql(/* GraphQL */ ` | ||
mutation editFolder($folderId: ID!, $heroImageId: ID!) { | ||
editFolder(folderId: $folderId, heroImageId: $heroImageId) { | ||
...FolderFragment | ||
...HeroImageFragment | ||
} | ||
} | ||
`); |
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
Oops, something went wrong.