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( const assoc = bodyshop.associations.find(
(a) => a.useremail === currentUser.email (a) => a.useremail === currentUser.email
); );
if (assoc) { if (assoc) {
await updateDefaultProdView({ await updateDefaultProdView({
variables: { assocId: assoc.id, view: value }, 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 = const filteredData =
search === "" search === ""
? data ? data

View File

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

View File

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

View File

@@ -1,8 +1,11 @@
import { gql } from "@apollo/client"; import { gql } from "@apollo/client";
export const QUERY_ALL_ASSOCIATIONS = gql` export const QUERY_ALL_ASSOCIATIONS = gql`
query QUERY_ALL_ASSOCIATIONS { query QUERY_ALL_ASSOCIATIONS($email: String) {
associations(order_by: { bodyshop: { shopname: asc } }) { associations(
where: { useremail: { _eq: $email } }
order_by: { bodyshop: { shopname: asc } }
) {
id id
active active
bodyshop { 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` export const UPDATE_ACTIVE_PROD_LIST_VIEW = gql`
mutation UPDATE_ACTIVE_PROD_LIST_VIEW($assocId: uuid, $view: String) { mutation UPDATE_ACTIVE_PROD_LIST_VIEW($assocId: uuid, $view: String) {

View File

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

View File

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