Skip to content

Commit

Permalink
TS-936 Municipality
Browse files Browse the repository at this point in the history
  • Loading branch information
birkirkristmunds committed Dec 13, 2024
1 parent d1108ea commit a2ce8ec
Show file tree
Hide file tree
Showing 21 changed files with 360 additions and 204 deletions.
3 changes: 3 additions & 0 deletions apps/skilavottord/web/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const hasDeveloperRole = (role: Role | undefined) =>
export const hasMunicipalityRole = (role: Role | undefined) =>
role === Role.municipality

export const hasRecyclingFundRole = (role: Role | undefined) =>
role === Role.recyclingFund

export const hasPermission = (page: Page, role: Role) => {
if (!role) return false

Expand Down
5 changes: 4 additions & 1 deletion apps/skilavottord/web/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,10 @@
},
"municipality": {
"label": "Municipality",
"placeholder": "Municipality"
"placeholder": "Municipality",
"rules": {
"required": "Municipality is required"
}
}
},
"buttons": {
Expand Down
10 changes: 9 additions & 1 deletion apps/skilavottord/web/i18n/locales/is.json
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@
},
"recyclingCompanies": {
"title": "Móttökuaðilar",
"info": "Upplýsingar til að hjálpa viðskiptavinum að hafa samband við móttökuaðila.",
"info": "Skráðir móttökuaðilar",
"empty": "Engin móttökuaðili fannst",
"subtitles": {
"companies": "Móttökuaðilar"
Expand Down Expand Up @@ -582,6 +582,13 @@
"rules": {
"required": "Skylda er að fylla út móttökuaðila"
}
},
"municipality": {
"label": "Sveitarfélag",
"placeholder": "Sveitarfélag",
"rules": {
"required": "Skylda er að fylla út sveitarfélag"
}
}
},
"buttons": {
Expand Down Expand Up @@ -643,6 +650,7 @@
},
"municipalities": {
"title": "Sveitarfélög",
"empty": "Ekkert sveitarfélag fannst",
"municipality": {
"view": {
"title": "Sveitarfélag",
Expand Down
1 change: 1 addition & 0 deletions apps/skilavottord/web/i18n/locales/translation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface ModalInputs {
recyclingLocation: RecyclingLocation
role: Name
partner: Name
municipality: Name
}

export interface Email {
Expand Down
56 changes: 43 additions & 13 deletions apps/skilavottord/web/screens/AccessControl/AccessControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@island.is/island-ui/core'
import {
hasDeveloperRole,
hasMunicipalityRole,
hasPermission,
} from '@island.is/skilavottord-web/auth/utils'
import { NotFound } from '@island.is/skilavottord-web/components'
Expand All @@ -41,7 +42,7 @@ import { AccessControlCreate, AccessControlUpdate } from './components'

import NavigationLinks from '@island.is/skilavottord-web/components/NavigationLinks/NavigationLinks'
import PageHeader from '@island.is/skilavottord-web/components/PageHeader/PageHeader'
import { SkilavottordAllRecyclingPartnersByTypeQuery } from '../RecyclingCompanies/RecyclingCompanies'
import { SkilavottordRecyclingPartnersQuery } from '../RecyclingCompanies/RecyclingCompanies'
import * as styles from './AccessControl.css'

const SkilavottordAllRecyclingPartnersQuery = gql`
Expand All @@ -51,6 +52,7 @@ const SkilavottordAllRecyclingPartnersQuery = gql`
companyName
active
municipalityId
isMunicipality
}
}
`
Expand All @@ -67,6 +69,7 @@ const SkilavottordAccessControlsQuery = gql`
companyId
companyName
municipalityId
isMunicipality
}
}
}
Expand All @@ -82,9 +85,11 @@ export const CreateSkilavottordAccessControlMutation = gql`
role
email
phone
partnerId
recyclingPartner {
companyId
companyName
municipalityId
}
}
}
Expand Down Expand Up @@ -138,9 +143,12 @@ const AccessControl: FC<React.PropsWithChildren<unknown>> = () => {
error: recyclingPartnerByIdError,
loading: recyclingPartnerByIdLoading,
},
] = useLazyQuery<Query>(SkilavottordAllRecyclingPartnersByTypeQuery, {
] = useLazyQuery<Query>(SkilavottordRecyclingPartnersQuery, {
ssr: false,
variables: { isMunicipality: false, municipalityId: user?.partnerId },
variables: {
isMunicipalityPage: false,
municipalityId: user?.partnerId,
},
})

const {
Expand Down Expand Up @@ -220,21 +228,34 @@ const AccessControl: FC<React.PropsWithChildren<unknown>> = () => {

const partners =
recyclingPartnerData?.skilavottordAllRecyclingPartners ||
recyclingPartnerByIdData?.skilavottordAllRecyclingPartnersByType ||
recyclingPartnerByIdData?.skilavottordRecyclingPartners ||
[]
const recyclingPartners = filterInternalPartners(partners).map((partner) => ({
label: partner.municipalityId
? `${partner.municipalityId} - ${partner.companyName}`
: partner.companyName,
value: partner.companyId,
}))
const recyclingPartners = filterInternalPartners(partners)
.filter((partner) => {
return !partner.isMunicipality
})
.map((partner) => ({
label: partner.municipalityId
? `${partner.municipalityId} - ${partner.companyName}`
: partner.companyName,
value: partner.companyId,
}))

const municipalities = filterInternalPartners(partners)
.filter((partner) => {
return partner.isMunicipality
})
.map((partner) => ({
label: partner.companyName,
value: partner.companyId,
}))

const roles = Object.keys(AccessControlRole)
.filter((role) =>
!hasDeveloperRole(user?.role) ? role !== Role.developer : role,
)
.filter((role) => {
if (user.role == Role.municipality) {
if (hasMunicipalityRole(Role.municipality)) {
return (
role === Role.recyclingCompany ||
role === Role.recyclingCompanyAdmin ||
Expand All @@ -253,7 +274,7 @@ const AccessControl: FC<React.PropsWithChildren<unknown>> = () => {
setIsCreateAccessControlModalVisible(false)

const handleCreateAccessControlOpenModal = () => {
if (user.role === Role.municipality) {
if (hasMunicipalityRole(user?.role)) {
getAllRecyclingPartnersByMunicipality()
} else {
getAllRecyclingPartner()
Expand Down Expand Up @@ -323,6 +344,7 @@ const AccessControl: FC<React.PropsWithChildren<unknown>> = () => {
onCancel={handleCreateAccessControlCloseModal}
onSubmit={handleCreateAccessControl}
recyclingPartners={recyclingPartners}
municipalities={municipalities}
roles={roles}
/>
</Box>
Expand Down Expand Up @@ -384,7 +406,14 @@ const AccessControl: FC<React.PropsWithChildren<unknown>> = () => {
items={[
{
title: t.buttons.edit,
onClick: () => setPartner(item),
onClick: () => {
if (hasMunicipalityRole(user?.role)) {
getAllRecyclingPartnersByMunicipality()
} else {
getAllRecyclingPartner()
}
setPartner(item)
},
},
{
title: t.buttons.delete,
Expand Down Expand Up @@ -438,6 +467,7 @@ const AccessControl: FC<React.PropsWithChildren<unknown>> = () => {
recyclingPartners={recyclingPartners}
roles={roles}
currentPartner={partner}
municipalities={municipalities}
/>
</PartnerPageLayout>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react'
import React, { FC, useContext, useEffect } from 'react'
import { useForm } from 'react-hook-form'

import { Option } from '@island.is/island-ui/core'
Expand All @@ -9,6 +9,8 @@ import {
} from '@island.is/skilavottord-web/graphql/schema'

import { AccessControlModal } from '../AccessControlModal/AccessControlModal'
import { UserContext } from '@island.is/skilavottord-web/context'
import { hasMunicipalityRole } from '@island.is/skilavottord-web/auth/utils'

interface AccessControlCreateProps
extends Omit<
Expand All @@ -18,12 +20,25 @@ interface AccessControlCreateProps
onSubmit: (partner: CreateAccessControlInput) => Promise<void>
recyclingPartners: Option[]
roles: Option[]
municipalities: Option[]
}

export const AccessControlCreate: FC<
React.PropsWithChildren<AccessControlCreateProps>
> = ({ title, text, show, onCancel, onSubmit, recyclingPartners, roles }) => {
> = ({
title,
text,
show,
onCancel,
onSubmit,
recyclingPartners,
roles,
municipalities,
}) => {
const { user } = useContext(UserContext)

const {
reset,
control,
handleSubmit,
watch,
Expand All @@ -32,19 +47,38 @@ export const AccessControlCreate: FC<
mode: 'onChange',
})

const getPartnerId = (
municipalityId: string,
partnerId: string,
role: Role,
) => {
// If the user has municipality role, then he can only create a new access under the same municipality
if (hasMunicipalityRole(user?.role) && hasMunicipalityRole(role)) {
return user.partnerId
}

// If selected role is municipality, use municipalityId, else use partnerId
return hasMunicipalityRole(role) ? municipalityId : partnerId || null
}

const handleOnSubmit = handleSubmit(
({ nationalId, name, role, partnerId, email, phone }) => {
({ nationalId, name, role, partnerId, email, phone, municipalityId }) => {
return onSubmit({
nationalId,
name,
phone,
email,
role: role.value,
partnerId: partnerId?.value,
partnerId: getPartnerId(municipalityId, partnerId, role),
})
},
)

useEffect(() => {
// clear the form if re-opened
reset()
}, [show, reset])

return (
<AccessControlModal
title={title}
Expand All @@ -53,10 +87,14 @@ export const AccessControlCreate: FC<
onCancel={onCancel}
onSubmit={handleOnSubmit}
recyclingPartners={recyclingPartners}
municipalities={municipalities}
roles={roles}
control={control}
errors={errors}
partnerIdRequired={watch('role')?.value === Role.recyclingCompanyAdmin}
partnerIdRequired={
watch('role')?.value === Role.recyclingCompanyAdmin ||
watch('role')?.value === Role.recyclingCompany
}
/>
)
}
Loading

0 comments on commit a2ce8ec

Please sign in to comment.