Skip to content

Instantly share code, notes, and snippets.

@viperfx
Last active November 10, 2020 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viperfx/55a0055bb31f997d006e0b20edb177ff to your computer and use it in GitHub Desktop.
Save viperfx/55a0055bb31f997d006e0b20edb177ff to your computer and use it in GitHub Desktop.
Redwood transactions and DB writes
const FeedbackPage = ({id}) => {
const { logIn, logOut, isAuthenticated, currentUser } = useAuth()
let { filter } = useParams()
const { addMessage } = useFlash()
return (
<CoreLayout>
<PageHeader title={"Feedback"}/>
<div class="px-4 mt-6 sm:px-6 lg:px-8">
<FeedbackListCell teamId={currentUser.currentTeam.id} filter={filter}/>
</div>
</CoreLayout>
)
}
export const getCurrentUser = async (decoded, { _token, _type }) => {
const account = await db.user.findOne(
{
where: {sub: decoded.sub},
include: {
currentTeam: true
}
},
)
if (!account) {
const newUser = await db.user.create({
data: {
email: decoded.email,
sub: decoded.sub,
}
})
const currentUser = {
...newUser,
currentTeam: null
}
}
const currentUser = {
userId: account.id,
email: account.email,
currentTeam: account.currentTeam,
}
console.log(currentUser)
return { ...currentUser }
}
const CREATE_CONTACT = gql`
mutation CreateTeamWithNameMutation($input: CreateTeamInput!) {
createTeamWithName(input: $input) {
id
name
}
}
`
const PickTeamPage = () => {
const { isAuthenticated, currentUser } = useAuth()
if (currentUser.currentTeam != null) {
return <Redirect to={routes.feedback()} />
}
const [create, {loading, error}] = useMutation(CREATE_CONTACT, {
onCompleted: async () => {
// await new Promise(r => setTimeout(r, 5000));
navigate(routes.feedback())
},
})
const onSubmit = (data) => {
create({ variables: { input: data } })
}
export const createTeamWithName = async ({input}) => {
requireAuth()
const newTeam = await db.team.create({
data: {
name: input.name,
owner: {
connect: {id: context.currentUser.userId}
}
},
})
return newTeam
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment