diff --git a/client/src/components/production-list-table/production-list-table-view-select.component.jsx b/client/src/components/production-list-table/production-list-table-view-select.component.jsx index a89aca21d..044c75994 100644 --- a/client/src/components/production-list-table/production-list-table-view-select.component.jsx +++ b/client/src/components/production-list-table/production-list-table-view-select.component.jsx @@ -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 }, diff --git a/client/src/components/profile-shops/profile-shops.component.jsx b/client/src/components/profile-shops/profile-shops.component.jsx index a668d1a40..9360ed7ad 100644 --- a/client/src/components/profile-shops/profile-shops.component.jsx +++ b/client/src/components/profile-shops/profile-shops.component.jsx @@ -39,7 +39,7 @@ export default function ProfileShopsComponent({ ), }, ]; - console.log("🚀 ~ file: profile-shops.component.jsx:45 ~ data", data); + const filteredData = search === "" ? data diff --git a/client/src/components/profile-shops/profile-shops.container.jsx b/client/src/components/profile-shops/profile-shops.container.jsx index 4b3eaafe1..41bcf010f 100644 --- a/client/src/components/profile-shops/profile-shops.container.jsx +++ b/client/src/components/profile-shops/profile-shops.container.jsx @@ -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. diff --git a/client/src/components/shop-users-auth-edit/shop-users-auth-edit.component.jsx b/client/src/components/shop-users-auth-edit/shop-users-auth-edit.component.jsx index 548d2276e..d99387fe7 100644 --- a/client/src/components/shop-users-auth-edit/shop-users-auth-edit.component.jsx +++ b/client/src/components/shop-users-auth-edit/shop-users-auth-edit.component.jsx @@ -42,7 +42,9 @@ export default function ShopUsersAuthEdit({ association }) { )} {!visible && ( -
setVisible(true)}> +
setVisible(true)} + > {association.authlevel || t("general.labels.na")}
)} diff --git a/client/src/graphql/associations.queries.js b/client/src/graphql/associations.queries.js index b1af48640..09f639b4a 100644 --- a/client/src/graphql/associations.queries.js +++ b/client/src/graphql/associations.queries.js @@ -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) { diff --git a/client/src/graphql/bodyshop.queries.js b/client/src/graphql/bodyshop.queries.js index cfefde25c..64df5f782 100644 --- a/client/src/graphql/bodyshop.queries.js +++ b/client/src/graphql/bodyshop.queries.js @@ -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 diff --git a/hasura/metadata/tables.yaml b/hasura/metadata/tables.yaml index 08e234281..9c0c5d876 100644 --- a/hasura/metadata/tables.yaml +++ b/hasura/metadata/tables.yaml @@ -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