Skip to content

Commit fabccdc

Browse files
committed
set up for saving tutorials
1 parent aef7a43 commit fabccdc

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

packages/gatsby-theme/src/components/TutorialListing.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,44 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
9595
);
9696
}}
9797
</Mutation>
98+
<Mutation
99+
mutation={gql`
100+
mutation SaveTutorial($id: ID!) {
101+
saveTutorial(tutorialId: $id) {
102+
code
103+
success
104+
userTutorial {
105+
id
106+
saved
107+
}
108+
}
109+
}
110+
`}
111+
variables={{
112+
id: tutorialId,
113+
}}
114+
>
115+
{save => {
116+
return (
117+
<button
118+
onClick={async () => {
119+
const mutationRes = await handleMutationResponse(
120+
save(),
121+
);
122+
if (mutationRes.error) {
123+
if (mutationRes.error === ApiErrors.AUTHORIZATION) {
124+
loginUser();
125+
} else {
126+
console.log(mutationRes.error);
127+
}
128+
}
129+
}}
130+
>
131+
Save
132+
</button>
133+
);
134+
}}
135+
</Mutation>
98136
</Box>
99137
<Box width={11 / 12}>
100138
<Link to={getTutorialOverviewSlug(tutorial.fileAbsolutePath)}>

packages/server/.yoga/nexus.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ export interface NexusGenFieldTypes {
327327
}
328328
Mutation: { // field return type
329329
authenticate: NexusGenRootTypes['AuthenticateUserPayload'] | null; // AuthenticateUserPayload
330+
saveTutorial: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
330331
upvoteTutorial: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
331332
}
332333
Query: { // field return type
@@ -392,6 +393,9 @@ export interface NexusGenArgTypes {
392393
authenticate: { // args
393394
githubCode: string; // String!
394395
}
396+
saveTutorial: { // args
397+
tutorialId: string; // ID!
398+
}
395399
upvoteTutorial: { // args
396400
tutorialId: string; // ID!
397401
}

packages/server/src/graphql/UserTutorial.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,44 @@ export const upvoteTutorial = mutationField('upvoteTutorial', {
6565
},
6666
})
6767

68+
export const saveTutorial = mutationField('saveTutorial', {
69+
type: UserTutorialPayload,
70+
description: 'An authenticated user can save a tutorial.',
71+
args: {
72+
tutorialId: idArg({
73+
required: true,
74+
}),
75+
},
76+
authorize: authorizeUser(),
77+
resolve: async (_, { tutorialId }, ctx) => {
78+
const userId = ctx.currentUserId
79+
const existingUserTutorial = await getUserTutorial(
80+
{
81+
userId,
82+
tutorialId,
83+
},
84+
ctx,
85+
)
86+
let upsertedUserTutorial = await upsertUserTutorial(
87+
{
88+
userId,
89+
tutorialId,
90+
userTutorialId: existingUserTutorial && existingUserTutorial.id,
91+
updates: {
92+
saved: existingUserTutorial ? !existingUserTutorial.saved : true,
93+
},
94+
},
95+
ctx,
96+
)
97+
return {
98+
code: '200',
99+
success: true,
100+
message: null,
101+
userTutorial: upsertedUserTutorial,
102+
}
103+
},
104+
})
105+
68106
async function upsertUserTutorial(
69107
args: {
70108
userTutorialId?: string

packages/server/src/schema.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type Mutation {
1818
githubCode: String!
1919
): AuthenticateUserPayload
2020

21+
"""An authenticated user can save a tutorial."""
22+
saveTutorial(tutorialId: ID!): UserTutorialPayload!
23+
2124
"""An authenticated user can upvote a tutorial."""
2225
upvoteTutorial(tutorialId: ID!): UserTutorialPayload!
2326
}

0 commit comments

Comments
 (0)