Skip to content

Commit

Permalink
feat(ajout): redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
alebret committed Feb 3, 2023
1 parent 6959941 commit df6b1c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 1 addition & 3 deletions apollo-client.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ApolloClient, InMemoryCache, createHttpLink } from "@apollo/client"
import { setContext } from '@apollo/client/link/context';


const API_URL = process.env.NEXT_PUBLIC_API_URL

const httpLink = createHttpLink({
uri: `${API_URL}/graphql?nocache`,
});

const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token');
const token = localStorage.getItem("token");
return {
headers: {
...headers,
Expand All @@ -18,7 +17,6 @@ const authLink = setContext((_, { headers }) => {
}
});


export const client = new ApolloClient({
cache: new InMemoryCache(),
headers: {
Expand Down
19 changes: 14 additions & 5 deletions pages/ajout-demande.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Fragment, useEffect, useState } from "react";
import { useSession } from "next-auth/react";
import { useLazyQuery, useMutation } from '@apollo/client';
import { client } from "../apollo-client"

import { useRouter } from "next/router";
import { useStyles } from "tss-react/dsfr";
import { LoggedState } from "../src/components/LoggedState";
import DatabaseApi from "../src/services/api/database";
Expand All @@ -24,6 +24,7 @@ import Alert from "@codegouvfr/react-dsfr/Alert";
export default function AjoutDemande() {
const { css } = useStyles();
const widthFitContent = css({ width: "fit-content" })
const router = useRouter()

const { status } = useSession()
const [isLogged, setLogged] = useState<Boolean>()
Expand All @@ -33,7 +34,7 @@ export default function AjoutDemande() {
const [selectedContactOrigin, setSelectedContactOrigin] = useState<string>()
const [selectedContactMode, setSelectedContactMode] = useState<Enum_Contacts_Mode>()
const [selectedContactSupport, setSelectedContactSupport] = useState<Enum_Contacts_Personne_Accompagnee>()
const [contactData, setContactData] = useState<any>()
const [isLoading, setLoading] = useState(false)

useEffect(() => {
setLogged(status === "authenticated")
Expand All @@ -53,8 +54,9 @@ export default function AjoutDemande() {
const [saveContactRequest] = useMutation(
DatabaseApi.SAVE_CONTACT, {
client: client,
onCompleted: (data) => {
console.log(data)
onCompleted: () => {
setLoading(false)
goToHome()
},
onError: (err) => console.error(err),
});
Expand All @@ -77,11 +79,18 @@ export default function AjoutDemande() {
commentaire: target.comment.value
}

setLoading(true)
await saveContactRequest({
variables: dataCollected
})
}

const goToHome = () => {
router.push({
pathname: "/",
})
}

return <div>
<LoggedState showButton={false} />

Expand Down Expand Up @@ -161,7 +170,7 @@ export default function AjoutDemande() {
id: "comment"
}} />

<Button type="submit" onClick={() => { }}>Créer le contact</Button>
<Button type="submit" onClick={() => { }} disabled={isLoading}>Créer le contact</Button>
</form>
}
</div>
Expand Down

0 comments on commit df6b1c7

Please sign in to comment.