|
| 1 | +import { useState } from 'react'; |
| 2 | +import { FormattedMessage } from 'react-intl'; |
| 3 | +import { useParams } from 'react-router-dom'; |
| 4 | +import ReactPlaceholder from 'react-placeholder'; |
| 5 | +import { useFetchWithAbort } from '../../hooks/UseFetch'; |
| 6 | +import messages from './messages'; |
| 7 | +import statusMessages from '../projectDetail/messages'; |
| 8 | +import { ViewAllLink } from './management'; |
| 9 | +import { CustomMenu } from '../CustomMenu'; |
| 10 | +import TeamLinkedProjectCard, { nTeamProjectsCardPlaceholders } from './TeamLinkedProjectCard'; |
| 11 | +import { useSelector } from 'react-redux'; |
| 12 | +import { fetchLocalJSONAPI } from '../../network/genericJSONRequest'; |
| 13 | +import toast from 'react-hot-toast'; |
| 14 | + |
| 15 | +const items = [ |
| 16 | + { id: 'PUBLISHED', label: <FormattedMessage {...statusMessages.status_PUBLISHED} /> }, |
| 17 | + { id: 'ARCHIVED', label: <FormattedMessage {...statusMessages.status_ARCHIVED} /> }, |
| 18 | + { id: 'DRAFT', label: <FormattedMessage {...statusMessages.status_DRAFT} /> }, |
| 19 | +]; |
| 20 | + |
| 21 | +export function TeamLinkedProjects({ viewAllEndpoint, border = true, canUserEditTeam }: Object) { |
| 22 | + const { id } = useParams(); |
| 23 | + const token = useSelector((state) => state.auth.token); |
| 24 | + const [selectedProjectStatus, setSelectedProjectStatus] = useState('PUBLISHED'); |
| 25 | + |
| 26 | + // eslint-disable-next-line no-unused-vars |
| 27 | + const [projectsError, projectsLoading, projects, refetch] = useFetchWithAbort( |
| 28 | + `projects/?teamId=${id}&omitMapResults=true&projectStatuses=${selectedProjectStatus}`, |
| 29 | + id, |
| 30 | + ); |
| 31 | + |
| 32 | + const unLinkProjectFromTeam = (projectId) => { |
| 33 | + fetchLocalJSONAPI(`teams/${projectId}/teams/${id}/`, token, 'DELETE') |
| 34 | + .then(() => { |
| 35 | + toast.success('Unlinked Successfully'); |
| 36 | + refetch(); |
| 37 | + }) |
| 38 | + .catch((e) => { |
| 39 | + console.log(e.message); |
| 40 | + }); |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <div className={`bg-white mb3 ${border ? 'b--grey-light ba pa4' : ''}`}> |
| 45 | + <h3 className="f3 barlow-condensed ttu blue-dark mv0 fw6 dib v-mid"> |
| 46 | + <FormattedMessage {...messages.projects} /> |
| 47 | + </h3> |
| 48 | + <ViewAllLink link={viewAllEndpoint} /> |
| 49 | + <div className="pv2"> |
| 50 | + <div style={{ width: '230px' }}> |
| 51 | + <CustomMenu |
| 52 | + items={items} |
| 53 | + activeMenuItem={selectedProjectStatus} |
| 54 | + onItemClick={(itemId) => setSelectedProjectStatus(itemId)} |
| 55 | + /> |
| 56 | + </div> |
| 57 | + <div className="pt3"> |
| 58 | + <ReactPlaceholder |
| 59 | + customPlaceholder={nTeamProjectsCardPlaceholders(4)} |
| 60 | + showLoadingAnimation={true} |
| 61 | + delay={10} |
| 62 | + ready={projects?.results} |
| 63 | + > |
| 64 | + {projects?.results?.map((card) => ( |
| 65 | + <TeamLinkedProjectCard |
| 66 | + projectId={card.projectId} |
| 67 | + projectName={card.name} |
| 68 | + unLinkFunc={canUserEditTeam ? unLinkProjectFromTeam : () => {}} |
| 69 | + canUserEditTeam={canUserEditTeam} |
| 70 | + /> |
| 71 | + ))} |
| 72 | + </ReactPlaceholder> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + ); |
| 77 | +} |
0 commit comments