Skip to content

Commit

Permalink
finished rol store
Browse files Browse the repository at this point in the history
  • Loading branch information
SudoThijn committed Oct 10, 2024
1 parent 8f5b872 commit edcf230
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 47 deletions.
6 changes: 3 additions & 3 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
- only one modal may be active at all times
- modals should be abstract and reachable form anywhere
- modals should be places in te src/modals folder
- modals should be triggerd through the state
- modals should be (er)ussable oudise of the view contect and stered in src/modals/[modal-name]
- modals should be triggered through the state
- modals should be (re)usable outside of the view content and stored in src/modals/[modal-name]

## Views

- vieuws should be centered around a core object
- views should be centered around a core object
- views should consist of a index, list and details page
- view files should be stored onder src/views/[view-name]

Expand Down
1 change: 1 addition & 0 deletions src/entities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './zaakTypen/index.js'
export * from './klanten/index.js'
export * from './taak/index.js'
export * from './bericht/index.js'
export * from './rol/index.js'
2 changes: 1 addition & 1 deletion src/entities/rol/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './rol.ts'
export * from './rol.types.ts'
export * from './zaak.mock.ts'
export * from './rol.mock.ts'
39 changes: 39 additions & 0 deletions src/entities/rol/rol.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Rol } from './rol'
import { TRol } from './rol.types'

export const mockRolData = (): TRol[] => [
{
uuid: '15551d6f-44e3-43f3-a9d2-59e583c91eb0',
omschrijving: 'Zaak 3',
omschrijvingGeneriek: 'Zaak 3',
url: 'http://example.com',
zaak: 'ZAAK-2024-3',
betrokkene: 'string',
betrokkeneType: 'string',
afwijkendeNaamBetrokkene: '0a3a0ffb-dc03-4aae-b207-0ed1502e60da',
roltype: 'gearchiveerd_procestermijn_onbekend',
roltoelichting: '2019-08-24',
registratiedatum: 'string',
indicatieMachtiging: '2019-08-24',
contactpersoonRol: {
emailadres: '[email protected]',
functie: 'something',
telefoonnummer: '06 123456789',
naam: 'Henry',
},
statussen: ['active', 'nieuw'],
_expand: {
zaak: '',
roltype: '',
statussen: '',
},
betrokkeneIdentificatie: {
identificatie: '',
achternaam: '',
voorletters: '',
voorvoegselAchternaam: '',
},
},
]

export const mockRol = (data: TRol[] = mockRolData()): TRol[] => data.map(item => new Rol(item))
12 changes: 12 additions & 0 deletions src/entities/rol/rol.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Rol } from './rol'
import { mockRolData } from './rol.mock'

describe('Rol Entity', () => {
it('should create a Rol entity with full data', () => {
const rol = new Rol(mockRolData()[0])

expect(rol).toBeInstanceOf(Rol)
expect(rol).toEqual(mockRolData()[0])
expect(rol.validate().success).toBe(true)
})
})
30 changes: 0 additions & 30 deletions src/entities/rol/zaak.mock.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/entities/rol/zaak.spec.ts

This file was deleted.

125 changes: 125 additions & 0 deletions src/store/modules/rol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* eslint-disable no-console */
import { defineStore } from 'pinia'
import { Rol } from '../../entities/index.js'

const apiEndpoint = '/index.php/apps/zaakafhandelapp/api/zrc/rollen'

export const useRolStore = defineStore('rollen', {
state: () => ({
rolItem: false,
rollenList: [],
}),
actions: {
setRolItem(rolItem) {
this.rolItem = rolItem && new Rol(rolItem)
console.log('Active rol item set to ' + rolItem)
},
setRollenList(rollenList) {
this.rollenList = rollenList.map(
(rolItem) => new Rol(rolItem),
)
console.log('Rollen list set to ' + rollenList.length + ' items')
},
/* istanbul ignore next */ // ignore this for Jest until moved into a service
async refreshRollenList(search = null) {
let endpoint = apiEndpoint

if (search !== null && search !== '') {
endpoint = endpoint + '?_search=' + search
}

const response = await fetch(endpoint, {
method: 'GET',
})

if (!response.ok) {
console.log(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

const data = (await response.json()).results
const entities = data.map((rolItem) => new Rol(rolItem))

this.setRollenList(data)

return { response, data, entities }
},
// New function to get a single rol
async getRol(id) {
const endpoint = `${apiEndpoint}/${id}`

const response = await fetch(endpoint, {
method: 'GET',
})

if (!response.ok) {
console.log(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

const data = await response.json()
const entity = new Rol(data)

this.setRolItem(data)

return { response, data, entity }
},
// Delete a rol
async deleteRol(rolItem) {
if (!rolItem.id) {
throw new Error('No rol item to delete')
}

const endpoint = `${apiEndpoint}/${rolItem.id}`

const response = await fetch(endpoint, {
method: 'DELETE',
})

if (!response.ok) {
console.log(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

this.refreshRollenList()

return { response }
},
// Create or save a rol from store
async saveRol(rolItem) {
if (!rolItem) {
throw new Error('No rol item to save')
}

const isNewRol = !rolItem.id
const endpoint = isNewRol
? `${apiEndpoint}`
: `${apiEndpoint}/${rolItem.id}`
const method = isNewRol ? 'POST' : 'PUT'

const response = await fetch(
endpoint,
{
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(rolItem),
},
)

if (!response.ok) {
console.log(response)
throw new Error(`HTTP error! status: ${response.status}`)
}

const data = await response.json()
const entity = new Rol(data)

this.setRolItem(data)
this.refreshRollenList()

return { response, data, entity }
},
},
})
36 changes: 36 additions & 0 deletions src/store/modules/rol.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-console */
import { setActivePinia, createPinia } from 'pinia'

import { useZaakStore } from './zaken.js'
import { Zaak, mockZaak } from '../../entities/index.js'

describe('Zaak Store', () => {
beforeEach(() => {
setActivePinia(createPinia())
})

it('sets zaak item correctly', () => {
const store = useZaakStore()

store.setZaakItem(mockZaak()[0])

expect(store.zaakItem).toBeInstanceOf(Zaak)
expect(store.zaakItem).toEqual(mockZaak()[0])

expect(store.zaakItem.validate().success).toBe(true)
})

it('sets zaken list correctly', () => {
const store = useZaakStore()

store.setZakenList(mockZaak())

expect(store.zakenList).toHaveLength(mockZaak().length)

store.zakenList.forEach((item, index) => {
expect(item).toBeInstanceOf(Zaak)
expect(item).toEqual(mockZaak()[index])
expect(item.validate().success).toBe(true)
})
})
})
4 changes: 3 additions & 1 deletion src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import { useZaakTypeStore } from './modules/zaakTypen.js'
import { useKlantStore } from './modules/klanten.js'
import { useTaakStore } from './modules/taak.js'
import { useBerichtStore } from './modules/berichten.js'
import { useRolStore } from './modules/rol.js'

const navigationStore = useNavigationStore(pinia)
const zaakStore = useZaakStore(pinia)
const zaakTypeStore = useZaakTypeStore(pinia)
const klantStore = useKlantStore(pinia)
const taakStore = useTaakStore(pinia)
const berichtStore = useBerichtStore(pinia)
const rolStore = useRolStore(pinia)

export {
// generic
navigationStore,
zaakStore,
zaakTypeStore,
klantStore,
taakStore,
berichtStore,
rolStore,
}

0 comments on commit edcf230

Please sign in to comment.