-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logic that lets users favorite a template (#9713)
- Loading branch information
1 parent
28c7432
commit 4558e14
Showing
16 changed files
with
250 additions
and
17 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
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
41 changes: 41 additions & 0 deletions
41
packages/client/mutations/ToggleFavoriteTemplateMutation.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,41 @@ | ||
import graphql from 'babel-plugin-relay/macro' | ||
import {commitMutation} from 'react-relay' | ||
import {ToggleFavoriteTemplateMutation as TToggleFavoriteTemplateMutation} from '../__generated__/ToggleFavoriteTemplateMutation.graphql' | ||
import {StandardMutation} from '../types/relayMutations' | ||
|
||
graphql` | ||
fragment ToggleFavoriteTemplateMutation_viewer on ToggleFavoriteTemplateSuccess { | ||
user { | ||
id | ||
...ActivityCardFavorite_user | ||
} | ||
} | ||
` | ||
|
||
const mutation = graphql` | ||
mutation ToggleFavoriteTemplateMutation($templateId: ID!) { | ||
toggleFavoriteTemplate(templateId: $templateId) { | ||
... on ErrorPayload { | ||
error { | ||
message | ||
} | ||
} | ||
...ToggleFavoriteTemplateMutation_viewer @relay(mask: false) | ||
} | ||
} | ||
` | ||
|
||
const ToggleFavoriteTemplateMutation: StandardMutation<TToggleFavoriteTemplateMutation> = ( | ||
atmosphere, | ||
variables, | ||
{onError, onCompleted} | ||
) => { | ||
return commitMutation<TToggleFavoriteTemplateMutation>(atmosphere, { | ||
mutation, | ||
variables, | ||
onCompleted, | ||
onError | ||
}) | ||
} | ||
|
||
export default ToggleFavoriteTemplateMutation |
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
37 changes: 37 additions & 0 deletions
37
packages/server/graphql/public/mutations/toggleFavoriteTemplate.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,37 @@ | ||
import getKysely from '../../../postgres/getKysely' | ||
import {getUserId} from '../../../utils/authorization' | ||
import {MutationResolvers} from '../resolverTypes' | ||
|
||
const toggleFavoriteTemplate: MutationResolvers['toggleFavoriteTemplate'] = async ( | ||
_source, | ||
{templateId}, | ||
{authToken, dataLoader} | ||
) => { | ||
const viewerId = getUserId(authToken) | ||
const pg = getKysely() | ||
const userId = getUserId(authToken) | ||
|
||
const favoriteTemplateIds = await dataLoader.get('favoriteTemplateIds').load(viewerId) | ||
|
||
let updatedFavoriteTemplateIds | ||
|
||
const isCurrentlyFavorite = favoriteTemplateIds.includes(templateId) | ||
|
||
if (isCurrentlyFavorite) { | ||
updatedFavoriteTemplateIds = favoriteTemplateIds.filter((id) => id !== templateId) | ||
} else { | ||
updatedFavoriteTemplateIds = [...favoriteTemplateIds, templateId] | ||
} | ||
|
||
await pg | ||
.updateTable('User') | ||
.set({ | ||
favoriteTemplateIds: updatedFavoriteTemplateIds | ||
}) | ||
.where('id', '=', userId) | ||
.execute() | ||
|
||
return true | ||
} | ||
|
||
export default toggleFavoriteTemplate |
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.