use fragment to update cache instead of direct write

This commit is contained in:
Barry Gitarts 2019-08-27 09:30:08 -04:00 committed by Barry G
parent 69bf7a2b45
commit 36fe68fb86
2 changed files with 32 additions and 10 deletions

View File

@ -18,7 +18,7 @@ import { toBN } from '../../utils/conversions'
import { getTokenLabel, getTokenByAddress } from '../../utils/currencies' import { getTokenLabel, getTokenByAddress } from '../../utils/currencies'
import MediaView from '../base/MediaView' import MediaView from '../base/MediaView'
import StatusTextField from '../base/TextField' import StatusTextField from '../base/TextField'
import { getProfileById } from './queries' import { getProfileById, pledgeLifetimeReceived } from './queries'
import styles from './styles/FundProject' import styles from './styles/FundProject'
import Loading from '../base/Loading' import Loading from '../base/Loading'
import BreadCrumb from '../base/BreadCrumb' import BreadCrumb from '../base/BreadCrumb'
@ -36,8 +36,17 @@ const addProjectSucessMsg = response => {
return `Project created with ID of ${idProject}, will redirect to your new project page in a few seconds` return `Project created with ID of ${idProject}, will redirect to your new project page in a few seconds`
} }
const optimisticUpdate = (client, pledgesInfo, weiAmount) => { const optimisticUpdate = (client, pledgesInfo, weiAmount) => {
const { __typename } = pledgesInfo
const updatedLifetimeReceived = toBN(weiAmount).add(toBN(pledgesInfo.lifetimeReceived)).toString() const updatedLifetimeReceived = toBN(weiAmount).add(toBN(pledgesInfo.lifetimeReceived)).toString()
client.writeData({ data: { [`PledgesInfo:${pledgesInfo.id}`]: {...pledgesInfo, lifetimeReceived: updatedLifetimeReceived } } }) const id = `${__typename}:${pledgesInfo.id}`
client.writeFragment({
id,
fragment: pledgeLifetimeReceived,
data: {
lifetimeReceived: updatedLifetimeReceived,
__typename
}
})
} }
const SubmissionSection = ({ classes, projectData, projectId, profileData, startPolling, client }) => { const SubmissionSection = ({ classes, projectData, projectId, profileData, startPolling, client }) => {
const { account, enableEthereum, openSnackBar, prices } = useContext(FundingContext) const { account, enableEthereum, openSnackBar, prices } = useContext(FundingContext)
@ -78,7 +87,7 @@ const SubmissionSection = ({ classes, projectData, projectId, profileData, start
openSnackBar('success', 'Funding Confirmed') openSnackBar('success', 'Funding Confirmed')
}) })
.catch(e => { .catch(e => {
openSnackBar('error', 'An error has occured with the transaction') openSnackBar('error', 'An error has occured')
console.log({e}) console.log({e})
}) })
.finally(() => { .finally(() => {

View File

@ -1,6 +1,22 @@
import { gql } from 'apollo-boost' import { gql } from 'apollo-boost'
export const pledgeLifetimeReceived = gql`
fragment PledgeLifetimeReceived on PledgesInfo {
lifetimeReceived
}
`
export const pledgesInfosFields = gql`
${pledgeLifetimeReceived}
fragment PledgesInfoFields on PledgesInfo {
id
...PledgeLifetimeReceived
token
}
`
export const getProfileById = gql` export const getProfileById = gql`
${pledgesInfosFields}
query Profile($id: ID!) { query Profile($id: ID!) {
profile(id: $id) { profile(id: $id) {
id id
@ -12,10 +28,7 @@ query Profile($id: ID!) {
name name
creationTime creationTime
pledgesInfos { pledgesInfos {
id ...PledgesInfoFields
token
lifetimeReceived
balance
} }
projectInfo { projectInfo {
id id
@ -36,6 +49,8 @@ query Profile($id: ID!) {
} }
` `
export const getProjects = gql` export const getProjects = gql`
${pledgesInfosFields}
query Projects($type: String! = "PROJECT"){ query Projects($type: String! = "PROJECT"){
profiles(first: 5, where: {type: $type, projectInfo_not: null}) { profiles(first: 5, where: {type: $type, projectInfo_not: null}) {
id id
@ -55,9 +70,7 @@ query Projects($type: String! = "PROJECT"){
goal goal
} }
pledgesInfos{ pledgesInfos{
id ...PledgesInfoFields
lifetimeReceived
token
} }
} }
} }