Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix refreshing of production list #1222

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/components/pages/Productions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,11 @@ export default {
'isProductionsLoading',
'isProductionsLoadingError',
'productionAvatarFormData',
'productions',
'productionMap'
'productions'
]),

currentLockText() {
if (this.productionToDelete) {
return this.productionToDelete.name
} else {
return ''
}
return this.productionToDelete?.name || ''
}
},

Expand Down
110 changes: 22 additions & 88 deletions src/store/modules/productions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import {
LOAD_PRODUCTION_STATUS_START,
LOAD_PRODUCTION_STATUS_ERROR,
LOAD_PRODUCTION_STATUS_END,
EDIT_PRODUCTION_START,
EDIT_PRODUCTION_ERROR,
EDIT_PRODUCTION_END,
DELETE_PRODUCTION_START,
DELETE_PRODUCTION_ERROR,
DELETE_PRODUCTION_END,
ADD_PRODUCTION,
UPDATE_PRODUCTION,
REMOVE_PRODUCTION,
Expand Down Expand Up @@ -343,25 +337,21 @@ const actions = {
},

newProduction({ commit, state }, data) {
commit(EDIT_PRODUCTION_START, data)
return productionsApi.newProduction(data).then(production => {
commit(EDIT_PRODUCTION_END, production)
commit(ADD_PRODUCTION, production)
return production
})
},

editProduction({ commit, state }, data) {
commit(EDIT_PRODUCTION_START)
return productionsApi.updateProduction(data).then(production => {
commit(EDIT_PRODUCTION_END, production)
commit(UPDATE_PRODUCTION, production)
})
},

deleteProduction({ commit, state }, production) {
commit(DELETE_PRODUCTION_START)
return productionsApi.deleteProduction(production).then(() => {
commit(REMOVE_PRODUCTION, production)
commit(DELETE_PRODUCTION_END)
})
},

Expand Down Expand Up @@ -631,92 +621,40 @@ const mutations = {
state.productionStatusMap = productionStatusMap
},

[EDIT_PRODUCTION_START](state, data) {},
[EDIT_PRODUCTION_ERROR](state) {},
[EDIT_PRODUCTION_END](state, newProduction) {
[ADD_PRODUCTION](state, production) {
const productionStatus = state.productionStatusMap.get(
newProduction.project_status_id
)
const production = state.productions.find(
production => production.id === newProduction.id
)
const openProduction = state.openProductions.find(
openProduction => openProduction.id === newProduction.id
production.project_status_id
)
newProduction.project_status_name = productionStatus.name

if (production) {
const openProductionIndex = state.openProductions.findIndex(
openProduction => openProduction.id === newProduction.id
)
if (newProduction.project_status_id) {
// Status changed from open to close
if (
openProductionIndex >= 0 &&
production.project_status_id !== newProduction.project_status_id
) {
state.openProductions.splice(openProductionIndex, 1)
// Status change from close to open
} else if (openProductionIndex < 0) {
state.openProductions = sortByName(state.openProductions)
}
}

if (
newProduction.production_type &&
newProduction.production_type !== production.production_type &&
newProduction.production_type === 'short'
) {
production.first_episode_id = undefined
openProduction.first_episode_id = undefined
}

Object.assign(production, newProduction)
if (openProduction) Object.assign(openProduction, newProduction)
if (
state.currentProduction &&
state.currentProduction.id === newProduction.id
) {
Object.assign(state.currentProduction, newProduction)
}
} else {
newProduction.team = []
newProduction.task_statuses = []
newProduction.asset_types = []
newProduction.task_types = []
newProduction.status_automations = []
state.productions.push(newProduction)
state.productionMap.set(newProduction.id, newProduction)
if (!openProduction) {
state.openProductions.push(newProduction)
state.openProductions = sortByName(state.openProductions)
}
state.productions = sortProductions(state.productions)
}
},

[ADD_PRODUCTION](state, production) {
production.project_status_name = productionStatus.name
state.productions.push(production)
state.productionMap.set(production.id, production)
state.productionMap = new Map(state.productionMap) // for reactivity
state.openProductions.push(production)
state.productions = sortProductions(state.productions)
state.openProductions = sortByName(state.openProductions)
state.productionMap.set(production.id, production)
},

[UPDATE_PRODUCTION](state, production) {
const previousProduction = state.productionMap.get(production.id)
const productionStatus = state.productionStatusMap.get(
production.project_status_id
const previousProduction = state.productions.find(
({ id }) => id === production.id
)
const openProduction = state.openProductions.find(
p => p.id === production.id
({ id }) => id === production.id
)
const productionStatus = state.productionStatusMap.get(
production.project_status_id
)

// status changed
const isStatusChanged =
previousProduction.project_status_id !== productionStatus.id

production.project_status_name = productionStatus.name

// update states
Object.assign(previousProduction, production)
if (openProduction) Object.assign(openProduction, production)
if (openProduction) {
Object.assign(openProduction, production)
}
if (isStatusChanged) {
if (production.project_status_name === 'Open') {
state.openProductions.push(previousProduction)
Expand All @@ -727,14 +665,12 @@ const mutations = {
)
}
}
state.productionMap.set(production.id, production)
state.productionMap = new Map(state.productionMap) // for reactivity
state.productions = sortProductions(state.productions)
state.openProductions = sortByName(state.openProductions)
},

[DELETE_PRODUCTION_START](state) {},

[DELETE_PRODUCTION_ERROR](state) {},

[REMOVE_PRODUCTION](state, productionToDelete) {
state.productions = removeModelFromList(
state.productions,
Expand All @@ -747,8 +683,6 @@ const mutations = {
state.productionMap.delete(productionToDelete.id)
},

[DELETE_PRODUCTION_END](state, productionToDelete) {},

[PRODUCTION_PICTURE_FILE_SELECTED](state, formData) {
state.productionAvatarFormData = formData
},
Expand Down
10 changes: 0 additions & 10 deletions src/store/mutation-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,6 @@ export const LOAD_PRODUCTION_STATUS_START = 'LOAD_PRODUCTION_STATUS_START'
export const LOAD_PRODUCTION_STATUS_END = 'LOAD_PRODUCTION_STATUS_END'
export const LOAD_PRODUCTION_STATUS_ERROR = 'LOAD_PRODUCTION_STATUS_ERROR'

export const NEW_PRODUCTION_END = 'NEW_PRODUCTION_END'

export const EDIT_PRODUCTION_START = 'EDIT_PRODUCTION_START'
export const EDIT_PRODUCTION_END = 'EDIT_PRODUCTION_END'
export const EDIT_PRODUCTION_ERROR = 'EDIT_PRODUCTION_ERROR'

export const DELETE_PRODUCTION_START = 'DELETE_PRODUCTION_START'
export const DELETE_PRODUCTION_END = 'DELETE_PRODUCTION_END'
export const DELETE_PRODUCTION_ERROR = 'DELETE_PRODUCTION_ERROR'

export const SET_CURRENT_PRODUCTION = 'SET_CURRENT_PRODUCTION'

export const PRODUCTION_PICTURE_FILE_SELECTED =
Expand Down
98 changes: 17 additions & 81 deletions tests/unit/store/productions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import {
ADD_PRODUCTION,
CLEAR_ASSETS,
CLEAR_SHOTS,
DELETE_PRODUCTION_END,
DELETE_PRODUCTION_START,
EDIT_PRODUCTION_END,
EDIT_PRODUCTION_START,
LOAD_OPEN_PRODUCTIONS_END,
LOAD_OPEN_PRODUCTIONS_ERROR,
LOAD_OPEN_PRODUCTIONS_START,
Expand All @@ -35,7 +31,6 @@ import {
UPDATE_METADATA_DESCRIPTOR_END,
UPDATE_PRODUCTION
} from '@/store/mutation-types'
import { sortByName, sortProductions } from '@/lib/sorting'

describe('Productions store', () => {
describe('Getters', () => {
Expand Down Expand Up @@ -354,18 +349,16 @@ describe('Productions store', () => {
productionApi.newProduction = vi.fn(productionId => Promise.resolve({ id: '1' }))
await store.actions.newProduction({ commit: mockCommit, state: null }, 'production-id')
expect(productionApi.newProduction).toBeCalledTimes(1)
expect(mockCommit).toBeCalledTimes(2)
expect(mockCommit).toHaveBeenNthCalledWith(1, EDIT_PRODUCTION_START, 'production-id')
expect(mockCommit).toHaveBeenNthCalledWith(2, EDIT_PRODUCTION_END, { id: '1' })
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, ADD_PRODUCTION, { id: '1' })

mockCommit = vi.fn()
productionApi.newProduction = vi.fn(productionId => Promise.reject(new Error('error')))
try {
await store.actions.newProduction({ commit: mockCommit, state: null }, 'production-id')
} catch (e) {
expect(productionApi.newProduction).toBeCalledTimes(1)
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, EDIT_PRODUCTION_START, 'production-id')
expect(mockCommit).toBeCalledTimes(0)
}
})

Expand All @@ -374,18 +367,16 @@ describe('Productions store', () => {
productionApi.updateProduction = vi.fn(productionId => Promise.resolve({ id: '1' }))
await store.actions.editProduction({ commit: mockCommit, state: null }, 'production-id')
expect(productionApi.updateProduction).toBeCalledTimes(1)
expect(mockCommit).toBeCalledTimes(2)
expect(mockCommit).toHaveBeenNthCalledWith(1, EDIT_PRODUCTION_START)
expect(mockCommit).toHaveBeenNthCalledWith(2, EDIT_PRODUCTION_END, { id: '1' })
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, UPDATE_PRODUCTION, { id: '1' })

mockCommit = vi.fn()
productionApi.updateProduction = vi.fn(productionId => Promise.reject(new Error('error')))
try {
await store.actions.editProduction({ commit: mockCommit, state: null }, 'production-id')
} catch (e) {
expect(productionApi.updateProduction).toBeCalledTimes(1)
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, EDIT_PRODUCTION_START)
expect(mockCommit).toBeCalledTimes(0)
}
})

Expand All @@ -394,19 +385,16 @@ describe('Productions store', () => {
productionApi.deleteProduction = vi.fn(productionId => Promise.resolve({ id: '1' }))
await store.actions.deleteProduction({ commit: mockCommit, state: null }, 'production-id')
expect(productionApi.deleteProduction).toBeCalledTimes(1)
expect(mockCommit).toBeCalledTimes(3)
expect(mockCommit).toHaveBeenNthCalledWith(1, DELETE_PRODUCTION_START)
expect(mockCommit).toHaveBeenNthCalledWith(2, REMOVE_PRODUCTION, 'production-id')
expect(mockCommit).toHaveBeenNthCalledWith(3, DELETE_PRODUCTION_END)
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, REMOVE_PRODUCTION, 'production-id')

mockCommit = vi.fn()
productionApi.deleteProduction = vi.fn(productionId => Promise.reject(new Error('error')))
try {
await store.actions.deleteProduction({ commit: mockCommit, state: null }, 'production-id')
} catch (e) {
expect(productionApi.deleteProduction).toBeCalledTimes(1)
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, DELETE_PRODUCTION_START)
expect(mockCommit).toBeCalledTimes(0)
}
})

Expand Down Expand Up @@ -693,67 +681,21 @@ describe('Productions store', () => {
expect(state.productionStatusMap.get(1)).toEqual({ id: 1, status: 'status' })
})

test.skip('EDIT_PRODUCTION_START', () => {})

test.skip('EDIT_PRODUCTION_ERROR', () => {})

test('EDIT_PRODUCTION_END', () => {
state.productionStatusMap = new Map()
state.productionMap = new Map()
state.productionStatusMap.set('Project status ID', { name: 'Status name' })
state.productionStatusMap.set('other Project status ID', { name: 'Status name 2' })
state.productions = [{ id: 1, project_status_id: 'other Project status ID', production_type: 'short', name: 'production3' }]
store.mutations.EDIT_PRODUCTION_END(state, {
id: 1,
name: 'production1',
project_status_id: 'Project status ID',
project_status_name: 'Project status name',
production_type: 'Production type',
first_episode_id: 1
})
store.mutations.EDIT_PRODUCTION_END(state, {
id: 2,
name: 'production2',
project_status_id: 'Project status ID',
project_status_name: 'Project status name',
production_type: 'Production type',
first_episode_id: 1
})
expect(state.productions[0]).toEqual({
id: 1,
name: 'production1',
project_status_id: 'Project status ID',
project_status_name: 'Status name',
production_type: 'Production type',
first_episode_id: 1
})
expect(state.productions[1]).toEqual({
id: 2,
name: 'production2',
project_status_id: 'Project status ID',
project_status_name: 'Status name',
production_type: 'Production type',
first_episode_id: 1,
team: [],
task_statuses: [],
asset_types: [],
task_types: [],
status_automations: [],
})
})

test('ADD_PRODUCTION', () => {
state.productionMap = new Map()
store.mutations.ADD_PRODUCTION(state, { id: 123, name: 'new production' })
state.productionStatusMap = new Map(Object.entries({
1: { name: 'old status' },
2: { name: 'new status' }
}))
store.mutations.ADD_PRODUCTION(state, { id: 123, name: 'new production', project_status_id: '2' })
expect(state.productions).toHaveLength(2)
expect(state.openProductions).toHaveLength(1)
expect(state.productionMap.get(123)).toEqual({ id: 123, name: 'new production' })
expect(state.productionMap.get(123)).toEqual({ id: 123, name: 'new production', project_status_id: '2', project_status_name: 'new status' })
})

test('UPDATE_PRODUCTION', () => {
state.productionMap = new Map(Object.entries({
123: { id: '123', name: 'old production', project_status_id: '1' }
}))
state.productions = [{ id: '123', name: 'old production', project_status_id: '1' }]
state.productionMap = new Map(state.productions.map(p => [p.id, p]))
state.productionStatusMap = new Map(Object.entries({
1: { name: 'old status' },
2: { name: 'new status' }
Expand All @@ -764,10 +706,6 @@ describe('Productions store', () => {
expect(state.productionMap.get('123')).toEqual({ id: '123', name: 'new production', project_status_id: '2', project_status_name: 'new status' })
})

test.skip('DELETE_PRODUCTION_START', () => {})

test.skip('DELETE_PRODUCTION_ERROR', () => {})

test('REMOVE_PRODUCTION', () => {
const production = { id: '123', name: 'old production', project_status_id: '1' }
state.productionMap = new Map(Object.entries({
Expand All @@ -781,8 +719,6 @@ describe('Productions store', () => {
expect(state.productionMap).toEqual(new Map())
})

test.skip('DELETE_PRODUCTION_END', () => {})

test('PRODUCTION_PICTURE_FILE_SELECTED', () => {
store.mutations.PRODUCTION_PICTURE_FILE_SELECTED(state, 'form-data')
expect(state.productionAvatarFormData).toEqual('form-data')
Expand Down