Files
bodyshop/client/src/components/profile-shops/profile-shops.container.jsx
2021-12-15 14:34:24 -08:00

68 lines
2.0 KiB
JavaScript

import { useMutation, useQuery } from "@apollo/client";
import React from "react";
import { logImEXEvent } from "../../firebase/firebase.utils";
import {
QUERY_ALL_ASSOCIATIONS,
UPDATE_ASSOCIATION,
} from "../../graphql/associations.queries";
import AlertComponent from "../alert/alert.component";
import ProfileShopsComponent from "./profile-shops.component";
import axios from "axios";
import { messaging } from "../../firebase/firebase.utils";
import { getToken } from "firebase/messaging";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
const mapDispatchToProps = (dispatch) => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(ProfileShopsContainer);
export function ProfileShopsContainer({ bodyshop }) {
const { loading, error, data } = useQuery(QUERY_ALL_ASSOCIATIONS, {
fetchPolicy: "network-only",
nextFetchPolicy: "network-only",
});
const [updateAssocation] = useMutation(UPDATE_ASSOCIATION);
const updateActiveShop = async (activeShopId) => {
logImEXEvent("profile_change_active_shop");
await axios.post("/notifications/unsubscribe", {
fcm_tokens: await getToken(messaging),
imexshopid: bodyshop.imexshopid,
type: "messaging",
});
await Promise.all(
data.associations.map(async (record) => {
await updateAssocation({
variables: {
assocId: record.id,
assocActive: record.id === activeShopId ? true : false,
},
});
})
);
//Force window refresh.
window.location.reload();
};
if (error) return <AlertComponent type="error" message={error.message} />;
return (
<ProfileShopsComponent
loading={loading}
data={data ? data.associations : null}
updateActiveShop={updateActiveShop}
/>
);
}