Improve changing active shop profile methods to avoid multiple queries.

This commit is contained in:
Patrick Fic
2023-03-02 20:04:54 -08:00
parent 2415b4c2b4
commit 99d3943955
7 changed files with 62 additions and 27 deletions

View File

@@ -55,6 +55,7 @@ export function ProductionListTable({
const assoc = bodyshop.associations.find(
(a) => a.useremail === currentUser.email
);
if (assoc) {
await updateDefaultProdView({
variables: { assocId: assoc.id, view: value },

View File

@@ -39,7 +39,7 @@ export default function ProfileShopsComponent({
),
},
];
console.log("🚀 ~ file: profile-shops.component.jsx:45 ~ data", data);
const filteredData =
search === ""
? data

View File

@@ -3,7 +3,7 @@ import React from "react";
import { logImEXEvent } from "../../firebase/firebase.utils";
import {
QUERY_ALL_ASSOCIATIONS,
UPDATE_ASSOCIATION,
UPDATE_ACTIVE_ASSOCIATION,
} from "../../graphql/associations.queries";
import AlertComponent from "../alert/alert.component";
import ProfileShopsComponent from "./profile-shops.component";
@@ -13,9 +13,13 @@ import { getToken } from "firebase/messaging";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import {
selectBodyshop,
selectCurrentUser,
} from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
currentUser: selectCurrentUser,
});
const mapDispatchToProps = (dispatch) => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
@@ -25,14 +29,18 @@ export default connect(
mapDispatchToProps
)(ProfileShopsContainer);
export function ProfileShopsContainer({ bodyshop }) {
export function ProfileShopsContainer({ bodyshop, currentUser }) {
const { loading, error, data } = useQuery(QUERY_ALL_ASSOCIATIONS, {
fetchPolicy: "network-only",
nextFetchPolicy: "network-only",
variables: {
email: currentUser.email,
},
skip: !currentUser,
});
const [updateAssocation] = useMutation(UPDATE_ASSOCIATION);
const [updateActiveAssociation] = useMutation(UPDATE_ACTIVE_ASSOCIATION);
const updateActiveShop = async (activeShopId) => {
const updateActiveShop = async (newActiveAssocId) => {
logImEXEvent("profile_change_active_shop");
try {
@@ -46,16 +54,12 @@ export function ProfileShopsContainer({ bodyshop }) {
} catch (error) {
console.log("No FCM token. Skipping unsubscribe.");
}
await Promise.all(
data.associations.map(async (record) => {
await updateAssocation({
variables: {
assocId: record.id,
assocActive: record.id === activeShopId ? true : false,
},
});
})
);
await updateActiveAssociation({
variables: {
newActiveAssocId: newActiveAssocId,
},
});
//Force window refresh.

View File

@@ -42,7 +42,9 @@ export default function ShopUsersAuthEdit({ association }) {
</div>
)}
{!visible && (
<div style={{ cursor: "pointer" }} onClick={() => setVisible(true)}>
<div
style={{ cursor: "pointer" }} //onClick={() => setVisible(true)}
>
{association.authlevel || t("general.labels.na")}
</div>
)}

View File

@@ -1,8 +1,11 @@
import { gql } from "@apollo/client";
export const QUERY_ALL_ASSOCIATIONS = gql`
query QUERY_ALL_ASSOCIATIONS {
associations(order_by: { bodyshop: { shopname: asc } }) {
query QUERY_ALL_ASSOCIATIONS($email: String) {
associations(
where: { useremail: { _eq: $email } }
order_by: { bodyshop: { shopname: asc } }
) {
id
active
bodyshop {
@@ -27,6 +30,30 @@ export const UPDATE_ASSOCIATION = gql`
}
}
`;
export const UPDATE_ACTIVE_ASSOCIATION = gql`
mutation UPDATE_ACTIVE_ASSOCIATION($newActiveAssocId: uuid) {
nweActive: update_associations(
where: { id: { _eq: $newActiveAssocId } }
_set: { active: true }
) {
returning {
id
shopid
active
}
}
inactive: update_associations(
where: { id: { _neq: $newActiveAssocId } }
_set: { active: false }
) {
returning {
id
shopid
active
}
}
}
`;
export const UPDATE_ACTIVE_PROD_LIST_VIEW = gql`
mutation UPDATE_ACTIVE_PROD_LIST_VIEW($assocId: uuid, $view: String) {

View File

@@ -12,6 +12,7 @@ export const QUERY_BODYSHOP = gql`
query QUERY_BODYSHOP {
bodyshops(where: { associations: { active: { _eq: true } } }) {
associations {
id
authlevel
useremail
default_prod_list_view

View File

@@ -210,9 +210,11 @@
- shopid
- useremail
filter:
user:
authid:
_eq: X-Hasura-User-Id
bodyshop:
associations:
user:
authid:
_eq: X-Hasura-User-Id
update_permissions:
- role: user
permission:
@@ -223,11 +225,9 @@
- kanban_settings
- qbo_realmId
filter:
bodyshop:
associations:
user:
authid:
_eq: X-Hasura-User-Id
user:
authid:
_eq: X-Hasura-User-Id
check: null
- table:
name: audit_trail