Skip to content

Commit e970cc7

Browse files
committed
refactor: Create removeArrayElByKey utility
1 parent 502916d commit e970cc7

File tree

5 files changed

+17
-33
lines changed

5 files changed

+17
-33
lines changed

composables/useDeployment.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ export default function useDeployment (deviceId: Device['id']) {
4040

4141
onResponse: ({ response }) => {
4242
if (response.ok && deployments.data.value) {
43-
const deploymentIndex = deployments.data.value.findIndex(
44-
deployment => deployment.id === id
45-
)
46-
47-
if (deploymentIndex >= 0) {
48-
deployments.data.value.splice(deploymentIndex, 1)
49-
}
43+
removeArrayElByKey(deployments.data.value, 'id', id)
5044
}
5145
}
5246
})
@@ -83,13 +77,7 @@ export default function useDeployment (deviceId: Device['id']) {
8377

8478
function removeByRelease (releaseId: Release['id']) {
8579
if (deployments.data.value) {
86-
const deploymentIndex = deployments.data.value.findIndex(
87-
deployment => deployment.releaseId === releaseId
88-
)
89-
90-
if (deploymentIndex >= 0) {
91-
deployments.data.value.splice(deploymentIndex, 1)
92-
}
80+
removeArrayElByKey(deployments.data.value, 'releaseId', releaseId)
9381
}
9482
}
9583

composables/useDevice.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ export default function useDevice () {
2929

3030
onResponse: ({ response }) => {
3131
if (response.ok && devices.data.value) {
32-
const deviceIndex = devices.data.value.findIndex(
33-
device => device.id === id
34-
)
35-
36-
if (deviceIndex >= 0) {
37-
devices.data.value.splice(deviceIndex, 1)
38-
}
32+
removeArrayElByKey(devices.data.value, 'id', id)
3933
}
4034

4135
if (response.ok) {

composables/useProject.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ export default function useProject () {
2929

3030
onResponse: ({ response }) => {
3131
if (response.ok && projects.data.value) {
32-
const projectIndex = projects.data.value.findIndex(
33-
project => project.id === id
34-
)
35-
if (projectIndex >= 0) {
36-
projects.data.value.splice(projectIndex, 1)
37-
}
32+
removeArrayElByKey(projects.data.value, 'id', id)
3833
}
3934

4035
if (response.ok) {

composables/useRelease.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ export default function useRelease (projectId: Project['id']) {
2727

2828
onResponse: async ({ response }) => {
2929
if (response.ok && releases.data.value) {
30-
const releaseIndex = releases.data.value.findIndex(
31-
release => release.id === id
32-
)
33-
if (releaseIndex >= 0) {
34-
releases.data.value.splice(releaseIndex, 1)
35-
}
30+
removeArrayElByKey(releases.data.value, 'id', id)
3631
}
3732

3833
if (response.ok) {

utils/array.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Removes element from array having a certain value for a giving key
3+
*/
4+
export function removeArrayElByKey (array: Array<Record<string, any>>, key: string, value: any) {
5+
const index = array.findIndex(
6+
el => el[key] === value
7+
)
8+
9+
if (index >= 0) {
10+
array.splice(index, 1)
11+
}
12+
}

0 commit comments

Comments
 (0)