import { client } from 'lib/apollo_graphql/client' ; import { updateAgreement } from 'lib/apollo_graphql/mutations/updateAgreement' ; import { ProfileNode , updateProfile } from 'lib/apollo_graphql/mutations/updateProfile' ; import { uploadProfileImage } from 'lib/apollo_graphql/mutations/uploadProfileImage' ; import { getMe } from 'lib/apollo_graphql/queries/getMe' ; import { action , configure , observable , computed } from 'mobx' ; configure ( { enforceActions : 'always' } ) ; export class ProfileStore { @ observable isInitialized = false ; @ observable isAgreed = false ; @ observable isStaff = false ; @ observable isSuperuser = false ; @ observable profile : ProfileNode | { } = { } ; @ action async retrieveMe ( ) { const response = await getMe ( client ) ( { } ) ; if ( response . data . me ) { this . profile = { ... response . data . me . profile } ; this . isStaff = response . data . me . isStaff || false ; this . isSuperuser = response . data . me . isSuperuser || false ; this . isAgreed = response . data . me . isAgreed || false ; this . isInitialized = true ; } return response . data . me ; } @ action async updateAgreement ( agreements ) { const response = await updateAgreement ( client ) ( { ... agreements } ) ; if ( response . data . updateAgreement . isAgreedAll ) { await this . retrieveMe ( ) ; } return response . data . updateAgreement ; } @ action clearUser ( ) { this . isStaff = false ; this . isAgreed = false ; this . isSuperuser = false ; this . profile = { } ; } @ action setProfile ( profile ) { this . profile = { ... profile } ; } @ action async updateProfile ( profile ) { if ( profile && profile . hasOwnProperty ( '__typename' ) ) { delete profile . __typename ; } return updateProfile ( client ) ( { data : profile } ) . then ( response => { this . setProfile ( response . data . updateProfile . profile ) ; return response . data . updateProfile . profile ; } ) ; } @ action async uploadProfileImage ( file ) { const response = await uploadProfileImage ( client ) ( { file } ) ; const image = response . data . uploadProfileImage . image ; this . profile = { ... this . profile , image } ; return image ; } @ computed get hasStaffPermission ( ) { return this . isStaff || this . isSuperuser ; } } export default new ProfileStore ( ) ;