mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2026-04-03 08:05:37 +00:00
54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
import {
|
|
generatePersonalInviteCode as generateMutation,
|
|
invalidateInviteCode as invalidateMutation,
|
|
} from '~/graphql/InviteCode'
|
|
|
|
export function useInviteCode({ apollo, toast, t, store }) {
|
|
async function generatePersonalInviteCode(comment) {
|
|
try {
|
|
await apollo.mutate({
|
|
mutation: generateMutation(),
|
|
variables: { comment },
|
|
update: (_, { data: { generatePersonalInviteCode: newCode } }) => {
|
|
const user = store.getters['auth/user']
|
|
store.commit('auth/SET_USER_PARTIAL', {
|
|
...user,
|
|
inviteCodes: [...user.inviteCodes, newCode],
|
|
})
|
|
},
|
|
})
|
|
toast.success(t('invite-codes.create-success'))
|
|
return { success: true }
|
|
} catch (error) {
|
|
toast.error(t('invite-codes.create-error', { error: error.message }))
|
|
return { success: false }
|
|
}
|
|
}
|
|
|
|
async function invalidateInviteCode(code) {
|
|
try {
|
|
await apollo.mutate({
|
|
mutation: invalidateMutation(),
|
|
variables: { code },
|
|
update: () => {
|
|
const user = store.getters['auth/user']
|
|
store.commit('auth/SET_USER_PARTIAL', {
|
|
...user,
|
|
inviteCodes: user.inviteCodes.map((inviteCode) => ({
|
|
...inviteCode,
|
|
isValid: inviteCode.code === code ? false : inviteCode.isValid,
|
|
})),
|
|
})
|
|
},
|
|
})
|
|
toast.success(t('invite-codes.invalidate-success'))
|
|
return { success: true }
|
|
} catch (error) {
|
|
toast.error(t('invite-codes.invalidate-error', { error: error.message }))
|
|
return { success: false }
|
|
}
|
|
}
|
|
|
|
return { generatePersonalInviteCode, invalidateInviteCode }
|
|
}
|