* create RecordTableScope * use RecordTableScope * working on useRecordTable hook * add RecordTableScope to company-table * add RecordTableScope to person-table * add filter state and sort state * add useSetRecordTableData to useRecordTable * wip * add setRecordTableData to useRecordTable * update in RecordTableEffect * fix bug * getting rid of unnecessary context and hooks * remove console.log * wip * fix bug by creating an init effect * fix viewbar not in scope in company and people tables * wip * updating useRecordTable to use internal hooks * updating useRecordTable to use internal hooks * updating useRecordTable to use internal hooks * updating useRecordTable to use internal hooks * modified according to comments
150 lines
5.5 KiB
TypeScript
150 lines
5.5 KiB
TypeScript
import styled from '@emotion/styled';
|
|
import { useRecoilCallback } from 'recoil';
|
|
|
|
import { companiesAvailableFieldDefinitions } from '@/companies/constants/companiesAvailableFieldDefinitions';
|
|
import { getCompaniesOptimisticEffectDefinition } from '@/companies/graphql/optimistic-effect-definitions/getCompaniesOptimisticEffectDefinition';
|
|
import { useCompanyTableContextMenuEntries } from '@/companies/hooks/useCompanyTableContextMenuEntries';
|
|
import { useSpreadsheetCompanyImport } from '@/companies/hooks/useSpreadsheetCompanyImport';
|
|
import { RecordTable } from '@/ui/object/record-table/components/RecordTable';
|
|
import { RecordTableEffect } from '@/ui/object/record-table/components/RecordTableEffect';
|
|
import { TableOptionsDropdownId } from '@/ui/object/record-table/constants/TableOptionsDropdownId';
|
|
import { useRecordTable } from '@/ui/object/record-table/hooks/useRecordTable';
|
|
import { TableOptionsDropdown } from '@/ui/object/record-table/options/components/TableOptionsDropdown';
|
|
import { RecordTableScope } from '@/ui/object/record-table/scopes/RecordTableScope';
|
|
import { ViewBar } from '@/views/components/ViewBar';
|
|
import { useViewFields } from '@/views/hooks/internal/useViewFields';
|
|
import { useView } from '@/views/hooks/useView';
|
|
import { ViewScope } from '@/views/scopes/ViewScope';
|
|
import { mapColumnDefinitionsToViewFields } from '@/views/utils/mapColumnDefinitionToViewField';
|
|
import { mapViewFieldsToColumnDefinitions } from '@/views/utils/mapViewFieldsToColumnDefinitions';
|
|
import { mapViewFiltersToFilters } from '@/views/utils/mapViewFiltersToFilters';
|
|
import { mapViewSortsToSorts } from '@/views/utils/mapViewSortsToSorts';
|
|
import {
|
|
UpdateOneCompanyMutationVariables,
|
|
useGetCompaniesQuery,
|
|
useGetWorkspaceMembersLazyQuery,
|
|
useUpdateOneCompanyMutation,
|
|
} from '~/generated/graphql';
|
|
import { companyTableFilterDefinitions } from '~/pages/companies/constants/companyTableFilterDefinitions';
|
|
import { companyTableSortDefinitions } from '~/pages/companies/constants/companyTableSortDefinitions';
|
|
|
|
import CompanyTableEffect from './CompanyTableEffect';
|
|
|
|
const StyledContainer = styled.div`
|
|
display: flex;
|
|
flex-direction: column;
|
|
height: 100%;
|
|
overflow: auto;
|
|
`;
|
|
|
|
export const CompanyTable = () => {
|
|
const viewScopeId = 'company-table-view';
|
|
const tableScopeId = 'companies';
|
|
|
|
const {
|
|
setTableFilters,
|
|
setTableSorts,
|
|
setTableColumns,
|
|
upsertRecordTableItem,
|
|
} = useRecordTable({
|
|
recordTableScopeId: tableScopeId,
|
|
});
|
|
|
|
const [updateEntityMutation] = useUpdateOneCompanyMutation();
|
|
|
|
const [getWorkspaceMember] = useGetWorkspaceMembersLazyQuery();
|
|
const { persistViewFields } = useViewFields(viewScopeId);
|
|
const { setEntityCountInCurrentView } = useView({ viewScopeId });
|
|
|
|
const { setContextMenuEntries, setActionBarEntries } =
|
|
useCompanyTableContextMenuEntries();
|
|
|
|
const updateCompany = async (
|
|
variables: UpdateOneCompanyMutationVariables,
|
|
) => {
|
|
if (variables.data.accountOwner?.connect?.id) {
|
|
const workspaceMemberAccountOwner = (
|
|
await getWorkspaceMember({
|
|
variables: {
|
|
where: {
|
|
userId: { equals: variables.data.accountOwner.connect?.id },
|
|
},
|
|
},
|
|
})
|
|
).data?.workspaceMembers?.[0];
|
|
variables.data.workspaceMemberAccountOwner = {
|
|
connect: { id: workspaceMemberAccountOwner?.id },
|
|
};
|
|
}
|
|
|
|
updateEntityMutation({
|
|
variables: variables,
|
|
onCompleted: (data) => {
|
|
if (!data.updateOneCompany) {
|
|
return;
|
|
}
|
|
upsertRecordTableItem(data.updateOneCompany);
|
|
},
|
|
});
|
|
};
|
|
|
|
const { openCompanySpreadsheetImport: onImport } =
|
|
useSpreadsheetCompanyImport();
|
|
|
|
return (
|
|
<ViewScope
|
|
viewScopeId={viewScopeId}
|
|
onViewFieldsChange={(viewFields) => {
|
|
setTableColumns(
|
|
mapViewFieldsToColumnDefinitions(
|
|
viewFields,
|
|
companiesAvailableFieldDefinitions,
|
|
),
|
|
);
|
|
}}
|
|
onViewFiltersChange={(viewFilters) => {
|
|
setTableFilters(mapViewFiltersToFilters(viewFilters));
|
|
}}
|
|
onViewSortsChange={(viewSorts) => {
|
|
setTableSorts(mapViewSortsToSorts(viewSorts));
|
|
}}
|
|
>
|
|
<StyledContainer>
|
|
<RecordTableScope
|
|
recordTableScopeId={tableScopeId}
|
|
onColumnsChange={useRecoilCallback(() => (columns) => {
|
|
persistViewFields(mapColumnDefinitionsToViewFields(columns));
|
|
})}
|
|
onEntityCountChange={useRecoilCallback(() => (entityCount) => {
|
|
setEntityCountInCurrentView(entityCount);
|
|
})}
|
|
>
|
|
<ViewBar
|
|
optionsDropdownButton={<TableOptionsDropdown onImport={onImport} />}
|
|
optionsDropdownScopeId={TableOptionsDropdownId}
|
|
/>
|
|
<CompanyTableEffect />
|
|
<RecordTableEffect
|
|
getRequestResultKey="companies"
|
|
useGetRequest={useGetCompaniesQuery}
|
|
getRequestOptimisticEffectDefinition={
|
|
getCompaniesOptimisticEffectDefinition
|
|
}
|
|
filterDefinitionArray={companyTableFilterDefinitions}
|
|
sortDefinitionArray={companyTableSortDefinitions}
|
|
setContextMenuEntries={setContextMenuEntries}
|
|
setActionBarEntries={setActionBarEntries}
|
|
/>
|
|
<RecordTable
|
|
updateEntityMutation={({
|
|
variables,
|
|
}: {
|
|
variables: UpdateOneCompanyMutationVariables;
|
|
}) => updateCompany(variables)}
|
|
/>
|
|
</RecordTableScope>
|
|
</StyledContainer>
|
|
</ViewScope>
|
|
);
|
|
};
|